CSCI 344
Beginning Bash Scripts
Lab 3
Write the following bash scripts. You may use the Internet as a
reference for bash script syntax and as a reference for UNIX/Linux
commands (such as grep, awk, and sed), but you may not search for
solutions to these problems--that would completely defeat the purpose
of doing these exercises.
When you complete one of the scripts, notify me and I will come watch a demo and give you credit for completing it.
Here is a link to the O'Reilly's Learning Bash. Note: this link only works from computers in the csuchico.edu domain.
As an example of a for loop in a bash script, the following script prints out all command line arguments, one on each line:
for cur in $@; do
echo $cur
done
(1) Write a bash script called oddeven that accepts a filename as a
command line argument and splits the lines in the file into two
different output files (also command line arguments). The odd numbered
lines (e.g. 1, 3, 5, ...) should be written to a file and the even
numbered lines (e.g. 2, 4, 6, ...) should be written to a file.
$ oddeven my_file my_file_odd my_file_even
(2) Write a bash script to display the current month, the day and the year, in the following format:
$ date
Day of week: Fri
Month: Feb
Day of month: 9
(3) Write a bash script that takes an input file with the form
"last,first" and creates two files, one with last name, one with first
names:
$ split input_filename last_filename first_filename
(4) Write a bash script to kill all of the current user's running processes that match the given string. For example:
$ kill_my sleep
would kill all the current user's processes running sleep.
(5) Write a bash script called rename that takes a set of files as arguments and
renames all files that have spaces in their name so that each space is replaced with a dot
(‘.’). Any file
that does not contain spaces in its name should be unchanged. Hint: In this script you need
to set the internal field separator (IFS) so it does not contain a space. If it contains a
space, it will consider the fileame "one two" as two separate files "one" "two." The
syntax is IFS=$'\x0A'$'\x0D'
"one two three" would become one.two.three
(6) Write a bash script that swaps two files
$ swap a.cpp b.cpp
a.cpp will be renamed b.cpp and b.cpp will be renamed a.cpp w/o deleting the contents of either