300 likes | 425 Views
Introduction to UNIX. Multitasking. Foreground Current Task The Command Running at the `$` Prompt Background Command Running Behind the Scenes Not Interactive (e.g. text editor). Multitasking. Background Tasks & Perform Command in the Background. $ who > whoson & [1] 10946 $.
E N D
Multitasking • Foreground • Current Task • The Command Running at the `$` Prompt • Background • Command Running Behind the Scenes • Not Interactive (e.g. text editor)
Multitasking • Background Tasks • & Perform Command in the Background $ 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
Multitasking • Background Tasks • nohup UnixCommand • Prevents Command From Terminating on Logoff $ nohup who & nohup: appending output to 'nohup.out' $ No output file specified. Output redirected to nohup.out $ nohup who > whoson & $ Output redirected to whoson
Multitasking • Every Command is Assigned a Unique Number • Process ID (PID) • ps Display Currently Running Tasks $ who > whoson;sleep 15 & $ $ ps PID TTY STAT TIME COMMAND 20174 p1 S 0:00 login -h 20175 p1 S 0:00 -sh 20241 p1 S 0:00 sleep 15 20244 p1 R 0:00 ps $
Multitasking • ps [-aux] • Display Currently Running Processes (Tasks) • -aAll User Processes • -uUser Info • -xNot Attached to a Terminal $ ps -aux USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND bin 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 httpd nobody 30224 0.0 4.3 1196 632 ? S Sep 16 0:00 httpd jsmith 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 -sh rdefe 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 -sh rdefe 20245 0.0 2.8 928 412 p1 R 21:47 0:00 ps aux root 1 0.0 0.3 884 56 ? S Sep 9 0:33 init [3]
Multitasking • kill [-9] PID1 PID2 … PIDN • Stop (kill) A Currently Running Process • Can Only Stop Your Processes • -9 `Sure Kill` $ kill 20241 $ $ ps PID TTY STAT TIME COMMAND 20174 p1 S 0:00 login -h 20175 p1 S 0:00 -sh 20241 p1 S 0:00 report1 20244 p1 R 0:00 ps $ $ kill -9 20241 $ $ kill 0 $
Multitasking • jobs List background jobs • kill %1 Kill background job 1 $ jobs [1] Running prog1 & [2]- Running report23 & [3]+ Running x57 & $ kill %2 [2]- Terminated report23 $
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 $ Display the last 9 commands
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 Press ESC – to return last command
Command Line Editing • Editing commands using fc $ 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
Command Aliases • Define Command Preferences • Redefine or create new or compound commands $ alias ls=“ls –p” $ alias cp=“cp –i” $ alias alias ls=‘ls –p’ alias cp=‘cp –i’ $ ls a999 et mail/ mymail xyz bin/ examples-cpio mbox public_html/
Command Aliases • Disabling Aliases $ alias alias ls='ls -p‘ $ $ ls a999 et/ mail/ mymail $ $ \ls a999 et mail mymail $ $ unalias ls $
Shell Scripts • Text File Containing Unix Commands • Must Use Correct Commands & Syntax • Options, Arguments, One Command per Line, etc. $ cat ll ls -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 $
Shell Scripts • echo [String] • Display a Message to the Screen $ cat ll echo “my ls command” ls -l $ ll my 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 $
Shell Scripts • Passing Arguments to Shell Scripts • Positional Parameters List the contents of a directory? No! $ cat ll ls -l $ ll data List the contents of a directory? Yes! $ cat ll ls -l $1 $ ll data
$0 Shell Scripts • Passing Arguments to Shell Scripts • Positional Parameters $0 Script File Name $1 - $9 Arguments 1-9 $* All Arguments $ cat ll ls -l $1 $2 $3 $ $ ll data mail docs
Shell Scripts • Positional Parameters • Use Any Where, Any Place for Any Purpose $0 Script File Name $1 - $9 Arguments 1-9 $* All Arguments $ cat mymove mv $1 $1.new $ $ cat mymore pr -n $* | more $ $ cat mylog date >> log who >> log grep $1 log $
Shell Scripts • Examples $ cat param echo "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 ccc the number of parameters passed = 3 param 0 = ./param param 1 = aaa param 2 = bbb param 3 = ccc param * = aaa bbb ccc
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 $ r Read Permission w Write Permission x Execute Permission - Place Holder/No Permission
Owner Group Owner Group Other Unix File Permissions • Owner • Group • Other $ 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 $
Unix File Permissions • chmod [u|g|o|a] [+|-|=] [r|w|x] File1 File2 … FileN • +Add Permission $ 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 Note:You can only change permissions on files that you own.
Unix File Permissions • chmod [u|g|o|a] [+|-|=] [r|w|x] File1 File2 … FileN • -Remove Permission $ 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
Unix File Permissions • chmod [u|g|o|a] [+|-|=] [r|w|x] File1 File2 … FileN • =Set to a specific value $ 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
Shell Variables • Store Values & Text Information • Values Are Lost When You Logoff $ x=678 $ echo $x 678 $ mesg=“This is a text string” $ echo $mesg This is a text string
Shell Variables • Environmental Variables • Stores Important Setup Information $ env PS1=$ PATH=/usr/local/bin:/bin:/usr/bin:/usr/bin/mh HOSTNAME=unix.ccri.cc.ri.us USER=rdefe MAIL=/var/spool/mail/rdefe HOME=/home/rdefe TERM=vt220 LOGNAME=rdefe $
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='$ ' $
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 $
Shell Variables • Saving Environmental Changes • .bash_profile • Shell Script Run at Login Time • Just Like Any Shell Script $ cat .bash_profile mesg n export PATH=$PATH:$HOME/bin export PS1='$PWD> ' who -q $
Shell Variables • Using Shell Variables in Shell Scripts $ cat mycopy end=123 cp $1 $HOME/$1.$end $ $ mycopy mbox Creates the file mbox.123 in your home directory