Introduction to UNIX. 2 Multitasking Foreground Current Task The Command Running at the `$` Prompt ...

30
Introduction to UNIX

Transcript of Introduction to UNIX. 2 Multitasking Foreground Current Task The Command Running at the `$` Prompt ...

Page 1: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

Introduction to UNIX

Page 2: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

2

Multitasking

Foreground Current Task The Command Running at the `$`

PromptBackground

Command Running Behind the Scenes Not Interactive (e.g. text editor)

Page 3: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

3

Multitasking

Background Tasks & Perform Command in the

Background$ who > whoson &[1] 10946 $

$ who > whoson &[1] 10946 $

Important Notes Logoff Terminates Background Tasks Do Not Overload The System Can Not Require Keyboard Input Screen Output May Not Be Desirable

Page 4: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

4

Multitasking

Background Tasks nohup UnixCommand

Prevents Command From Terminating on Logoff

$ nohup who &nohup: appending output to 'nohup.out'$

$ nohup who &nohup: appending output to 'nohup.out'$

$ nohup who > whoson &$

$ nohup who > whoson &$

No output file specified. Output redirected to nohup.out

Output redirected to whoson

Page 5: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

5

Multitasking

Every Command is Assigned a Unique Number

Process ID (PID)ps Display Currently Running Tasks $ who > whoson;sleep 15 &$

$ who > whoson;sleep 15 &$

$ psPID TTY STAT TIME COMMAND20174 p1 S 0:00 login -h 20175 p1 S 0:00 -sh20241 p1 S 0:00 sleep 1520244 p1 R 0:00 ps$

$ psPID TTY STAT TIME COMMAND20174 p1 S 0:00 login -h 20175 p1 S 0:00 -sh20241 p1 S 0:00 sleep 1520244 p1 R 0:00 ps$

Page 6: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

6

Multitaskingps [-aux]

Display Currently Running Processes (Tasks) -a All User Processes -u User Info -x Not Attached to a Terminal

$ ps -auxUSER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMANDbin 131 0.0 0.0 896 0 ? SW Sep 9 0:00 (portmap)nobody 30221 0.0 4.1 1184 608 ? S Sep 16 0:00 httpdnobody 30224 0.0 4.3 1196 632 ? S Sep 16 0:00 httpdjsmith 20115 0.0 5.2 1024 776 p0 S 21:21 0:00 login -h jsmith 20116 0.0 3.2 1048 472 p0 S 21:21 0:00 -shrdefe 20174 0.0 5.2 1024 776 p1 S 21:36 0:00 login -h rdefe 20175 0.0 2.9 1048 428 p1 S 21:36 0:00 -shrdefe 20245 0.0 2.8 928 412 p1 R 21:47 0:00 ps auxroot 1 0.0 0.3 884 56 ? S Sep 9 0:33 init [3]

$ ps -auxUSER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMANDbin 131 0.0 0.0 896 0 ? SW Sep 9 0:00 (portmap)nobody 30221 0.0 4.1 1184 608 ? S Sep 16 0:00 httpdnobody 30224 0.0 4.3 1196 632 ? S Sep 16 0:00 httpdjsmith 20115 0.0 5.2 1024 776 p0 S 21:21 0:00 login -h jsmith 20116 0.0 3.2 1048 472 p0 S 21:21 0:00 -shrdefe 20174 0.0 5.2 1024 776 p1 S 21:36 0:00 login -h rdefe 20175 0.0 2.9 1048 428 p1 S 21:36 0:00 -shrdefe 20245 0.0 2.8 928 412 p1 R 21:47 0:00 ps auxroot 1 0.0 0.3 884 56 ? S Sep 9 0:33 init [3]

Page 7: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

7

Multitasking

kill [-9] PID1 PID2 … PIDN

Stop (kill) A Currently Running Process Can Only Stop Your Processes -9 `Sure Kill`

$ kill 20241$

$ kill 20241$

