how to create a array in unix shell programming ?
and i have to excute the program still i press a key similar to do u want to continue option
how to create a array in unix shell programming ?
and i have to excute the program still i press a key similar to do u want to continue option
As to your question about creating an array in a shell script, I haven't personally done it, but I found the following:
http://www.linuxtopia.org/online%5Fbooks/advanced%5Fbash%5Fscripting%5Fguide/arrays.html
Interesting how accessing elements within an array requires such a different notation.
You can try of the following type :
#!/bin/bash
declare -a arr
i=0
j=0
for dir in $(find /home/rmajeti/programs -type d)
do
arr[i]=$dir
i=$((i+1))
done
while [ $j -lt $i ]
do
echo ${arr[$j]}
j=$((j+1))
done
#!/bin/bash
# define a array, space to separate every item
foo=(foo1 foo2)
# access
echo ${foo[1]}
# add or changes
foo[0]=bar
foo[2]=cat
foo[1000]=also_OK
You can read the ABS "Advanced Bash-Scripting Guide"
The Bourne shell and C shell don't have arrays, IIRC.
In addition to what others have said, in Bash you can get the number of elements in an array:
elements=${#arrayname[@]}
and do slice-style operations:
array=(apple banana cherry)
echo ${arrayname[@]:1} # yields "banana cherry"
echo ${arrayname[@]: -1} # yields "cherry"
echo ${arrayname[${#arrayname[@]}-1]} # yields "cherry"
echo ${arrayname[@]:0:2} # yields "apple banana"
echo ${arrayname[@]:1:1} # yields "banana"