Bash Scripting Exercise
To be performed on your Linux machine
Exercise 1: Every bash script usually begins with a Shebang (#!) – It is used to specify the absolute path of the bash.
Example: #!/bin/bash
Create a folder in your home folder called BASH. cd to BASH.
Gedit a file: mybash1.sh. Add the above shebang and save. Change the mode of mybash1.sh to an executable. Recall our Linux exercise on changing modes.
Exercise 2: Add this code in mybash1.sh. Do not just copy. Type it in with tab indentation.
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome number: $i"
done
Execute mybash1.sh
Exercise 3: This exercise pertains to for loops for repetitive tasks. Example for loop script is as follows (type it in and mind the indentation):
#!/bin/bash
for ((i=0;i<12;i++))
do
echo "Welcome number: $i"
done
Create a new file called mybash2.sh. Can you modify the above code to create folders: lab2 to lab12?
Exercise 4: This exercise pertains to conditional statements in bash scripts. The conditional expressions in bash may use a C-like syntax as follows:
if ((<some C-like conditional>))
then
<commands>
fi
Create a new file called mybash3.sh. Can you modify the above code to create folders: lab02, lab03,..,lab09,.. lab12?
Arrays in Bash:
Syntax:
declare -a arrayname=(element1 element2 element3)
Example:
declare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');
Length of an array is queried as: ${#ArrayName[@]}
An example of array element access at the ith position:
${ArrayName[i]}
Putting it all together:
I want to run the amplification program on Lenna_org_1024.pgm, gaussian width = 11, and for sigma values: 0.3, 0.4, …1.1 (totaling 9 executions). Automate these lines:
./amplify Lenna_org_1024.pgm 11 0.3 2
./amplify Lenna_org_1024.pgm 11 0.4 2
./amplify Lenna_org_1024.pgm 11 0.5 2
:
./amplify Lenna_org_1024.pgm 11 1.1 2
Exercise 5:
I want to run the amplification program on Lenna_org_1024.pgm for sigma values: 0.3, 0.4, …1.1 AND
gaussian widths: 5, 7, 11, 15, and 19. Automate this execution.
Script away Lab 7!!