Portál AbcLinuxu, 10. května 2025 01:12
SLOVA="sport hash hush spool hurry hill speak spoke spoken guru gravity"podle sveho prvniho parametru, napr.:
ta_funkce "hu"vypsala:
hush hurry hurricanePri parametru
"sp"
by vypsala sport speak spoke spoken
, pri parametru "hur"
by vypsala hush hurry hurricane
, pri parametru "spor"
by vypsala jen sport
atd?
Diky za napady.
"hur"
nema samosebou vypsat to hush
, snad je to jasne... a do SLOVA
pridejte jeste to slovo hurricane
. SLOVA=" sport hash hush spool hurry hill speak spoke spoken guru gravity" for word in $SLOVA; do TMP=${word#$1} [ "$TMP" != "$word" ] && echo $word done
ta_funkce() { for i in $SLOVA do [[ "$i" == "$1"* ]] && echo $i done }
ahoj, je to tu uz davno, ale mal by som dotaz
vysvetli mi niekto, preco su v cykle dve hranate zatvorky? :)
[[ "$i" == "$1"* ]] && echo $i
rozumiem, ze jedna z nich je prikaz test, ale neviem, preco je tam ta druha (resp. prva)
[
není to samé jako [[
$ 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.
x$x = x$y -o $z -eq 1
aha, takze figel je v tom, ze v [[ mozem pouzivat expressions a v [ ] nemozem? :)
neviem, ci je to uplne spravne prepisane, ale vlastne takto by to bolo bez [[ ?
echo "$SLOVA" > subor
cat subor | tr " " "\n" | echo `grep "^$1.*"`
neviem, ci som to dobre povedal, tak som to pochopil, ze napr ten zapis "$1"* nemoze byt v [ ] teda wildkardy sa mozu pouzit len v [[....
alebo opravte ma niekto, ak sa mylim
$ set +v $ [ "ahoj" = aho* ] && echo ok? + '[' ahoj = 'aho*' ']' $ [[ "ahoj" == aho* ]] && echo ok? + [[ ahoj == aho* ]] + echo 'ok?' ok? $ set -vale nevím jestli to je k něčemu :) Od verze bash 3.? lze použít i
[[ výraz =~ ^regexp$ ]]
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.