Portál AbcLinuxu, 5. května 2024 13:54


Dotaz: skript

31.12.2008 15:49 Mondavi
skript
Přečteno: 329×
Odpovědět | Admin
co je na tomhle skriptu špatně? Má se zkusit uhodnout číslo, které si myslí PC od 0 do 9.
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
Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

31.12.2008 16:01 cronin | skóre: 49
Rozbalit Rozbalit vše Re: skript
Odpovědět | | Sbalit | Link | Blokovat | Admin
Skolske zadanie?
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
Len som pouzil pre tag, aby sa to lahsie lustilo.
31.12.2008 16:04 cronin | skóre: 49
Rozbalit Rozbalit vše Re: skript
if [ "$num" -ge 1 ]
Tu ma byt patrne 0, nie 1.
31.12.2008 16:34 Mondavi
Rozbalit Rozbalit vše Re: skript
Ale mně to píše chybu už na tom prvním řádku - je to správně zapsané to gnerování čísel?
31.12.2008 20:32 cronin | skóre: 49
Rozbalit Rozbalit vše Re: skript
Ako to spúšťaš? Akým shellom to interpretuješ? Akú chybu to píše? Vešteckú guľu mám pokazenú, možno Ti pomôže návod.
1.1.2009 12:21 Mondavi
Rozbalit Rozbalit vše Re: skript
No používám linux sidux- jaký je v něm shell?
1.1.2009 13:31 cronin | skóre: 49
Rozbalit Rozbalit vše Re: skript
...
31.12.2008 16:11 cronin | skóre: 49
Rozbalit Rozbalit vše Re: skript
Odpovědět | | Sbalit | Link | Blokovat | Admin
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.
1.1.2009 14:02 fakenickname | skóre: 42 | blog: fakeblog
Rozbalit Rozbalit vše Re: skript
Odpovědět | | Sbalit | Link | Blokovat | Admin
doporučil bych si přečíst pozorně věci jako
$ 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..

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.