CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2 Prof. Michael J. Reale Fall 2014
COMMAND KATA 7: VARIABLES
Command Kata 7: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata7 and go to it mkdir dkata7; cd dkata7 Make a script called kata7 script kata7 When we re done: exit
Command Kata 7: The Way of Bourne Start a Bash shell bash Create a shell variable BESTMOVIE= Supremacy Print value of variable echo ${BESTMOVIE} Start a new bash shell bash Print value of variable echo ${BESTMOVIE} Exit shell exit Make variable an environment variable export BESTMOVIE Start a new bash shell bash Print value of variable echo ${BESTMOVIE} Exit shell exit Exit shell again exit
Command Kata 7: The Way of the C (Shell) Start tcsh tcsh Create shell variable set ocean= Atlantic Print value of variable echo ${ocean} Create environment variable setenv OCEAN Pacific Start tcsh shell tcsh Print value of shell variable echo ${ocean} Print value of environment variable echo ${OCEAN} Exit shell exit Exit shell again exit
COMMAND KATA 8: REDIRECTION
Command Kata 8: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata8 and go to it mkdir dkata8; cd dkata8 Make a script called kata8 script kata8 When we re done: exit
Command Kata 8 Run cat cat Type some data and hit enter Press CTRL+D (EOF) Redirect cat output to myfile cat > myfile Type some multi-line data and press CTRL+D (EOF) Print contents of myfile cat < myfile Redirect myfile to STDIN and STDOUT to yourfile cat < myfile > yourfile Print contents of yourfile cat < yourfile
COMMAND KATA 9: MORNING TEE
Command Kata 9: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata9 and go to it mkdir dkata9; cd dkata9 Make a script called kata9 script kata9 When we re done: exit
Command Kata 9 Print list of users logged in who Pipe output of who to input of sort who sort Grab list of logged-in users before sorting who tee listwho sort Grab list of sorted logged-in users who sort tee listsort Print list of logged-in users cat listwho Print sorted list of logged-in users cat listsort
COMMAND KATA 10: CROSSING THE STREAMS
Command Kata 10: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata10 and go to it mkdir dkata10; cd dkata10 Make a script called kata10 script kata10 When we re done: exit
Command Kata 10: Bourne Start Bash bash Try to run make (should fail) make Redirect STDOUT make > outfile Redirect STDERR make 2> errfile Redirect both STDOUT and STDERR make > bothfile 2>&1 Print outfile cat outfile Print errfile cat errfile Print bothfile cat bothfile Remove files rm *file Exit Bash exit
Command Kata 10: C-Shell Start tcsh tcsh Try to run make (should fail) make Redirect both STDOUT and STDERR make >& bothfile Redirect STDOUT to outfile (subshell), then STDERR to errfile (make > outfile) >& errfile Print outfile cat outfile Print errfile cat errfile Print bothfile cat bothfile Remove files rm *file Exit tcsh exit
COMMAND KATA 11: #YESFILTER
Command Kata 11: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata11 and go to it mkdir dkata11; cd dkata11 Make a script called kata11 script kata11 When we re done: exit
Command Kata 11: Part 1 Open vi to create the file file1 vi file1 Hit i, and enter the following: ghi abc abc def def ESC, then SHIFT+ZZ to save and exit
Command Kata 11: Part 2 Print file1 cat file1 Get first 2 lines of file1 cat file1 head n2 Get last 2 lines of file1 cat file1 tail n2 Find abc in file1 cat file1 grep abc Get first column of file1 cat file1 cut d f 1 Get number of lines in file1 cat file1 wc l Get unique values and counts cat file1 uniq c Sort file1 cat file1 sort Count how many lines have abc cat file1 grep abc wc -l
COMMAND KATA 12: Time Travelling under an Assumed Name
Command Kata 12: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata12 and go to it mkdir dkata12; cd dkata12 Make a script called kata12 script kata12 When we re done: exit
Command Kata 12 Start a bash shell bash Create an alias to repeat the last command alias r= fc -s Create some files touch a1 a2 a3 Re-execute command, but with a replaced by b fc s a=b Re-execute command, but with b replaced by c r b=c See what files you have ls Remove all files beginning with a through c rm [a-c]* Check your history history Check your alias alias r Get rid of your alias unalias r Check it again alias r Exit bash exit
COMMAND KATA 13: Find and Execute
Command Kata 13: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata13 and go to it mkdir dkata13; cd dkata13 Make a script called kata13 script kata13 When we re done: exit
Command Kata 13 Find all regular files in your home find ~ -type f Find all directories in your home find ~ -type d Find all files beginning with kata find ~ -type f name kata* Print out contents of all kata files find ~ -type f name kata* exec cat {} \; Do same thing with xargs find ~ -type f name kata* xargs cat List long listing for all directories find ~ -type d exec ls ld {} \; Do same thing with xargs find ~ -type d xargs ls -ld
COMMAND KATA 14: UGO; I Stay
Command Kata 14: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata14 and go to it mkdir dkata14; cd dkata14 Make a script called kata14 script kata14 When we re done: exit
Command Kata 14 Make a file touch myfile Give ALL permissions chmod a=rwx myfile List long format ls l Remove read, write, execute from group and others chmod go-rwx myfile List long format ls l Add read and execute to group chmod g+rx myfile List long format ls l Set user permissions to equal group chmod u=g myfile List long format ls l Add write permissions to user AND add execute to others chmod u+w,o+x myfile List long format ls l
COMMAND KATA 15: Expressing Yourself Regularly
Command Kata 15: Preparation First, go to ~/cs307 cd ~/cs307 Make directory dkata15 and go to it mkdir dkata15; cd dkata15 Make a script called kata15 script kata15 When we re done: exit
Command Kata 15 Copy dictionary file cp /usr/share/dict/words words Find all lines that end in e egrep e$ words Find all lines that begin with x egrep ^x words Find all lines that begin with x or y and end with e egrep ^[xy].*e$ words Find all lines that repeat a twice egrep a{2} words Find all lines that repeat e or s three times egrep (e{3} s{3}) words Find all lines that end in thing POSSIBLY followed by s egrep things?$ words Find all lines that end in one or more z egrep z+$ words