23
Agenda The Bourne Shell – Part I Redirection ( >, >>, < , <<) Pipes Controlling Processes Creating Subshells, ps, ps -l, $!, $$ Managing Processes (Jobs) Command Grouping, Running jobs in background (bg), bringing jobs to foreground (fg), job status (jobs), Suspending jobs (CTRL Z), Restarting jobs in background, Displaying/Killing processes or jobs

Agenda The Bourne Shell – Part I Redirection ( >, >>,

Embed Size (px)

Citation preview

Page 1: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Agenda The Bourne Shell – Part I Redirection ( >, >>, < , <<) Pipes Controlling Processes

Creating Subshells, ps, ps -l, $!, $$ Managing Processes (Jobs)

Command Grouping, Running jobs in background (bg), bringing jobs to foreground (fg), job status (jobs), Suspending jobs (CTRL Z), Restarting jobs in background, Displaying/Killing processes or jobs

Page 2: Agenda The Bourne Shell – Part I Redirection ( >, >>,

The Bourne Shell The Bourne Shell was the first shell

developed for the UNIX operating system. Since many other “new and improved” shells incorporate the Bourne shell, we will be learning about this shell first.

When you login to PHOBOS, you can access the Bourne shell by issuing the command: sh (although you can use Bourne shell commands in the default shell for PHOBOS – which is the Korn Shell)

There is no Bourne Shell in Linux…

Page 3: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Standard Input, Standard Output

and Standard Error Three terms are used to indicate the

direction of information when issuing UNIX / Linux commands:

Standard Input (stdin) – The default is the keyboard

Standard Output (stdout) – The default is the screen

Standard Error (stderr) – The default is the screen

Page 4: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Redirection Since everything in UNIX / Linux

operating system is a file, standard input, standard output or the standard error can be directed from/to different files:

1>, > Redirects standard output to a file. The 1> indicates standard output is redirected, but “>” is considered “1>” by default. This will overwrite previous contents of a file unless you use “>>”

Page 5: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Redirection 2> Redirects the standard error to

a file. This can be used to view error messages in a file for later reference. If you want to “throw-away” error messages, redirect to /dev/null commonly referred to as the “bit-bucket” or “trash can”Example:

cat a b c 2> /dev/null

Page 6: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Redirection < Command takes input for a

command from a file. Example:

grep pattern < file 1

Can You explain the following redirections?

cat < file1 > file2 grep msaul < file3 > file1 2> file7 grep dward < file4 > file 5 2>/dev/null

Page 7: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Pipes ( | ) Pipes are used to send the standard

output of one command into another command as the standard input

Pipes have the advantage of modifying standard output to achieve a task (possibly without having to create temporary files which need to be removed later)

Page 8: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Controlling Processes A process in UNIX simply represents the

running of a command The process structure in UNIX is similar to

the hierarchical structure of directories: The beginning process is root Parent processes can create child processes Processes can spawn other processes When a child process is finished, execution

returns to its parent process

Page 9: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Controlling Processes It is important to learn how UNIX

processes work, since administering a UNIX system requires knowledge of how to start, stop and monitor processes.

The UNIX system is built on a series of processes and sub-processes. If you don’t understand processes, you could accidentally shut down the UNIX system.(not a very smart or popular thing to do!)

Page 10: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Processes For your account, the basic process is the

shell (although for the entire system - from root downwards there are many processes).

When you type in a UNIX command and press ENTER, a child process is created to allow execution of command. During this time the parent process “sleeps” and resumes when child process is completed. There is even a command called sleep to suspend the process for a specific number of seconds!

Page 11: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Process Identification (PID) Process Identification Numbers (PIDs)

are used to keep track of separate processes

To view a compact listing of process ID numbers, you can enter ps

To view a detailed listing of process ID numbers (relating child (PID)& parent processes(PPID)) enter ps -l

To view listing of processes for the entire system, enter ps -ef

Page 12: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Read-only Variables for PIDs

The shell can store the PID number as a read-only shell variable

echo $$ displays the PID number that is currently executing

echo $! displays the PID of the last process that was run in the background

The next slide shows a practical example of using these read-only variables

Page 13: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Creating Filenames using $$

The PID number can be used to create unique filenames (that need to be different everytime they are run)

To create temporary files within a script: cp file_name file_name.$$

This will create a temporary file with the PID as the extension. This is useful when removing all temp files (eg rm *.$$)

Page 14: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Managing Processes (Jobs) Command Grouping Running jobs in background (bg) brings jobs to foreground (fg) Displaying job status (jobs

command) Suspending jobs (CTRL Z) Killing processes or jobs (kill)

Page 15: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Command Grouping You can run a set of commands on

the same command line by typing such commands as: ls ; who ; date <ENTER>

You can also, combine commands or files to be run in both the foreground or background (eg ls ; who or a & b ; c)

Page 16: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Command Grouping The UNIX OS has the ability to run

programs in the foreground and in the background. This feature allows users of the UNIX system to run lengthy processes without interfering with their other tasks.

To run a command or script in the background, type the symbol & after the command or filename (eg. who &). Note that spaces are allowed...

Page 17: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Command Grouping It is important to note that although these

commands appear to be in a sequence, the shell may execute these in a different order may schedule jobs in a order

For example, you may want to run both commands a & b in the background, and run command c in the foreground. Here is the command:

(a;b)&c

Page 18: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Bringing processesto foreground

When processes are run in the foreground, the shell will wait for the command to finish before allowing the user to enter another command (thus background processes are useful)

If a process or processes are in the background (or suspended), you can bring it to the foreground by typing fg

Page 19: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Displaying job status (jobs command)

You can display the status of jobs that are running in the background by typing the UNIX command jobs

jobs commands give a listing of jobs running in the background:

jobs with a plus sign “+” indicates default job to bring to foreground by entering fg

Can bring numbered job to foreground by entering fg %(job#) - Note space between fg & %

Page 20: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Suspending jobs (CTRL Z) You can suspend or stop the execution of

processes while they are running in the “foreground” by pressing <CTRL><Z>

When you press these keys, the process is suspended and placed into the background in order to free up the shell for other operations.

You can resume process by bringing it to the foreground or have it run in background

Page 21: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Restarting Suspended Processes

in the Background A process may take such a long time that

it is better to suspend the job (i.e. send it to the background, and then have it run in the background). This will leave you to operate in the foreground

If a process or processes are suspended in the background, you can have it continue running in the background by entering bg (rules such as bg %(job#) also apply)

Page 22: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Terminating processes or jobs (kill)

Depending on your permissions, you may be able to abort or “kill” a process.

Only root is allowed to kill other user’s processes, but as a user, you can processes that you have created.

Killing a process is also useful to halt processes that are running in the background since <CTRL><C> or DELETE keys in foreground won’t work

Page 23: Agenda The Bourne Shell – Part I Redirection ( >, >>,

Procedure to “Kill” a Process

Procedure: We are assuming that you are killing

one of your own process (you are logged in…)

View process by typing ps -l Carefully locate PID to kill Type kill %(job#) to kill that job

number (can also use kill PID or kill -9 PID to kill process. The kill command by itself kills more recent stopped job