$ psPID TTY STAT TIME COMMAND20174 p1 S 0:00 login -h 20175 p1 S 0:00 -sh20241 p1 S 0:00 report120244 p1 R 0:00 ps$

$ psPID TTY STAT TIME COMMAND20174 p1 S 0:00 login -h 20175 p1 S 0:00 -sh20241 p1 S 0:00 report120244 p1 R 0:00 ps$

$ kill -9 20241$

$ kill -9 20241$

$ kill 0$

$ kill 0$

Page 8: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

8

Multitasking

jobs List background jobskill %1 Kill background job 1

$ jobs[1] Running prog1 &[2]- Running report23 &[3]+ Running x57 &$ kill %2[2]- Terminated report23$

$ jobs[1] Running prog1 &[2]- Running report23 &[3]+ Running x57 &$ kill %2[2]- Terminated report23$

Page 9: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

9

Command Line Editing

Command History

$ history 9 653 ls -l data 654 pwd 655 vi junk 656 pwd 657 ls data 658 cat data/junk 659 cat junk 660 who 661 ls | grep m$

$ history 9 653 ls -l data 654 pwd 655 vi junk 656 pwd 657 ls data 658 cat data/junk 659 cat junk 660 who 661 ls | grep m$

Display the last 9 commands

Page 10: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

10

Command Line Editing

Editing Commands on the command line Setting command editor to vi Commands can be changed using vi editing

commands

$ set –o vi$ history 3 659 cat junk 660 who 661 ls | grep m$ ls | grep m

$ set –o vi$ history 3 659 cat junk 660 who 661 ls | grep m$ ls | grep m

Press ESC – to return last command

Page 11: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

11

Command Line Editing

Editing commands using fc

$ history 3 653 ls -l data 654 pwd 655 vi p1$ fc 656$$ fc –s 660

$ history 3 653 ls -l data 654 pwd 655 vi p1$ fc 656$$ fc –s 660

Edit command using vi. Command is run when you exit vi.

Run command 660

Page 12: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

12

Command Aliases

Define Command PreferencesRedefine or create new or compound

commands$ alias ls=“ls –p”$ alias cp=“cp –i”$ aliasalias ls=‘ls –p’alias cp=‘cp –i’$ lsa999 et mail/ mymail xyzbin/ examples-cpio mbox public_html/

$ alias ls=“ls –p”$ alias cp=“cp –i”$ aliasalias ls=‘ls –p’alias cp=‘cp –i’$ lsa999 et mail/ mymail xyzbin/ examples-cpio mbox public_html/

Page 13: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

13

Command Aliases

Disabling Aliases

$ aliasalias ls='ls -p‘$ $ lsa999 et/ mail/ mymail$

$ aliasalias ls='ls -p‘$ $ lsa999 et/ mail/ mymail$

$ \lsa999 et mail mymail$

$ \lsa999 et mail mymail$

$ unalias ls$

$ unalias ls$

Page 14: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

14

Shell Scripts

Text File Containing Unix CommandsMust Use Correct Commands &

Syntax Options, Arguments, One Command per Line,

etc.$ cat llls -l $ sh ll-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 feb-rw-r--r-- 1 rdefe unix 62 Sep 12 21:47 ll$

$ cat llls -l $ sh ll-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 feb-rw-r--r-- 1 rdefe unix 62 Sep 12 21:47 ll$

$ chmod u+x ll$ ll-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 feb-rwxr--r-- 1 rdefe unix 62 Sep 12 21:47 ll$

$ chmod u+x ll$ ll-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 feb-rwxr--r-- 1 rdefe unix 62 Sep 12 21:47 ll$

Page 15: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

15

Shell Scripts

echo [String] Display a Message to the Screen

$ cat llecho “my ls command”ls -l $ llmy ls command-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 feb-rwxr--r-- 1 rdefe unix 62 Sep 12 21:47 ll$

