CT320 Bash Lab I                
Group Project                
You may work in pairs, if space demands.                 
Introduction to Shell Scripting                
The purpose of this assignment is to get you started writing Linux
scripts using bash. You will create and edit a bash script in class
that uses many of the features of shell scripts described in the
lecture.
                
Part 1                
Make sure you can login to the Linux system using the ct320 user and the password given to you by the instructor or teaching assistant. Make sure that you understand the system well enough to run a browser, file manager, terminal and text editor.                 
Part 2                
Practice redirection and pipes using find and grep, as shown by
the following sequence:
$ cd /bin
$ find . -print | grep dir
$ find . -print | grep ch
Advanced students may realize that these pipelines can be reduced
to single find statements.
                
Practice arranging output in order by name or size or date using ls
and sort commands, as shown by the following sequence:
$ ls -l | sort -k 9
$ ls -l | sort -k 5 -n
Of course, there are probably ls options to accomplish the same
ends.
                
Practice splitting an output stream to the terminal and a file using
echo and tee, as shown by the following sequence:
$ cd
$ echo "Brush your teeth after every meal" | tee string.txt
$ cat string.txt
Practice operating on all of the files in a directory using ls and
chmod, in conjunction with xargs, as shown by the following
sequence:
$ mkdir temp
$ cd temp
$ echo "File 1" >file1.txt
$ echo "File 2" >file2.txt
$ echo "File 3" >file3.txt
$ echo "File 4" >file4.txt
$ ls -l
$ ls | xargs chmod a=rw
$ ls -l
Part 3                
Write a script that does all of the following:
- Script should be stored in a file called
bscript. - Has the appropriate initial line that identifies the file as a bash script.
- Has a comment with your personal information, as follows:
# Students: name1, name2
# Logins: login1, login2
# Assignment: CT320 BashI
# Date: YYYY-MM-DD
- Shows the date and time.
- Reads the first command line parameter into a variable called
directory. - Checks that the directory exists.
- If the directory doesn’t exist, displays “Nonexistent Directory:” $directory and stops.
- Displays the text: “Script is processing directory:” $directory.
- Prompts the user with “Action (unprotect, list, delete, archive): ”.
- Reads the user input into a variable called action.
- Checks that $action has one of the listed actions.
- If the action is undefined, displays “Invalid Action: ” $action and exits.
- Echoes the text: “Action requested is: ” $action.
- If the action is
unprotect, sets protections usingchmod a=rwxfor all contents. - If the action is
list, displays a long directory listing. - If the action is
delete, deletes all files and dirs recursively. - If the action is
archive, copies all files and directories to/tmp/archive. - Echoes the text: “Successful completion!”.
In addition to the previous requirements, the script must:                 
- The unprotect action does not need to operate recursively.
- A loop must be used to perform the unprotect action.
- The list action should sort the files by file name, in Z–A order.
- The delete action should work without prompting the user.
- The archive action should operate on the directory contents recursively.
- The archive action must create
/tmp/archiveif it does not exist. - All output should be echoed to the file
bscript.log, creating it anew for each run. - To perform all actions, the script must change to the specified directory.
- The script must change back to the directory from which it was run before exiting.
Part 4                
You might want to make a backup copy of the Test directory before
testing any commands in case your script intentionally or accidentally
deletes files. Now test your script as follows:
                
- Create a directory in the ct320 home directory called
Test. - Create 5 files with names
file1.txtthroughfile5.txtinTest. - Make each file contain its name as its contents.
- Create a subdirectory in
TestcalledSubtest. - Create 2 files with names
Subfile1.txtandSubfile2.txtinSubtest. - Examine the default protections of all dirs and files in
Test. - Run the script with action equal to
unprotect. - Make sure all protections have changed to 777 (
-rwxrwxrwx). - Run the script with action equal to
list, and examine the resulting listing. - Run the script with action equal to
archive, and check/tmp/archive. - Run the script with action equal to
deleteand make sureTestis empty.
Part 5                
When you have tested your script, and it works, show your work to the TA.