Download presentation
Presentation is loading. Please wait.
Published byColleen Field Modified over 9 years ago
1
Introduction to UNIX
2
2 Multitasking zForeground yCurrent Task yThe Command Running at the `$` Prompt zBackground yCommand Running Behind the Scenes yNot Interactive (e.g. text editor)
3
3 Multitasking zBackground Tasks y&Perform Command in the Background $ who > whoson & [1] 10946 $ $ who > whoson & [1] 10946 $ zImportant Notes yLogoff Terminates Background Tasks yDo Not Overload The System yCan Not Require Keyboard Input yScreen Output May Not Be Desirable
4
4 Multitasking zBackground Tasks ynohup UnixCommand xPrevents 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
5
5 Multitasking zEvery Command is Assigned a Unique Number zProcess ID (PID) zpsDisplay Currently Running Tasks $ who > whoson;sleep 15 & $ $ 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 $ $ 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 $
6
6 Multitasking zps [-aux] yDisplay Currently Running Processes (Tasks) y-a All User Processes y-u User Info y-x Not 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] $ 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]
7
7 Multitasking zkill [-9] PID 1 PID 2 … PID N yStop (kill) A Currently Running Process yCan Only Stop Your Processes y-9 `Sure Kill` $ kill 20241 $ $ 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 $ $ 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 -9 20241 $ $ kill 0 $ $ kill 0 $
8
8 Multitasking zjobsList background jobs zkill %1Kill 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 $
9
9 Command Line Editing zCommand 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
10
10 Command Line Editing zEditing Commands on the command line ySetting command editor to vi yCommands 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
11
11 Command Line Editing zEditing 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
12
12 Command Aliases zDefine Command Preferences zRedefine 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/ $ 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/
13
13 Command Aliases zDisabling Aliases $ alias alias ls='ls -p‘ $ $ ls a999 et/ mail/ mymail $ $ alias alias ls='ls -p‘ $ $ ls a999 et/ mail/ mymail $ $ \ls a999 et mail mymail $ $ \ls a999 et mail mymail $ $ unalias ls $ $ unalias ls $
14
14 Shell Scripts zText File Containing Unix Commands zMust Use Correct Commands & Syntax yOptions, 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 $ $ 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 $ $ 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 $
15
15 Shell Scripts zecho [String] yDisplay 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 $ $ 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 $
16
16 Shell Scripts zPassing Arguments to Shell Scripts yPositional Parameters $ cat ll ls -l $ ll data $ cat ll ls -l $ ll data List the contents of a directory? No! $ cat ll ls -l $1 $ ll data $ cat ll ls -l $1 $ ll data List the contents of a directory? Yes!
17
17 Shell Scripts zPassing Arguments to Shell Scripts yPositional Parameters $ cat ll ls -l $1 $2 $3 $ $ cat ll ls -l $1 $2 $3 $ $ ll data mail docs $0 Script File Name $1 - $9 Arguments 1-9 $* All Arguments $0
18
18 Shell Scripts zPositional Parameters yUse Any Where, Any Place for Any Purpose $ cat mymove mv $1 $1.new $ $ cat mymove mv $1 $1.new $ $0 Script File Name $1 - $9 Arguments 1-9 $* All Arguments $ cat mymore pr -n $* | more $ $ cat mymore pr -n $* | more $ $ cat mylog date >> log who >> log grep $1 log $ $ cat mylog date >> log who >> log grep $1 log $
19
19 Shell Scripts zExamples $ 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 * = $*" $ $ 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 $ 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
20
20 Unix File Permissions zDetermines 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 $ rRead Permission wWrite Permission xExecute Permission -Place Holder/No Permission
21
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 $ OwnerGroup zOwner zGroup zOther OwnerGroupOther
22
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 zchmod [u|g|o|a] [+|-|=] [r|w|x] File 1 File 2 … File N y+ Add Permission Note: You can only change permissions on files that you own.
23
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 zchmod [u|g|o|a] [+|-|=] [r|w|x] File 1 File 2 … File N y- Remove Permission
24
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 zchmod [u|g|o|a] [+|-|=] [r|w|x] File 1 File 2 … File N y= Set to a specific value
25
25 Shell Variables zStore Values & Text Information zValues Are Lost When You Logoff $ x=678 $ echo $x 678 $ mesg=“This is a text string” $ echo $mesg This is a text string $ x=678 $ echo $x 678 $ mesg=“This is a text string” $ echo $mesg This is a text string
26
26 Shell Variables zEnvironmental Variables zStores 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 $ $ 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 $
27
27 Shell Variables zChanging Environmental Variables yPS1 $ 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='$ ' $
28
28 Shell Variables zChanging Environmental Variables yPATH $ 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 $
29
29 Shell Variables zSaving Environmental Changes z.bash_profile xShell Script Run at Login Time xJust Like Any Shell Script $ cat.bash_profile mesg n export PATH=$PATH:$HOME/bin export PS1='$PWD> ' who -q $ $ cat.bash_profile mesg n export PATH=$PATH:$HOME/bin export PS1='$PWD> ' who -q $
30
30 Shell Variables zUsing Shell Variables in Shell Scripts $ cat mycopy end=123 cp $1 $HOME/$1.$end $ $ mycopy mbox $ cat mycopy end=123 cp $1 $HOME/$1.$end $ $ mycopy mbox Creates the file mbox.123 in your home directory
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.