$ cat llecho “my ls command”ls -l $ llmy ls command-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 feb-rwxr--r-- 1 rdefe unix 62 Sep 12 21:47 ll$

Page 16: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

16

Shell Scripts

Passing Arguments to Shell Scripts Positional Parameters

$ cat llls -l $ ll data

$ cat llls -l $ ll data

List the contents of a directory?

No!

$ cat llls -l $1 $ ll data

$ cat llls -l $1 $ ll data

List the contents of a directory?

Yes!

Page 17: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

17

Shell Scripts

Passing Arguments to Shell Scripts Positional Parameters

$ cat llls -l $1 $2 $3$

$ cat llls -l $1 $2 $3$

$ ll data mail docs$ ll data mail docs

$0 Script File Name

$1 - $9 Arguments 1-9

$* All Arguments

$0

Page 18: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

18

Shell Scripts

Positional Parameters Use Any Where, Any Place for Any Purpose

$ cat mymovemv $1 $1.new$

$ cat mymovemv $1 $1.new$

$0 Script File Name

$1 - $9 Arguments 1-9

$* All Arguments

$ cat mymorepr -n $* | more$

$ cat mymorepr -n $* | more$

$ cat mylogdate >> logwho >> loggrep $1 log$

$ cat mylogdate >> logwho >> loggrep $1 log$

Page 19: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

19

Shell Scripts

Examples

$ cat paramecho "the number of parameters passed = $#"

echo "param 0 = $0"echo "param 1 = $1"echo "param 2 = $2"echo "param 3 = $3"echo "param * = $*"$

$ cat paramecho "the number of parameters passed = $#"

echo "param 0 = $0"echo "param 1 = $1"echo "param 2 = $2"echo "param 3 = $3"echo "param * = $*"$

$ param aaa bbb cccthe number of parameters passed = 3param 0 = ./paramparam 1 = aaaparam 2 = bbbparam 3 = cccparam * = aaa bbb ccc

$ param aaa bbb cccthe number of parameters passed = 3param 0 = ./paramparam 1 = aaaparam 2 = bbbparam 3 = cccparam * = aaa bbb ccc

Page 20: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

20

Unix File Permissions

Determines Who Can Access What File$ ls -l-rwxrw-r-- 1 rdefe unix 53 Sep 12 21:46 p1-rwxr-xr-x 1 rdefe unix 62 Sep 12 21:47 mar$

$ ls -l-rwxrw-r-- 1 rdefe unix 53 Sep 12 21:46 p1-rwxr-xr-x 1 rdefe unix 62 Sep 12 21:47 mar$

r Read Permission

w Write Permission

x Execute Permission

- Place Holder/No Permission

Page 21: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

21

Unix File Permissions

$ ls -l-rwxrw-r-- 1 rdefe unix 53 Sep 12 21:46 p1-rwxr-xr-x 1 rdefe unix 62 Sep 12 21:47 mar$

$ ls -l-rwxrw-r-- 1 rdefe unix 53 Sep 12 21:46 p1-rwxr-xr-x 1 rdefe unix 62 Sep 12 21:47 mar$

Owner Group

OwnerGroupOther

Ow

ne

r

Gro

up

Oth

er

Page 22: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

22

Unix File Permissions

$ ls -l-rw-rw-r-- 1 rdefe unix 53 Sep 12 21:46 p1

$ chmod gu+x p1$ ls -l-rwxrwxr-- 1 rdefe unix 53 Sep 12 21:46 p1

$ ls -l-rw-rw-r-- 1 rdefe unix 53 Sep 12 21:46 p1

$ chmod gu+x p1$ ls -l-rwxrwxr-- 1 rdefe unix 53 Sep 12 21:46 p1

chmod [u|g|o|a] [+|-|=] [r|w|x] File1 File2 … FileN

+ Add Permission

Note: You can only change permissions on files that you own.

Page 23: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

23

Unix File Permissions

$ ls -l-rwxrwxr-x 1 rdefe unix 53 Sep 12 21:46 p1

