Log in to pe15
- Run the ssh client
Use the values in the table below to connect to pe15.
ssh configuration parameter value protocol ssh ssh version 2 ssh port 22 host pe15.cs.umb.edu user name your Unix username Authentication method password
- Enter your password
A dialog box will appear on the screen into which you must type the password for your Unix account.
Directory for This Exercise
(When you are ready for me to check this exercise, please scroll back up to where you entered these commands!)
- Go to your ex directory
cd ~/it244/ex- Create an ex18 directory
mkdir ex18- Go to your ex18 directory
cd ex18Start Up a script Session
Execute the script command, using the --flush option
script --flushContinuing a Command on the Next Line
NOTE: Where you see the following notation:\[Enter]...that means to input the backslash character, \ , and then press the Enter key.
- Backslash with echo
echo Go \[Enter] Red \[Enter] Sox [Enter]The greater than symbol, > , that you see after all but the last time you hit Enter is a secondary prompt.
It tells you that the shell is awaiting more input.
This will not work unless you hit the Enter key immediately after typing the backslash.
- More practice with backslash
echo The University \[Enter] of Massachusetts \[Enter] at Boston [Enter]- Try typing a space before hitting Enter after backslash
echo Hello \[Space][Enter]The echo command does not wait for further input, it prints its argument and you don't get a secondary prompt.
The backslash is a quoting character that turns off the special meaning of whatever character comes next.
When you hit Enter immediately after the backslash, you are turning off the special meaning of the new line character, which tells the shell to execute a command.
If you put a space, or any other character immediately after the backslash, that is the character that the backslash applies to, not the new line character which the shell receives after you hit Enter.Changing Prompts
- Change your primary prompt
PS1='Prompt-> 'Note the single quotes.
You must use them
Since you have not added this to your .bash_profile, this will only be your prompt for this terminal session.
- Change your secondary prompt
PS2='>> '- Show your new secondary prompt
echo Go \[Enter] Red \[Enter] Sox [Enter]You should see your new secondary prompt with each new line.Semi-colon, ; , to Separate Commands
- Use three echo commands on the same command line
echo foo; echo bar; echo bletchEach echo command generates its own line of output.
- Use three more echo commands on the same line
echo Unix; echo is not; echo for the faint of heart- Use different commands on the same line
echo; echo This is what is in my home directory:; ls -al ~& to Separate Commands
- Copy hi_bye.sh from ~ckelly/course_files/it244_files
cp ~ckelly/course_files/it244_files/hi_bye.sh .- Display the contents of hi_bye.sh
cat hi_bye.sh- Run mulitple instances of hi_bye.sh with & as a separator, then run the jobs command
./hi_bye.sh & ./hi_bye.sh & jobsThe ampersand causes each of the two invocations of hi_bye.sh to run in the background.
This means the ampersand has two functions, to cause a command to run in the background and to separate one command from the next.
Notice that jobs is able to report on the two invocations of hi_bye.sh running in the background, because it is running in the foreground.
- Wait for completion of jobs
After the second Goodbye appears, hit Enter.
You should see a report that the two jobs have terminated.Important Keyword Shell Variables
- Display PATH
echo $PATH- Display HOME
echo $HOME- Display SHELL
echo $SHELL- Display PS2
echo $PS2- Display some of your global variables
envLocal Variables
- Create a local variable
greeting=Hello- Display the local variable you just created
echo $greeting- Assign a string with a space in it to this local variable
greeting='Hello there'- Display the new value of this variable
echo $greeting- Copy print_foo.sh from ~ckelly/course_files/it244_files
cp ~ckelly/course_files/it244_files/print_foo.sh .- Print this script to the screen
cat print_foo.shThis script simply prints the value of the variable foo to the screen.
- Create the local variable foo
foo=FOO- Print the value of foo
echo $foo- Run print_foo.sh
./print_foo.shThe script prints no value for foo, since foo was a local variable defined in your original shell.
The script runs in a subshell and cannot see the local variables of its parent shell.Global Variables
- Set foo to a new value and export it
export foo=BAR- Print foo in your current shell
echo $foo- Run print_foo.sh
./print_foo.shSince the export command makes foo a global variable, print_foo.sh can now see this variable in the subshell in which it is run.The Directory Stack
- Go to ~ckelly/course_files/it244_files/dir_tree
cd ~ckelly/course_files/it244_files/dir_tree- Look at your current location
pwd- Look at the contents of the directory
ls- Use dirs to look at the directory stack
dirsIt only lists the current directory.
- Go down one level using pushd
pushd projTwo directories are now listed.
- Display your location
pwd- Look at the contents of the directory
ls- Go down another level
pushd proj1Three directories are now listed.
- Display your location
pwd- Go up one level using popd
popdNow, only two directories are listed, because you poped the previous directory from the stack.
- Display your location
pwd- Go up one level, suppress output and get location, all on one line
popd > /dev/null; pwdBy redirecting output to /dev/null, you have stopped popd from printing out the directory stack.
By using a semi-colon followed by pwd, you have displayed your current location without having to wait for a prompt.Exit Your script Session
Use the exit command
exitYou should see a confirmation message confirming that the script session is finished, resulting in a file called typescript. I will look at the typescript file, along with the other files in your directory.Bash Script for This Exercise
- Go back to your ex18 directory
cd ~/it244/ex/ex18- Create the script ex18.sh
nano ex18.sh- Write into this file commands from certain of the sections above
Include Unix commands from the section Important Keyword Shell variables, to the section The Directory Stack.
- Save this file
Save and quit.
- Make this scipt executable
chmod 755 ex18.sh- Test the script
./ex18.shFix any errors you find.
If you have trouble, see me.