tech 154 – week 4

8
 TECH 154 UNIX AND THE INTERNET Maninder Singh [email protected]  J230 – Part- Time

Upload: manilion

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 1/8

 TECH 154 UNIX AND THEINTERNET

Maninder Singh

[email protected]

 J230 – Part-Time

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 2/8

 Topics

Exploring use of shell scripts

Practice basic job control

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 3/8

Exploring use of shell scripts

Shell scripts are text files that contain commands. If thescript is executable a process (a sub-shell) is automaticallycreated to deal with the script commands. The . (dot)command will suppress the creation of a subshell.

Specify the script interpreter: By default the defaultshell will be used to interpret the script but it is possiblespecify which program will be used to interpret the scriptand it is good practice to always do this. The first line mustcontain the name of the interpreter in this format:#!/bin/bash Note that this must be the first line in thescript and that first characters on the line are “#!”

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 4/8

Exploring use of shell scripts

Scripts can be used in a number of ways. Some of them are:

• shell name followed by the script name, e.g., bash script

• script name – This is the most common way and requires that the script file isexecutable, e.g., script

• the . (dot) command will suppress the creation of a subshell, eg . script 

ps Command

ps 

• displays your current processes.

ps -f  

• causes the more information to be displayed including PPID (Parent PID) andcommand arguments.

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 5/8

 Job Control  The shell associates a job number (e.g., [2]) and a process ID (PID) with

every pipeline. For example, when a job is started in the background theshell displays the job number and the PID associated with the job: e.g., [2]27927 (job =2, PID=27927)

Use the % character to refer to job number, e.g., %2.

&: (background) The & starts a command (pipeline) in the background.Processes running in the background are not able to use the terminal. Eg,ls –R * | grep bash > bashfile &.

bg: (background) Move a job in the background, eg, bg %2 restart asuspended job in the background

fg: (foreground) Move a job into the foreground, e.g. fg %2.

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 6/8

 Job Control  jobs: list jobs

kill: send signals to jobs or processes. By default it sends the TERM signal.It is possible to specify a job or a process, e.g, kill %2, kill 27927 or kill –skill 27927 to send the KILL signal to the process.

ps: display processes associated with the current terminal. ps x will display all your processes including ones running on other

terminals.

stty: The terminal used key combinations to send signals to the programthat currently has control of the terminal (running in the foreground.) The

stty -a command displays information about the terminal settings. control-z: Suspend(don't kill) the foreground job, and then return to the

shell

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 7/8

 Job Control Foreground

By default a command or script runs in the foreground and has access to theterminal of the user. Once run the user can suspend the command or script byholding the CTRL key on the keyboard and pressing z key.

Once the script is suspended it can be brought to the foreground again to berun using “fg” or it can continue to run in the background by using the “bg”command.

 You can run a command or script in background right away by adding “&” afterthe command name.

$sleep 10 &

Once in the background it does not have access to the users terminal.

7/27/2019 TECH 154 – Week 4

http://slidepdf.com/reader/full/tech-154-week-4 8/8

 Job Control