Portál AbcLinuxu, 10. května 2025 09:23
cislo=$(($RANDOM % 10)) Konec=ne num=0 echo "Prave si myslim nejake cislo od 0 do 9. Zkus uhodnout jake. " echo while test $Konec = ne do echo -n "Hadejte: " read num Dobre=ne JeCislo=ne delka=`expr length "$num"` f test $delka = 1 then if [ "$num" -ge 1 ] && [ "$num" -le 9 ] then Dobre=ano else echo -n "Zadejte CISLO OD 0 DO 9! " fi f `test $Dobre = ano` && `test $num = $cislo` then echo -n "Gratulujeme, uhodl jste. Bylo to cislo " echo "$num" Konec=ano elif [ $Dobre = ne ] then echo "Toto cislo to bohuzel neni. Zkuste jine." fi done
cislo=$(($RANDOM % 10)) Konec=ne num=0 echo "Prave si myslim nejake cislo od 0 do 9. Zkus uhodnout jake. " echo while test $Konec = ne do echo -n "Hadejte: " read num Dobre=ne JeCislo=ne delka=`expr length "$num"` f test $delka = 1 then if [ "$num" -ge 1 ] && [ "$num" -le 9 ] then Dobre=ano else echo -n "Zadejte CISLO OD 0 DO 9! " fi f `test $Dobre = ano` && `test $num = $cislo` then echo -n "Gratulujeme, uhodl jste. Bylo to cislo " echo "$num" Konec=ano elif [ $Dobre = ne ] then echo "Toto cislo to bohuzel neni. Zkuste jine." fi doneLen som pouzil pre tag, aby sa to lahsie lustilo.
if [ "$num" -ge 1 ]Tu ma byt patrne 0, nie 1.
co je na tomhle skriptu špatně?Jednoducho povedane, takmer vsetko. Ked som pochopil ze f znamena if, tak som dosiel k zaveru, ze skript nie je mozne spravne ani spustit; nesedia uzatvorenia if-ov. No a ked si to syntakticky opravis, skus si vziat pero a papier a nakreslit si workflow diagram.
$ help if if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi The `if COMMANDS' list is executed. If its exit status is zero, then the `then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list is executed in turn, and if its exit status is zero, the corresponding `then COMMANDS' list is executed and the if command completes. Otherwise, the `else COMMANDS' list is executed, if present. The exit status of the entire construct is the exit status of the last command executed, or zero if no condition tested true. $ help while while: while COMMANDS; do COMMANDS; done Expand and execute COMMANDS as long as the final command in the `while' COMMANDS has an exit status of zero. $ help [ [: [ arg... ] This is a synonym for the "test" builtin, but the last argument must be a literal `]', to match the opening `['. [[ ... ]]: [[ expression ]] Returns a status of 0 or 1 depending on the evaluation of the conditional expression EXPRESSION. Expressions are composed of the same primaries used by the `test' builtin, and may be combined using the following operators ( EXPRESSION ) Returns the value of EXPRESSION ! EXPRESSION True if EXPRESSION is false; else false EXPR1 && EXPR2 True if both EXPR1 and EXPR2 are true; else false EXPR1 || EXPR2 True if either EXPR1 or EXPR2 is true; else false When the `==' and `!=' operators are used, the string to the right of the operator is used as a pattern and pattern matching is performed. The && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to determine the expression's value. $a vůbec celý 'man bash' by neuškodil. podívej se po nějakých bash how-to (je jich hodně českých), většina věcí co se snažíš udělat je většinou dobře rozebraná v každém takovém článku..
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.