Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Published byModified over 5 years ago
Presentation on theme: "Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental."— Presentation transcript:
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental
Brief Reviewing of Shells Function of a shell: 1. Command Execution 2. Filename Substitution 3. I/O Redirection 4. Pipes 5. Environment Control 6. Background Processing 7. Shell Script
Shell Vatiables shell acts as a command interpreter. In order for shell to serve all your requests (executing commands, manipulating files,etc.), It needs to have certain information and to keep track of that information (such as: your home dir, terminal type, and prompt sign). This information is stored in shell variables. 1. Local variables---user defined 2. Environmental variables—standard variables
Displaying and Removing Variables: set and unset commands set to displaying the variables (both local and environmental) unset to remove a variable (you have to give a specific name for the variable) Rules have to be followed: 1. A shell variable name must begin with a letter and not a digit. 2. There are no spaces on either side of the equal sign.
Understanding the Standard Variables 1. They are mainly set by the system administrator. 2. You can change them just like you change the local variables. However, the changes are temporary and apply to only the current session. The next time you log in,... 3. If you want the changes to be permanent, place them in a file called.profile.
IFS ---Internal Field Separator Example: ls –l is a familiar command. What will happen if you forgot the space? What will happen if we do the following: echo $IFS save=$IFS IFS=“@” will change the invisible space to @ sign. *** This may not work on all Unix (Linux) Systems.***
PATH Variable The PATH variable is set to the directory names that the shell searches for the location of commands (programs) in directory structure. Example: PATH=:/bin:/usr/bin 1. Separate dirs with : 2. When the first char in the PATH is :, the shell interprets it as.: (dot, colon)
Something Special about bin UNIX usually stores the executable files in the bin directory You can modify it such as : PATH=:/bin:/usr/bin:$HOME/mybin will add mybin (suppose you have stored all your exe files in mybin under HOME) It to your PATH variable.
PS1---Not IBM System 1 echo $PS1 echo $PS2 PS1=HERE: PS2=“To continue, hit ENTER key:”
More Metacharacters 1. Executing the commands: Using Single Back Quotation Marks `command` $echo The date and time is : `date’ $echo “List of filenames in your current directory:\n” `ls –C` > LIST 2. Sequencing the commands: semicolon ; $date; pwd; ls –C pwd—display the absolute pathname of your working directory. Will run one after another from left-to-right
Grouping the Commands: ( ) $( ls –C; date; pwd ) > outfile enter $cat outfile enter $( scriptName; date) > outfile2 Enter
Background Processing: “ & “ 1. Type a command followed by an “&” will put that command into the background processing. The purpose? Fully utilize multi-tasking For some program take long time to finish, one don’t have to wait. One can type and run another immediately.
Examples 1. $sort data > sorted & 1348 ------ Process ID is displayed $ date i.e., one can type and run another command without waiting. You can specify more than one background command on a single command line. Date & pwd & ls –C & Enter
Terminating (kill) a process kill PID To be able to see the PID, type ps Enter
Chaining the Commands: “ | “ “ | “ is called pipe metacharacter. Format: command A | command B Example: $echo “Number of the logged in users:” `who | wc –l` > outfileName Enter
Options for ps: -a and -f -a --- Display the status of all the active processes, not just the user’s. -f --- Display a full list of information, including the full command line.
Keep On running: nohup When you log out, your background process will be terminated as well. 1. nohup can prevent this to happen. 2. The process, however, will not have any thing to do with any terminal. 3. When job (background process) is done, the redirected output (if any) will be saved in a file called “nohup.out”
Example $ nohup (sleep 120; echo “job done”) & ** This command may work differently on different systems.
Splitting the Output: the “tee” Aim: look at the output on the screen and save to a file at the same time. Example: $ ls –C | tee dir.list $ cat dir.list Enter
Options for “tee” -a -i tee –a ---Appends output to a file without overwriting an existing file. tee –i --- Ignores interrupts; does not respond to the interrupt signal.
File Searching: grep command grep (Global Regular Expression Print) grep is used to search for a specified pattern in a file or list of files. The pattern used by grep is called regular expression. Example: grep “bubble” bubble.c In cs315 directory Enter
grep Options -c --- Display only count of the matching lines in each file that contain the match. -i --- Ignores the distinction between lowercase and uppercase letters in the search pattern. -l --- Display the names of the files with one or more matching lines, not the lines themselves. -n --- Display a line number before each line. -v --- Display only those lines that do not match the pattern.
Unix Process Management Review: 1. Process---the execution of a program is called a process: you called it a program, but when your program is loaded into the memory for execution Unix calls it a process. 2. Unix maintains a process table for each process in the system and the table contains:
Process number Process status(ready/waiting) Event number that the process is waiting for System data area address
Sorting Text Files: The sort command Let look at an example first: We have a file named junk and it has the following contents: This is line one This is line two this is a line starting with a space character 4: this is a line starting with a number 11: this is another line starting with a number End of junk
From this example we can see: 1. ASCII values for nonalphanumeric characters (space, dash, backslash, etc.) are less than those for alphanumeric characters. 2. Uppercase letters are sorted before lowercase letters. 3. Numbers are sorted by first digit.
Options for sort command -b Ignore leading blank -d Use the dictionary order for sorting, ignore punctuation and control characters. -f ignore the distinction between lowercase and uppercase letters. -n Numbers are sorted by their arithmetic values. -o Store the output in the specified file. -r Reverse the order of the sort, from ascending to descending order. Examples: plist & plist2 in cs315 dir
STARTUP FILES System profile is stored in /etc/profile cat /etc/profile (Home dir) Enter Will display the content of profile 1.Usually, the system profile file is complex and incorporates some administration commands and requires some programming. 2. You can look at your profile file by the above example. Normally, it is a read-only file.
Process Table 1. Process number. 2. Process status (ready/waiting) 3. Event number that the process is waiting for 4. System data area address fork, parent and child in shells