$ chmod g-w,-x p1$ ls -l-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 p1

$ ls -l-rwxrwxr-x 1 rdefe unix 53 Sep 12 21:46 p1

$ chmod g-w,-x p1$ ls -l-rw-r--r-- 1 rdefe unix 53 Sep 12 21:46 p1

chmod [u|g|o|a] [+|-|=] [r|w|x] File1 File2 … FileN

- Remove Permission

Page 24: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

24

Unix File Permissions

$ ls -l-rwxrw-r-- 1 rdefe unix 53 Sep 12 21:46 p1

$ chmod u=r,g=rx p1$ ls -l-r--r-xr-- 1 rdefe unix 53 Sep 12 21:46 p1

$ ls -l-rwxrw-r-- 1 rdefe unix 53 Sep 12 21:46 p1

$ chmod u=r,g=rx p1$ ls -l-r--r-xr-- 1 rdefe unix 53 Sep 12 21:46 p1

chmod [u|g|o|a] [+|-|=] [r|w|x] File1 File2 … FileN

= Set to a specific value

Page 25: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

25

Shell Variables

Store Values & Text InformationValues Are Lost When You Logoff

$ x=678$ echo $x678

$ mesg=“This is a text string”$ echo $mesgThis is a text string

$ x=678$ echo $x678

$ mesg=“This is a text string”$ echo $mesgThis is a text string

Page 26: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

26

Shell Variables

Environmental VariablesStores Important Setup Information

$ envPS1=$PATH=/usr/local/bin:/bin:/usr/bin:/usr/bin/mhHOSTNAME=unix.ccri.cc.ri.usUSER=rdefeMAIL=/var/spool/mail/rdefeHOME=/home/rdefeTERM=vt220LOGNAME=rdefe$

$ envPS1=$PATH=/usr/local/bin:/bin:/usr/bin:/usr/bin/mhHOSTNAME=unix.ccri.cc.ri.usUSER=rdefeMAIL=/var/spool/mail/rdefeHOME=/home/rdefeTERM=vt220LOGNAME=rdefe$

Page 27: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

27

Shell Variables

Changing Environmental Variables PS1

$ echo $PS1 $$ export PS1='> '>> export PS1='$PWD> '/home/rdefe> cd /etc/etc> cd/home/rdefe> /home/rdefe> export PS1='$ '$

$ echo $PS1 $$ export PS1='> '>> export PS1='$PWD> '/home/rdefe> cd /etc/etc> cd/home/rdefe> /home/rdefe> export PS1='$ '$

Page 28: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

28

Shell Variables

Changing Environmental Variables PATH

$ echo $PATH/usr/local/bin:/bin:/usr/bin:.$ export PATH=$PATH:$HOME/bin$ echo $PATH/usr/local/bin:/bin:/usr/bin:.:/home/rdefe/bin$

$ echo $PATH/usr/local/bin:/bin:/usr/bin:.$ export PATH=$PATH:$HOME/bin$ echo $PATH/usr/local/bin:/bin:/usr/bin:.:/home/rdefe/bin$

Page 29: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

29

Shell Variables

Saving Environmental Changes .bash_profile

Shell Script Run at Login TimeJust Like Any Shell Script

$ cat .bash_profilemesg nexport PATH=$PATH:$HOME/binexport PS1='$PWD> 'who -q$

$ cat .bash_profilemesg nexport PATH=$PATH:$HOME/binexport PS1='$PWD> 'who -q$

Page 30: Introduction to UNIX. 2 Multitasking  Foreground Current Task The Command Running at the `$` Prompt  Background Command Running Behind the Scenes Not.

30

Shell Variables

Using Shell Variables in Shell Scripts

$ cat mycopyend=123cp $1 $HOME/$1.$end$$ mycopy mbox

$ cat mycopyend=123cp $1 $HOME/$1.$end$$ mycopy mbox

Creates the file mbox.123 in your home directory