Handout10-Bash Shell Commands and Perl Programming (1)
Handout10-Bash Shell Commands and Perl Programming (1)
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 2 of 6
Handout 10 - Bash Shell Commands and Perl Programming
_____________________________________________________________________________________
The common field is not necessary to be the first column of a file. If the common field is not the first
column, option “-j” can be used to specify the common field, such as “join –j 2 file1 file2.”
How to read a file line by line in a bash script using either a while-loop or a for-loop
A while-loop example:
$ cat d1
Apple $2.15
Pear $2.50
Orange $1.80
$
$ cat > myread
#!/bin/bash
while read -r line
do
echo "$line"
done < $1
^D
$
$ chmod +x myread
$ ./myread d1
Apple $2.15
Pear $2.50
Orange $1.80
$
A for-loop example:
$ cat d2
2 $4.30
3 $7.50
2 $3.60
$
$ cat > mysum
#!/bin/bash
echo ""
echo "Input numbers are:"
# the following line makes newlines the only separator
IFS=$'\n'
for mynext in `cut -d' ' -f1 $1`
do
echo "$mynext"
let t=t+mynext
done
echo "============="
echo "Total is $t"
echo ""
exit 0
^D
$
$ chmod +x mysum
$ ./mysum d2
Input numbers are:
2
3
2
=============
Total is 7
$
A for-loop is often used to process a file generated on the fly in a bash shell script.
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 3 of 6
Handout 10 - Bash Shell Commands and Perl Programming
_____________________________________________________________________________________
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 4 of 6
Handout 10 - Bash Shell Commands and Perl Programming
_____________________________________________________________________________________
Time = 00:58)
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 5 of 6
Handout 10 - Bash Shell Commands and Perl Programming
_____________________________________________________________________________________
hour = 04
minute = 00
cfs264sp2541 pts/2 98.61.15.234 Mon Jan 13 18:46 - 18:51 (00:05)
Time = 00:05)
hour = 00
minute = 05
cfs264sp2541 pts/0 98.61.15.234 Mon Jan 13 14:24 - 17:36 (03:11)
Time = 03:11)
hour = 03
minute = 11
cfs264sp2541 pts/0 98.61.15.234 Mon Jan 13 13:25 - 14:24 (00:58)
Time = 00:58)
hour = 00
minute = 58
$
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 6 of 6
Handout 10 - Bash Shell Commands and Perl Programming
_____________________________________________________________________________________
mynext=0
h=7
mynext=00
m=0
=============
mynext=05
m=5
=============
mynext=11
m=16
=============
mynext=58
m=74
=============
hours = 7
minutes = 74
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L