Assign 0: UNIX and Bash
Table of Contents:
Goals for Assignment 0
After the assignment, you should
- Know how to learn about new commands and use them
- Know how to make environment variables and customize your environment.
- Know how to use some
chmod, file system, and process commands. - Know how to redirect output
- Have a better understanding of UNIX permissions
- Understand pipes and how to use them effectively
- Know how to write basic bash scripts, including
using
ifstatements andforloops.
Overview
You're going to practice your Unix commands and set up your environment, and then show that you did them in a bash script that you'll execute and save in an output file.
This is due before class on February 4.
Objective: Environment Variables
- Create two new environment variables
called
CS397, which represents the path to the course directory, andHANDOUTSthat is the path to the course'shandoutsdirectory.- Open
~/.bashrcusing emacs or jedit or your favorite text editorRecall that hidden files (those that start with .) are not shown by default when you list files in your directory. Use the
-aoption. In text editors, you may need to figure out how to show hidden files. - At the bottom of the file, create a new variable:
CS397=/csci/courses/cs397 - Export the variable:
export CS397 - In terminal, run the source command to load your new profile
source ~/.bashrc - Check that your new variable was created by displaying its value.
- Use the variable by going to that directory, i.e.,
cd $CS397 - Follow a similar process for the
HANDOUTSvariable. Note, since you have the variableCS397, you may want to use that in defining yourHANDOUTSvariable's value.
- Open
- In a new terminal (so that it loads the updated .bashrc file),
- Display the
$HANDOUTSvariable - Display the basename of
$HANDOUTS - Display the directory name of
$HANDOUTS
- Display the
Objective: Learning new Unix commands
Use the man command to learn about
the yes and touch command. You'll need to
"prove" that you learned these commands later in this assignment.
Objective: Customizing Your Prompt
- Open up your
.bashrcfile in your favorite text editor. - Search for
force_color_prompt - Uncomment the line that says
fore_color_prompt=yes - Search for the definition of
PS1. - Comment out (
#) thePS1definition. (Which branch of the if should you be in?) - Add the following code below the commented out prompt.
PS1="\e[1;31m\h:\W \u (\!)\$ \e[00m" - Source the .bashrc file. What does this prompt look like?
- Run
psand some other commands to better buess what the prompt is doing. - Then try setting it as
PS1='\e[00;35m\]\! \e[00;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ ' - Try out more by checking out the wiki page on customizing your prompt.
- You can revert your prompt to the original, commented out prompt or to whatever you want to use for your prompt.
Objective: Aliases
- Open (or create)
~/.bash_aliasesfile. - Create an alias so that ssh aliases to
ssh -XY. Then, whenever you ssh, you'll automatically have the -XY options. - Reload your configuration file by running source ~/.bashrc Test that your new alias works. (I'll be able to see that you did this correctly from the next objective.)
- Create an alias called "peptalk" that is aliased to repeatedly print "You can do it!"
Objective: Unix Commands (100 pts)
If you haven't already, create a cs397
directory in your home directory and an assignments subdirectory.
Create an assign0 subdirectory
within assignments.
In conclusion, you should have the
directory $HOME/cs397/assignments/assign0
Do the following steps within ~/cs397/assignments/assign0 directory.
You're going to create a script
called assign0.sh in your assign0 directory that
demonstrates the following tasks. BUT FIRST, practice them on the
command line, then put them into the script, as you go, and make sure
the output from the script is what you expect. You will be practicing using both
absolute and relative paths in this assignment. Use whichever is easier/simpler (i.e., favor relative paths).
- Show all your aliases in your
.bash_aliasesfile. - Display the
$HANDOUTSvariable - Display the basename of
$HANDOUTS - Display the directory name of
$HANDOUTS - List the contents
of
/csci/courses/cs397/handouts/assign0Note that you created environment variables that could be helpful for this task. - Copy the
directory
/csci/courses/cs397/handouts/assign0and all of its contents into this directory. So, you should have a directory that is~/cs397/assignments/assign0/assign0 - List the contents of the current directory and then
your
assign0subdirectory. - Go into the
assign0directory - Execute
example.shby executing bash example.sh - Move
lastnames.txtinto the parent directory. - Create a file within your directory, named
mine.txt, using thetouchcommand. - View the contents of your directory.
- View the files' permissions.
- Try running the script as ./example.sh
- Change the permissions of
example.shsuch that it is executable by you (and only you). - Now, execute
example.shby executing ./example.sh - Create a backup copy of
firstnames.txt, named with the extension.bkup. (A shortcut would be useful here.) - Run
psto see the processes running in this terminal. - Display (only) the first 10 results from
ps -efNote the differences in the displayed output - Run the above command again and save its output in a file called
firstps.out(This should be implemented as a command.) - Get the last 10 processes and save them in a file called
lastps.out - Display the contents of
lastps.out - Display how much space all the files in the current directory take up, in total. (I want as little output as needed to answer this.)
- The script should be called
submit397.sh - The script should take a command-line argument that is the name of the assignment.
- The script should copy your assignment directory (likely located
in ~/cs397/assignments/assignmentname)
to your
turnindirectory (/csci/courses/cs397/turnin/yourusername), still namedassignmentname. For example, for this assignment, the directory isassign0. In the end, all of your submitted assignment files in/csci/courses/cs397/turnin/yourusername/assign0 - The script should use your environment variable
$CS397 - If you previously submitted the assignment into
the
turnindirectory, the script should move the existing submission into a backup copy in your turnin directory, with the date and time in its name to keep track of versions of the submission. Then, copy the current version of the assignment into theturnindirectory. - Upon submission (read: copy), show the contents of the directory
that is now in the
turnindirectory to show that the submission script worked. It is helpful to have some output before this that states what is happening. (Yes, yes, I know that's not following the Unix philosophy.) - The script should handle errors and display appropriate and descriptive/helpful messages
to the user, including:
- incorrect number of arguments
- the assignment directory to copy doesn't exist
- See breakdown above.
Creating Your Script
Put the commands above into a script called assign0.sh
in the parent assign0 directory. Make sure you have the
shebang and high-level comments describing what your script does. Note
that some commands that work in the shell may not behave the same way in
the script. Check that you're getting the expected results from your commands.
Carefully delete your assign0 subdirectory
and all its content.
Then, execute your assign0.sh script (which should
create the assign0 subdirectory again) and save its output in a file
called assign0.out. (Like all the files you're submitting,
this output file should be in your assign0 directory.
A Script to Execute Scripts (30 pts)
Copy the (whole)
directory /csci/courses/cs397/handouts/bash/
into your ~/cs397/assignments/assign0 directory.
Write a script called execAll.sh that
uses a for loop to get all the files that end
in .sh from your bash
directory, displays the name of each file, and then executes it.
Save the output from your script in a file called execAll.out
Script to Submit Your Assignment (30)
You need to submit everything from the assignment directory into your turnin directory.
Use your development skills to build the script in pieces to make sure it's working correctly.
Script Requirements:
Clean up (read: carefully remove) any extra copies of directories in your turnin directory that you created during testing.
Demonstrate that your script worked by running: bash submit397.sh assign0 > assign0_sub.out
Finishing up: What to turn in for this assignment
Run your submit397.sh script one more time--without the output redirection--so that
your assign0_sub.out file will be in your final submission in your
turnin directory. (There should be a backup copy of the directory from
when you ran the script above.)