



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: INTRO TO SYSTEMS SOFTWARE; Subject: Computer Science; University: University of Pittsburgh; Term: Spring 2007;
Typology: Lab Reports
1 / 7
This page cannot be seen from the preview
Don't miss anything!




TA : Ricardo Villamarín-Salomón
Part I Basic Shell Information Common commands used in UNIX: (please see additional handout) Determining the Shell you are using^1 : $ echo $SHELL These are some possible^2 answers: /bin/sh (^) Bourne Shell /bin/csh (^) C Shell /bin/ksh (^) Korn shell /bin/bash (^) Bash (“Bourne Again Shell”) /bin/tcsh (^) TENEX C shell Changing your shell for the current session The following command changes your shell to the Korn Shell: $ /bin/ksh Exiting from a shell / closing a session If you changed your shell using the example above and you want to return to your original shell, simply type exit: $ exit Changing your shell “permanently” $ chsh Valid login shells are: /bin/bash /bin/tcsh /bin/csh /bin/sh /bin/ksh Enter the new login shell for
computer. Common features of all shells Store values in variables Execute command files Accept arguments to command files Understand multiple commands typed in sequence Formulate pipes and filters Provide control structures for decision making, like other programming languages Store values in variables and referencing them Shells allow you to create variables, assign them values and use them in shell scripts. Bourne Shell (and compatibles) $
Semicolon (;) This is an example of using a semicolon to execute several commands in sequence: $ echo Hello > file1; ls file1; cat file1; rm file1; ls file file Hello file1: No such file or directory (the last command fails because rm file1 deleted the file file1) The && sign The && between two valid commands results in the next command being implemented only if the first command executed successfully For example, if we create a file called “temp_file”: $ cat >temp_file any content ^D And then we execute the following commands: $ cat temp_file > backup_file && echo temp_file && rm temp_file temp_file The first command copies the contents of temp_file to backup_file and then prints the text “temp_file”. Finally, it removes the file temp_file. If you attempt to execute the previous line again: $ cat temp_file > backup_file && echo temp_file && rm temp_file cat: cannot open temp_file You get an error. Since the first command failed (cat could not open temp_file because it was removed by the last command in the previous line), the second command (and any other command after it) is not executed (i.e., the text “temp_file” is not printed). The | | sign Use of || between two valid commands results in the next command being implemented only if the first command fails. For example, assuming that temp_file does not exist, and you try to execute this sequence: $ cat temp_file > backup_file || ls -l > temp_file cat: cannot open temp_file You receive an error because cat could not find temp_file. However, the second command did get executed and if you attempt the previous sequence again: $ cat temp_file > backup_file || ls -l > temp_file
You do not get any error. This is because in the previous execution of this sequence, after cat failed, the second command (ls -l > temp_file) was executed and temp_file was created, thus there was no error the second time you attempted to run cat temp_file > backup_file Formulating the pipeline ( | ) The pipeline is used when we want the output of a command to be fed into the input of another Example 1 : Display the files in the current working directory owned by you, using the command less (displays one screen at a time with scrolling). $ ls -l | grep "<pitt_usr>" | less (replace <pitt_usr> above with your actual Pitt’s username but leave the quotes) Example 2 : Display the files in the current working directory owned by you, but in sorted order. Lists the files using the command less $ ls -l | tr -s ' ' | grep "<pitt_usr>" | cut -d' ' -f 9 | sort -f | less Provide control structures for decision making, like other programming languages These are the common control structures: Construct Description / Features The if - else construct if command1 executes then execute command else execute command fi If command1 succeeds, then the commands between then and else are executed. If not, the commands between else and fi execute. Example: if date | grep "Fri" then echo "Today is friday!" fi
Construct Description / Features value2 is substituted into var the second time commands between do and done are executed for value1, value2 and so on. Example: for fruit in apples oranges pears bananas do echo "$fruit" done echo "Task complete." References