Post on 21-Dec-2015
More Shell Basics
CS465 - Unix
Unix shells• User’s default shell - specified in /etc/passwd file
• To show which shell you are currently using:$ echo $SHELL
Filename of the shell you are running displays.
• Example, if running the Bourne shell (sh): $ echo $SHELL /bin/sh
$
Subshells
• Subshells are shells created by a shell
– Environment variables are passed
• Times when subshells are used:
– Explicit invocation
– Shell script execution
– Grouped command execution
– Background processing
Changing Your Shell (temporarily)
• Commands for invoking a subshell:sh, ksh, bash, csh, tcsh
Changes your shell and shows correct shell prompt.
• To leave the subshell and return to your default shell:exit
• Example, if your default is the Korn shell, change to the C shell and then back to the Korn shell:
$ csh % exit $
Managing ProcessesEach command invokes program and runs it
- while a program is running it is called a process. The ps command shows your running processes.
$ psPID TTY TIME
CMD 14651 pts/0 00:00:00 ksh 14954 pts/0 00:00:00 ps
$
Process information:PID - process ID TTY- associated terminalTIME - CPU time used by the processCOMMAND - the name of the command
Managing Processes
The –f option shows a full listing.
$ ps –fUID PID PPID C STIME TTY TIME CMDsmith 719 716 0 12:24:19 pts/2 0:00 –kshjones 715 716 0 11:38:12 pts/0 0:00 –csh$
Additional process information shown:PPID – parent process ID C - % CPU time used in past minuteSTIME - time process was created
Managing ProcessesThe –e option shows ALL processes running on the system (not just
yours).
$ ps –e PID TTY TIME CMD 0 ? 0:08 sched 1 ? 0:00 init 2 ? 0:00 pageout 3 ? 0:06 fsflush 484 ? 0:00 ttymon
58 ? 0:00 sysevent 246 ? 0:00 cron
: 482 pts/1 0:00 ttymon 719 pts/2 0:00 ksh 747 pts/2 0:00 ps
$
• A process may be executed in the foreground or the background.
• Foreground:$ command
– shell waits for the command completion– there is no prompt until the process is completed
• Background:$ command & [1] 276$
– The command is started, the process id is returned, and the shell is immediately ready to take another command
Managing Processes
Managing Processes
• If a background process will produce output, you must redirect the output to a file, or output will appear on your display.
$ sort < names > sorted.names &
Managing Processes
• By default, all processes are terminated when you log out.
• To allow a background process continue to run, place the nohup command in front of the background command.
• Example:
$ nohup sort < names > sortednames &
$ exit
Managing Processes
• To suspend a foreground process<ctrl>Z
• To move a foreground command to the background:<ctrl>Z1 + [stopped]$ bg
• To move a background process to the foreground: $ fg [process-id]
Jobs
• The jobs command tells you what jobs are running:
$ cat names | sort > snames
<ctrl>Z
Suspended
$ jobs
[1] +Suspended cat names | sort > snames
$
Stopping Background Processes
• kill %[job#]Example:
$ kill %1“%1” means “job #1”. You can also use the PID.
• kill [process-id] – but the process can refuse to be killed…
• kill -9 [process-id]– the system kills the process without a question!
Example: $ kill –9 1278
Links Revisitedln creates a new link, not a new file. The new link and the original filename are equivalent pointers to the file.
• The last argument is the link destination:
$ ln names lnamesjones
lnames namesletter3
007
Golden Eye
Tomorrow Never Dies
FileContents
Links• A link contains two pieces of information
– A name– An inode number
• An inode number is an index into a system table that has all the information about the file.
• You can use ls -i to see if two links point to the same inode:$ ls -li
total 8
42979 -rw-r--r-- 3 jones cs 64 Feb 6 18:36 lnames
42976 -rw-r--r-- 1 jones cs 34 Feb 4 15:00 letter3
42979 -rw-r--r-- 3 jones cs 64 Feb 4 15:00 names
59980 drwxr-xr-x 2 jones cs 512 Feb 4 17:10 secret/
Links
• When you use rm
– only removes a link to the file
– when the last link to a file is removed, the file contents are then deleted
Symbolic Links • A symbolic link is a pointer to a pathname, not a pointer to the
file itself. $ ln -s original target (creates symbolic link)
• A symbolic link has a different inode than the original.
$ ln -s names snames$ ls -li total 1042979 -rw-r--r-- 3 jones cs 64 Feb 6 18:36 lnames42976 -rw-r--r-- 1 jones cs 34 Feb 4 15:00 letter342979 -rw-r--r-- 3 jones cs 64 Feb 4 15:00 names59980 drwxr-xr-x 2 jones cs 512 Feb 4 17:10 secret/42916 lrwxrwxrwx 1 jones cs 5 Feb 8 17:09 snames -> names
• Symbolic links are sometimes called soft links, and “regular” links are sometimes called hard links.
Differences Between Hard and Soft Links (1)
• You can’t make a hard link to a directory, but you can make a symbolic link to a directory.
$ ln secret seclinkln: secret is a directory$ ln -s secret seclink$ ls -li total 1242979 -rw-r--r-- 3 jones cs 64 Feb 6 18:36 lnames42976 -rw-r--r-- 1 jones cs 34 Feb 4 15:00 letter342979 -rw-r--r-- 3 jones cs 64 Feb 4 15:00 names59980 drwxr-xr-x 2 jones cs 512 Feb 4 17:10 secret/42917 lrwxrwxrwx 1 jones cs 6 Feb 8 17:21 seclink -> secret/42916 lrwxrwxrwx 1 jones cs 5 Feb 8 17:09 snames -> names$ cd seclink$ pwd/homes/jbond/secret
Differences Between Hard and Soft Links (2)
• You can also make symbolic links across file systems (on different drives)
$ ln /tmp/ps_data ps_dataln: ps_data is on a different file system$ ln -s /tmp/ps_data ps_data$ ls –litotal 459944 -rw-r--r-- 1 jones cs 154 Feb 4 16:38 letter59597 lrwxrwxrwx 1 jones cs 12 Feb 8 17:39 ps_data->/tmp/ps_data
• There is no way to tell how many symbolic links there are to a file.
Biggest Difference Between Hard and Soft Links
• The most important difference between hard and symbolic links occurs when a link is removed. - For a hard link:
$ echo 123 > first$ ln first second$ rm first$ cat second123$ echo 456 > first$ cat first 456$ cat second 123
- For a symbolic link:
$ echo 123 > first
$ ln -s first second
$ rm first
$ cat second
cat: cannot open second
$ echo 456 > first
$ cat first
456
$ cat second
456
Hard Link Summary
• A hard link is a directory entry which points to the disk space of another file
– It is like another name for a file
• There is only a single copy of the file on disk
– A file may have numerous links
• A link may only be to a file on the same file system
• Changing either name has no side effects
Symbolic Link Summary
• A symbolic link is a directory entry which contains the ASCII text string of the pathname of another file or directory
– It is like a pointer to the other file
• Changing the name of the pointed-at file will “break” the link
Shell Scripts
• A sequence of operations can be scripted (i.e. make to be automatic) by placing the operations in a script file.
• Shell scripts provide a full featured programming language, with variables, conditional statements, and the ability to execute other programs.
Shell Script Example
$ cat myscript
# show date
date
# show who is logged on
echo “Currently logged on:”
who
# display all files in home directory
ls ~
$
Running a Shell Script
• Give the file execute permission:$ ls -l myscript-rw-r--r-- 1 smith345 users 114 May 12 11:28 myscript
$ chmod 744 myscript
$ ls -l myscript-rwxr--r-- 1 smith345 users 114 May 12 11:28 myscript
• Execute the script:$ myscriptMon May 12 12:26:44 MDT 2003Currently logged on:small000 pts/2 May 12 12:24 (pcisys.net)file1 file2 myscript
Running a Shell Script
Possible Error Message: If you get a “file not found” error, then your path may not be set up to recognize your current working directory.
Modify by:$
PATH=$PATH:.
Here documents
• Use the “<<” character with the cat command to create “here” documents.
Format:cat << [keyword]
Mainly used inside scripts to display multiple lines without echo on each.
Here document Example$ cat script2cat << stopI can’t stop now. . .Long live Unix!stopdate$ script2I can’t stop now. . .Long live Unix!Mon May 12 12:26:44 MDT 2003$
Exit Codes• Every Unix process sends an exit code back to the shell
when it terminates– An exit code of 0 indicates success.– Variable $? holds the exit code of the last command.
• Example:$ ls f*file1 file2$ echo $?0$ ls x*ls: x*: No such file or directory$ echo $?1$
Exit Codes
• Your scripts should return exit codes.
• Format:
exit [NUMBER]
• For a successful exit:
exit 0