Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 More Shell Basics CS465 - Unix

2 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 $

3 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

4 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 $

5 Managing Processes Each command invokes program and runs it - while a program is running it is called a process. The ps command shows your running processes. $ ps PID TTY TIME CMD 14651 pts/0 00:00:00 ksh 14954 pts/0 00:00:00 ps $ Process information: PID - process ID TTY- associated terminal TIME - CPU time used by the process COMMAND - the name of the command

6 Managing Processes The –f option shows a full listing. $ ps –f UID PID PPID C STIME TTY TIME CMD smith 719 716 0 12:24:19 pts/2 0:00 –ksh jones 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 minute STIME - time process was created

7 Managing Processes The –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 $

8 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

9 If a background process will produce output, you must redirect the output to a file, or output will appear on your display. $ sort sorted.names &

10 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 sortednames & $ exit

11 Managing Processes To suspend a foreground process Z To move a foreground command to the background: Z 1 + [stopped] $ bg To move a background process to the foreground: $ fg [process-id]

12 Jobs The jobs command tells you what jobs are running: $ cat names | sort > snames Z Suspended $ jobs [1] +Suspended cat names | sort > snames $

13 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

14 Links Revisited ln 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 lnames jones lnamesnamesletter3 007 Golden Eye Tomorrow Never Dies File Contents

15 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/

16 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

17 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 10 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/ 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.

18 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 seclink ln: secret is a directory $ ln -s secret seclink $ ls -li total 12 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/ 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

19 Differences Between Hard and Soft Links (2) You can also make symbolic links across file systems (on different drives) $ ln /tmp/ps_data ps_data ln: ps_data is on a different file system $ ln -s /tmp/ps_data ps_data $ ls –li total 4 59944 -rw-r--r-- 1 jones cs 154 Feb 4 16:38 letter 59597 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.

20 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 second 123 $ 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

21 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

22 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

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

24 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 ~ $

25 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: $ myscript Mon May 12 12:26:44 MDT 2003 Currently logged on: small000 pts/2 May 12 12:24 (pcisys.net) file1 file2 myscript

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

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

28 Here document Example $ cat script2 cat << stop I can’t stop now... Long live Unix! stop date $ script2 I can’t stop now... Long live Unix! Mon May 12 12:26:44 MDT 2003 $

29 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 $

30 Exit Codes Your scripts should return exit codes. Format: exit [NUMBER] For a successful exit: exit 0


Download ppt "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."

Similar presentations


Ads by Google