Bash
From AdminWiki
(Difference between revisions)
Revision as of 01:02, 24 May 2006
Bash
Bash is the most common shell form on linux, and most other unix systems.
Just a small reminder. Before you start writing a long bash script, think if you can do this in perl.
Questions and Solutions
Arrays in Bash:
foo[0]=1;
foo[1]=2;
foo[2]=3;
# loop
for (( i=0; i<${#foo[@]}; i++ ));
do
echo ${foo[$i]};
done;
# more like a foreach
for i in ${foo[@]};
do
echo $i;
done;
Variable Variables
foobar=5;
bar="foobar";
echo $foobar;
echo $bar;
echo ${!bar};
Variable Variables in Arrays
foo[0]=1;
foo[1]=2;
foo[2]=3;
foo_tcp[0]="AT";
foo_upd[0]="AU";
foo_tcp[1]="BT";
foo_upd[1]="BU";
foo_tcp[2]="CT";
foo_upd[2]="CU";
# loop
for (( i=0; i<${#foo[@]}; i++ ));
do
echo ${foo[$i]};
for k in upd tcp;
do
data=foo_${k}[$i];
echo ${!data};
done;
done;
More Documentation
The best advanced guide for bash scripting: http://www.tldp.org/LDP/abs/html/