100%(1)1 out of 1 people found this document helpful
This preview shows page 2 - 4 out of 6 pages.
character in the first line.Save the above in a file such listing. Then run the shell script by typing the command bash listing.Shell VariablesLike any programming language, a shell supports variables. One of them is the PATH variable. PATH variable consists of the search paths the shell uses to locate commands. You can see your current search paths by typing the command echo $PATH. Try it.In last week's exercises, you run programs, such as a.out, with a relative path such as ./a.out. If you run the program with the program name, the shell would complain: Command not found. This is because the directory containing a.out was not in the search paths. To add your current directory to the search paths, you can issue the following command:% PATH=$PATH:.Note in the above, character "%" denote your shell prompt.Try the above command at the shell prompt, then type the command echo $PATH again. You will find that the current directory "." is now in the search path. Now you should be able to run program a.out by entering only the program name a.out if it is in the current directory.Type the following command to see a list of variables and their values in your shell process:% declare | moreStandard I/O RedirectionsLast week, we mentioned standard input, standard output and standard error. To aUnix program, stdin, stdout and stderr are treated as three open files. Usually a shell associates stdin with the terminal keyboard, stdout and stderr with the terminal screen. However such associations can be changed (redirected) to actualfiles. For example, the program ls sends its output to standard output, ie the terminal screen. However if you redirect the standard output to a file, then output of ls would go to that file.In a shell, we use symbol "<" to redirect standard input, and ">" to redirect standard output, and "2>" to redirect standard error. The following example sends the output from program ls to file foo:% ls -l > fooType the above command, and then check the content of file foo with command cat foo.Our next example shows that the command sort reads lines from file foo and sort them in alphabetical order and displays the results in the standard output (terminal screen):% sort < fooArchive and Extract Files with tarWe often need to archive files and extract files from an archive. The Unix command for this task is tar. The command tar stands for Tape ARchive, since in the old days archives were stored on agnetic tapes. The command has numerous
options. Check its manual page with man tar.One commonly used option is to create an archive file:tar -cvf your_tar_filename.tar directory_nameto create a tar file your_tar_filename.tar from files in the directory directory_name. For example the following command:% tar -cvf ~/lab02.tar ~/lab02The above command would create tar file lab02.tar containing all files under directory lab02. The tar file is stored under your home directory.