More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file...

30
More Shell Basics CS465 - Unix
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file...

Page 1: 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.

More Shell Basics

CS465 - Unix

Page 2: 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.

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

$

Page 3: 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.

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

Page 4: 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.

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 $

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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

$

Page 8: 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.

• 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

Page 9: 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.

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 &

Page 10: 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.

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

Page 11: 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.

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]

Page 12: 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.

Jobs

• The jobs command tells you what jobs are running:

$ cat names | sort > snames

<ctrl>Z

Suspended

$ jobs

[1] +Suspended cat names | sort > snames

$

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

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/

Page 16: 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.

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

Page 17: 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.

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.

Page 18: 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.

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

Page 19: 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.

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.

Page 20: 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.

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

Page 21: 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.

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

Page 22: 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.

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

Page 23: 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.

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.

Page 24: 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.

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 ~

$

Page 25: 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.

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

Page 26: 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.

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:.

Page 27: 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.

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.

Page 28: 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.

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$

Page 29: 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.

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$

Page 30: 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.

Exit Codes

• Your scripts should return exit codes.

• Format:

exit [NUMBER]

• For a successful exit:

exit 0