Top Banner
Now, return to the Unix • Unix shells: Subshells--- Variable---1. Local 2. Environmental
32

Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

Now, return to the Unix

• Unix shells:

Subshells---

Variable---1. Local 2. Environmental

Page 2: 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

Page 3: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Page 4: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 5: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 6: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.***

Page 7: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

MAILCHECK VARIABLE

• echo $MAILCHECK

• MAILCHECK=600 (every 10 minutes)

Page 8: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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)

Page 9: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 10: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

PS1---Not IBM System 1

• echo $PS1

• echo $PS2

• PS1=HERE:

• PS2=“To continue, hit ENTER key:”

Page 11: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Page 12: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

Grouping the Commands: ( )

• $( ls –C; date; pwd ) > outfile enter

• $cat outfile enter

$( scriptName; date) > outfile2

Enter

EnterEnter

Enter

Page 13: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 14: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Enter

Enter

Page 15: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

Terminating (kill) a process

• kill PID

• To be able to see the PID, type ps

Enter

Enter

Page 16: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Page 17: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 18: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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”

Page 19: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

Example• $ nohup (sleep 120; echo

“job done”) &

• ** This command may work differently on different systems.

Page 20: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Enter

Page 21: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 22: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Page 23: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 24: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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:

Page 25: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

• Process number

• Process status(ready/waiting)

• Event number that the process is waiting for

• System data area address

Page 26: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Page 27: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Page 28: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Page 29: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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.

Page 30: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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

Page 31: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

STARTUP FILES

• System profile is stored in /etc/profile

• cat /etc/profile (Home dir)Enter

Will display the content of profile1. 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.

Page 32: Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.

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