Portál AbcLinuxu, 5. května 2025 05:01
Zkoušel jsem to takto MAX="10" SELECT="25" DO="$("$MAX" + "$SELECT")" echo "Výsledek je: $DO"
Řešení dotazu:
DO=$(( $MAX + $SELECT ))
if [ $SEL < $MAX ] then echo "foo"; fiHlásí to chybu: /home/bash/test.sh: line 1: 25: No such file or directory Děkuji
if [ [ $SEL -lt $MAX ] ] then echo "foo"; fi
Nešťastně jste složil dohromady dvě rady, které byly myšleny jako dvě různé možnosti. Takže buď použijte klasický test
if [ $SEL -lt $MAX ]; then
nebo bashismus
if [[ $SEL < $MAX ]]; then
Pokud ale používáte dvojité hranaté závorky, nesmí mezi nimi být mezera, "[ [
" je něco úplně jiného než "[[
".
a proč nefunguje tohle [ $? -eq 1 -o $? -eq 255 ] && clear; break; a tohle funguje, je to prece stejný, ne? if [ $? -eq 1 -o $? -eq 255 ] then clear break fiDík
Není. To první je ekvivalentní
if [ $? -eq 1 -o $? -eq 255 ]; then clear fi break
bash$ [[ 01 < 1 ]] && echo wth wth bash$ [[ 01 -lt 1 ]] && echo wth bash$ [[ 01 -eq 1 ]] && echo ok okale někdy i taková malá chybka může způsobit problém, takže pozor na to.
man bash: [[ expression ]] ... When used with [[, the < and > operators sort lexicographically using the current locale. ... CONDITIONAL EXPRESSIONS When used with [[, the < and > operators sort lexicographically using the current locale. The test command sorts using ASCII ordering. ... string1 == string2 string1 = string2 True if the strings are equal. = should be used with the test command for POSIX conformance. string1 != string2 True if the strings are not equal. string1 < string2 True if string1 sorts before string2 lexicographically. string1 > string2 True if string1 sorts after string2 lexicographically. arg1 OP arg2 OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
bash$ [[ 1 < -1 ]] && echo wth wth ash$ [[ 1 -lt -1 ]] && echo wth bash$ [[ 1 -gt -1 ]] && echo ok okS ohledem na toto bych <>= vůbec pro čísla nedoporučoval.
[[ 10 < 9 ]] && echo wth
bash$ sel="" bash$ max="20" bash$ [ $sel -lt $max ] && echo ok bash: [: -lt: unary operator expecteda docílilo se relevantnějších chybových hlášení:
bash$ [ "$sel" -lt "$max" ] && echo ok bash: [: : integer expression expectedU dvojitých uvozovek to není potřeba. Takže shrnuto:
... buď použijte klasický test if [ "$SEL" -lt "$MAX" ]; then nebo bashismus if [[ $SEL -lt $MAX ]]; then
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.