Portál AbcLinuxu, 9. května 2025 21:34

Dotaz: bash-aritmetika s argumenty

27.11.2008 11:02 king
bash-aritmetika s argumenty
Přečteno: 566×
Odpovědět | Admin

Ahoj, chtel bych provadet aritmetiku nad argumenty sheelskriptu ($1, $2, ...). Neco ve stylu:

for arg in $(seq $#); do

   echo "argument cislo $arg: ${$arg}

done

Bash mi ale rve Bad substitution. Nevite, jak provadet expanzi za nazev promenne? dekuji 

Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

27.11.2008 11:25 Jan Šimák | skóre: 37 | Hradec Králové
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty
Odpovědět | | Sbalit | Link | Blokovat | Admin
for arg in $(seq $@); do
   echo "argument cislo $arg: $arg"
done
27.11.2008 11:35 king
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty

hmmm

a kdyz budu chtit skocit o 3 doprava a pak o 2 doleva? 

27.11.2008 11:58 Jan Šimák | skóre: 37 | Hradec Králové
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty
declare -a pole

pole=( $(seq -s ' ' $@) )

echo "${pole[@]}"
echo "${pole[3]}"
echo "${pole[1]}"
27.11.2008 12:05 Semo | skóre: 45 | blog: Semo
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty
Odpovědět | | Sbalit | Link | Blokovat | Admin
Len na vypisanie argumentov ti staci jednoduchsi cyklus
for i; do
# to iste ako for i in *; do
    echo "$i"
done
Ak to chces cez cislo argumentu, tak potrebujes nepriame odkazovanie
for ((i=1; i<$#; i++)); do
    echo "argument cislo $i: ${!i}"
done
Ak chces najprv robit aritmetiku o tom, ktory argument vlastne chces;
i=2
j=$((i+4))
echo "argument cislo $j: ${!j}"
If you hold a Unix shell up to your ear, you can you hear the C.
27.11.2008 13:01 king
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty

to je ono!

${!variable}

divne je ze manualova stranka bashe se o teto konstrukci nezminuje.

27.11.2008 14:33 Semo | skóre: 45 | blog: Semo
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty
Ale zmienuje. v casti Parameter Expansion

If the first character of parameter is an exclamation point, a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
If you hold a Unix shell up to your ear, you can you hear the C.
27.11.2008 13:23 Jan Šimák | skóre: 37 | Hradec Králové
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty
v cyklu to zle udělat i třeba takto:
argCislo=1

for arg in "$@"; do
	echo "argument cislo: $argCislo je: $arg"
	let argCislo++
done
27.11.2008 19:13 luky
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty

Coz ovsem neni v nicem lepsi reseni nez to predchozi, nemluve o tom, ze pointa je jinde :) 

27.11.2008 19:20 Jan Šimák | skóre: 37 | Hradec Králové
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty
Tady ale nešlo o ukázku lepšího řešení :-D které z podstaty ani být nemůže ;-)
27.11.2008 12:10 Jirka P
Rozbalit Rozbalit vše Re: bash-aritmetika s argumenty
Odpovědět | | Sbalit | Link | Blokovat | Admin

Dá se klasicky přes eval:

eval echo "argument cislo $arg: \${$arg} 

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.