Shell Script: Arrays
Lab 10 – Shell Script: Arrays
Part I
Use array to write a shell script to print out the information about the files and directories
in the current directory according to the following sample run.
>>>>> ls
compute print repeat
>>>>> compute
practice: shell command and array
list all directories and files in current directory: compute print repeat
total number of files and directories: 3
the first element in this list: compute
all elements in this list:
compute
print
repeat
>>>>>
Part II (choice A)
Two files courses and emails are given where columns are separated by tabs.
courses:
101 60212 90
101 60256 81
102 60231 70
102 60212 85
103 60256 n/a
emails:
101 101@uwindsor.ca
102 102@uwindsor.ca
103 103@uwindsor.ca
Write a bash script to find the emails of all students taking a specific course, which is
given as a command line argument.
Hints: use grep command for the search, and use array to store intermediate data.
Sample output:
>>>>> mysolution
usage: mysolution course-number
>>>>> mysolution 60200
course number 60200 not found
>>>>> mysolution 60256
101@uwindsor.ca
103@uwindsor.ca
Part II (choice B)
Two files 60-256 and 60-212 are given. Each contains two columns for student ids and
the marks. Columns are separated by tabs. Write a bash script to print out all the ids of
the students who passed both 60-256 and 60-212.

Part III (optional)
In the current directory, there are several files starting with 60-. The total number of such
files could be changed, and the names of these files are not known. Each file contains two
columns: student ids and the marks. Columns are separated by tabs. Write a bash script to
print out all the ids of the students who passed all the courses.
Answer:
PART 1
#!/bin/bash
arr=(`ls`)
size=${#arr[*]}
first=${arr[0]}
echo practice shell command and array
echo list all directories and files in the current directory ${arr[*]}
echo total number of files and directories: $size
echo the first element in this list: $first
echo all elements in this list:
for i in ${arr[@]}; do
echo $i
done
PART 2-A
#!/bin/bash
email="/home/ho11z/Desktop/256/Lab10/emails" #location of emails
course="/home/ho11z/Desktop/256/Lab10/courses" #location of courses
if [ -z $(grep "$1" < "$course") ]
then
echo "$1 not found" # not found
else
arr=$(grep "$1" < "$course" | cut -d' ' -f1)
for i in ${arr[*]}
do
grep "$i" < "$email" | cut -f 2
done
fi # end if statement
PART 2-B
#! /bin/bash
array1=()
array2=()
array3=()
array4=()
arrayp1=()
arrayp2=()
min=49
h=0 n=0 r=0 f=0 x=0 d=0 a=0
size3=$((($((wc -l|cut -f1) < 60-256))))
size4=$((($((wc -l|cut -f1) < 60-212))))
while [ $x -lt $size3 ]; do
array1[$x]=$((($((cut -f1 | head -n $(($x+1)) | tail -n 1) < 60-256))))
array2[$x]=$((($((cut -f2 | head -n $(($x+1)) | tail -n 1) < 60-256))))
let x++
done
for e in "${array2[@]}"
do
if [ "${array2[n]}" -gt "$min" ]
then
arrayp1[$h]=${array1[$n]}
let h++
fi
let n++
done
while [ $a -lt $size4 ]; do
array3[$a]=$((($((cut -f1 | head -n $(($a+1)) | tail -n 1) < 60-212))))
array4[$a]=$((($((cut -f2 | head -n $(($a+1)) | tail -n 1) < 60-212))))
let a++
done
while [ $f -lt $size3 ]; do
if [[ "${array4[$f]}" > $min ]]
then
arrayp2[$r]=${array3[$f]}
let r++
fi
let f++
done
l2=" ${arrayp2[*]} "
for item in ${arrayp1[@]}; do
if [[ $l2 =~ " $item " ]] ; then
result+=($item)
fi
done
size_result=${#result[@]}
while [ $d -lt $size_result ]
do
echo ${result[$d]}
let d++
done
Leave a reply