Avatar billede mnc Nybegynder
01. september 2004 - 13:16 Der er 5 kommentarer og
1 løsning

Hjælp til nogle shell scripts

Hey eksperter.

Jeg sidder her med en opgave i skolen (H4 på datafagtekniker uddannelsen) hvor vi skal lave nogle shell scripts...
Indtil videre har jeg fået lavet mig et shell script der søger /home/* igennem for ulovlige filer (defineret i scriptet), og smider det i en mappe med navn efter PID-dags dato, f.eks. 30254-01-09-04, og lægger en log over outputtet deri. Jeg mangler nu følgende opgaver:

5: Udbyg scriptet så der fra kommando prompten kan angives hvilke(n) filtype(r) der er ulovlig(e).

6: Udbyg scriptet, så ulovlige filer slettes.

7: Udbyg scriptet så der genereres en rapport, visende antallet af ulovlige filer ialt, samt antallet af ulovlige filer fordelt pr. bruger.

8: Output rapporterne skal HTML eller XML formatteres (jeg foretrækker her, HTML)

9: Få systemet til at køre dit script 2 gange pr. døgn.

---------

Det jeg ønsker er hints til hvordan jeg kunne lave det, og helst med små eksempler... Mit script ser indtil videre ud som følgende:

# Variablen MAPPE saettes til $$ (PID) og saa dags dato, i formatet PID-dd-mm-yy, et eksempel kunne vaere 30572-01-09-04
MAPPE=($$-$(date +%d-%m-%y))

# Mappen laves :]
mkdir /home/$MAPPE

# Find, gennemkigger /home/ og undermapper, case INsensitivt, for *.mp3 eller *.mpg eller *.exe
# Og >> skriver det til files.log som placeres i den mappe vi lige har oprettet :]
find /home -iname *.mp3 -o -iname *.mpg -o -iname *.exe >> /home/$MAPPE/files.log

-----

Håber i kan hjælpe mig med mine problemer her, da jeg ikke er den største haj til programmering... (håber det kommer :P)

Mnc
Avatar billede mnc Nybegynder
01. september 2004 - 13:56 #1
Bør måske lige tilføje at 8'eren er mindre vigtig, da den er lidt sværere...
De vigtigste opgaver er 5,6,7 & 9

Mnc
Avatar billede strych9 Praktikant
01. september 2004 - 17:40 #2
Måske dette her hjælper:

4.2.1 Positional Parameters
As we have already seen, you can define values for variables with statements of the form varname=value, e.g.:

$ hatter=mad
$ echo "$hatter"
mad
Some environment variables are predefined by the shell when you log in. There are other built-in variables that are vital to shell programming. We will look at a few of them now and save the others for later.

The most important special, built-in variables are called positional parameters. These hold the command-line arguments to scripts when they are invoked. Positional parameters have the names 1, 2, 3, etc., meaning that their values are denoted by $1, $2, $3, etc. There is also a positional parameter 0, whose value is the name of the script (i.e., the command typed in to invoke it).

Two special variables contain all of the positional parameters (except positional parameter 0): * and @. The difference between them is subtle but important, and it's apparent only when they are within double quotes.

"$*" is a single string that consists of all of the positional parameters, separated by the first character in the environment variable IFS (internal field separator), which is a space, TAB, and NEWLINE by default. On the other hand, "$@" is equal to "$1" "$2"... "$N", where N is the number of positional parameters. That is, it's equal to N separate double-quoted strings, which are separated by spaces. If there are no positional parameters, "$@" expands to nothing. We'll explore the ramifications of this difference in a little while.

The variable # holds the number of positional parameters (as a character string). All of these variables are "read-only," meaning that you can't assign new values to them within scripts.

For example, assume that you have the following simple shell script:

echo "alice: $@"
echo "$0: $1 $2 $3 $4"
echo "$# arguments"
Assume further that the script is called alice. Then if you type alice in wonderland, you will see the following output:

alice: in wonderland
alice: in wonderland
2 arguments
In this case, $3 and $4 are unset, which means that the shell will substitute the empty (or null) string for them. [3]

[3] Unless the option nounset is turned on, in which case the shell will return an error message.

4.2.1.1 Positional parameters in functions
Shell functions use positional parameters and special variables like * and # in exactly the same way as shell scripts do. If you wanted to define alice as a function, you could put the following in your .bash_profile or environment file:

function alice
{
    echo "alice: $*"
    echo "$0: $1 $2 $3 $4"
    echo "$# arguments"
}
You will get the same result if you type alice in wonderland.

Typically, several shell functions are defined within a single shell script. Therefore each function will need to handle its own arguments, which in turn means that each function needs to keep track of positional parameters separately. Sure enough, each function has its own copies of these variables (even though functions don't run in their own subshells, as scripts do); we say that such variables are local to the function.

However, other variables defined within functions are not local (they are global), meaning that their values are known throughout the entire shell script. For example, assume that you have a shell script called ascript that contains this:

function afunc
{
  echo in function: $0 $1 $2
  var1="in function"
  echo var1: $var1
}

var1="outside function"
echo var1: $var1
echo $0: $1 $2
afunc funcarg1 funcarg2
echo var1: $var1
echo $0: $1 $2
If you invoke this script by typing ascript arg1 arg2, you will see this output:

var1: outside function
ascript: arg1 arg2
in function: ascript funcarg1 funcarg2
var1: in function
var1: in function
ascript: arg1 arg2
In other words, the function afunc changes the value of the variable var1 from "outside function" to "in function," and that change is known outside the function, while $1 and $2 have different values in the function and the main script. Notice that $0 doesn't change because the function executes in the environment of the shell script and $0 takes the name of the script. Figure 4.2 shows the scope of each variable graphically.
Avatar billede strych9 Praktikant
01. september 2004 - 17:42 #3
angående opgave 9, så ville det mest relevante være at køre et cron job. Jeg ved ikke om det er en gyldig løsning til opgaven?

Sig til hvis du har behov for flere hints..
Avatar billede strych9 Praktikant
01. september 2004 - 17:44 #4
Det er da egentlig en ordentlig omgang bash-fu du er blevet kastet ud i. Middelsvære til svære opgaver. Vidste ikke at edb fagteknikere kunne den slags.

Men i hvert fald skal du have held og lykke med din læringsprocess!
Avatar billede mnc Nybegynder
02. september 2004 - 12:33 #5
Jeg fik selv lavet frem til opgave 6, og fik næsten denne til at virke.. Cron job blev også nævnt i klassen, og det blev godtaget som en løsning :]

Blev egentlig ret forvirret over den lange der, hehe... Vi fandt frem til at bruge ting som "find" og "$@" i for løkker og sådan :]

men ellers, tak for hjælpen
Mnc
Avatar billede strych9 Praktikant
02. september 2004 - 17:03 #6
jamen så vil jeg da lægge et svar hvis du endte med at benytte positional parameters
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester