mit einem ShellScript

Hi Community,
ich wollte ein Shellscript schreiben welches eine Zufallszahl zwischen 1 und 360 findet und einen "Treffer" notiert falls die Zahl unter 50 ist. So weit so gut, das ist mein Code:
Code:
clear

echo -n "Anzahl der Durchläufe: "
read anzahl

while [$i -le $anzahl]
do
	zufallszahl = `expr $RANDOM % 360`
	if [ $zufallszahl < 50 ] 
	then
	$treffer=`expr $treffer + 1`	
	fi
	i=`expr $i + 1`
done

echo "Es gab $treffer Treffer"
Nur leider kommt dann eine Fehlermeldung:
Code:
[: 14: missing ]
Was heißt das??

MfG Felix
 
Ich glaube du hast eine Strichpunkt vergessen.
Code:
while [$i -le $anzahl] ;

Gruß odigo
 
Code:
clear

echo -n "Anzahl der Durchläufe: "
read anzahl

i=0
while [ $i -le $anzahl ]
do
	zufallszahl = `expr $RANDOM % 360`
	if [ $zufallszahl < 50 ] 
	then
	$treffer=`expr $treffer + 1`	
	fi
	i=`expr $i + 1`
done

echo "Es gab $treffer Treffer"
 
Es gibt 2 Möglichkeiten für die while-Schleife. Entweder man nutzt

Code:
while [ $i -le $anzahl ]
do
...
done

oder man nutzt

Code:
while [ $i -le $anzahl ]; do
...
done

Wichtig: Die Klammern müssen von Freizeichen umgeben sein.
 
Also mein Code sieht jetzt so aus:
Code:
clear

echo -n "Anzahl der Durchläufe: "
read anzahl

i=0
while [ $i -le $anzahl ]
do
	zufallszahl = ´expr $RANDOM % 360`
	  if [ $zufallszahl < 50 ]
	    then
	    $treffer= ´expr $treffer + 1`	
	  fi
	i=`expr $i + 1`
done
echo "Es gab $treffer Treffer"
Nur kommt jetzt diese Fehlermeldung wenn ich "anzahl" eingebe:
Code:
Anzahl der Durchläufe: 3
08_zufall: 4: Syntax error: end of file unexpected (expecting "fi")

Was ist das jetzt? Da steht doch fi
 
man bash :rolleyes:

1. Backticks verwenden, wenn du einen eingebetteten Befehl ausführen willst. Also nicht

Code:
zufallszahl = ´expr $RANDOM % 360`

sondern

Code:
zufallszahl = `expr $RANDOM % 360`

2. if-Anweisung korrekt schreiben:

Code:
if [ ausdruck ]; then
  ...
fi

3. $zufallszahl < 50 wird vermutlich zu folgender Meldung führen:

Code:
50: No such file or directory

da '<' eine Umleitung ist. Daher... 'man bash' :D
 
Original von bitmuncher
man bash :rolleyes:

1. Backticks verwenden

2. if-Anweisung korrekt schreiben:

3. $zufallszahl < 50 wird vermutlich zu folgender Meldung führen:

1. Dann kommt Syntaxfehler, wenn ich ´expr ...` mache kommt nix ?(
2. Hab ich geändert
3. Hab ich durch -le ersetzt

4. Trotzdem kommt der Fehler noch
Code:
clear

echo -n "Anzahl der Durchläufe: "
read anzahl

i=0
while [ $i -le $anzahl ] ; do
	zufallszahl = ´expr $RANDOM % 360`
	  if [ $zufallszahl -le 50 ] ; then
	    treffer= ´expr $treffer + 1`	
	  fi
	i=´expr $i + 1`
done
echo "Es gab $treffer Treffer"
echo ""

Der Fehler:
Code:
3: Syntax error: end of file unexpected (expecting "fi")
 
Ich bin nun wirklich kein Bashskript-Profi und würde den Spass eher als Perl-Skript schreiben, aber ich bekomme keine Syntax-Fehler, wenn ich alle expr-Ausdrücke in Backticks klammere. Daher: 'man bash' :P
 
Original von bitmuncher
Ich bin nun wirklich kein Bashskript-Profi und würde den Spass eher als Perl-Skript schreiben, aber ich bekomme keine Syntax-Fehler, wenn ich alle expr-Ausdrücke in Backticks klammere. Daher: 'man bash' :P
Perl kann ich nicht!
Habs das Programm ja schon in C++ geschrieben. Wollte es halt mal zum Spaß als ShellScript machen.
Trotzdem Danke an alle!
 
Original von weau
Hi,

was macht denn read anzahl ? ist das wie $ eine Kommandozeilenübergabe?

read kann man nutzen um eine Eingabe von STDIN zu lesen

Code:
echo "dein name:"
  read name;
echo "du heisst $name";

oder um eine Datei zeilenweise einzulesen

Code:
cat "eine_datei" | while read x y z; do
  print "$x $y $z";
done

Näheres dazu findet sich in 'man bash'.

Code:
       read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]
              One line is read from the standard input, or from the file descriptor fd supplied as an argument to the -u
              option, and the first word is assigned to the first name, the second word to the second name, and  so  on,
              with  leftover words and their intervening separators assigned to the last name.  If there are fewer words
              read from the input stream than names, the remaining names are assigned empty values.  The  characters  in
              IFS  are used to split the line into words.  The backslash character (\) may be used to remove any special
              meaning for the next character read and for line continuation.  Options, if supplied, have  the  following
              meanings:
              -a aname
                     The  words are assigned to sequential indices of the array variable aname, starting at 0.  aname is
                     unset before any new values are assigned.  Other name arguments are ignored.
              -d delim
                     The first character of delim is used to terminate the input line, rather than newline.
              -e     If the standard input is coming from a terminal, readline (see READLINE above) is  used  to  obtain
                     the line.
              -n nchars
                     read returns after reading nchars characters rather than waiting for a complete line of input.
              -p prompt
                     Display  prompt on standard error, without a trailing newline, before attempting to read any input.
                     The prompt is displayed only if input is coming from a terminal.
              -r     Backslash does not act as an escape character.  The backslash is considered to be part of the line.
                     In particular, a backslash-newline pair may not be used as a line continuation.
              -s     Silent mode.  If input is coming from a terminal, characters are not echoed.
              -t timeout
                     Cause  read  to  time out and return failure if a complete line of input is not read within timeout
                     seconds.  This option has no effect if read is not reading input from the terminal or a pipe.
              -u fd  Read input from file descriptor fd.

              If no names are supplied, the line read is assigned to the variable  REPLY.   The  return  code  is  zero,
              unless  end-of-file is encountered, read times out, or an invalid file descriptor is supplied as the arguâ
              ment to -u.
 
Zurück
Oben