Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.

Similar presentations


Presentation on theme: "Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University."— Presentation transcript:

1 Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University

2 Objectives Understand different Unix shells and how to change Understand how to use wildcards Understand how to shell variables and how to set up Understand how to create sub-shells Understand common shell commands Understand the difference of using arithmetic expressions in all four shells

3 Unix Shells 4 Unix command shells: 1.Bourne (by Steve Bourne): provided in most Unix machines, use /bin/sh to create a sub-shell, good for batch processing, lack of aliases and command history, prompt $ 2.C (by Bill Joy): good for interactive work, use /bin/csh to create a sub-shell, has aliases and command history, prompt % 3.Korn (by David Korn): combine interactive features from C shell and programming features from Bourne shell, lack of directory stack manipulation and weak aliases, use /bin/ksh to create a sub-shell, prompt $ 4.Bash(Bourne Again by GNU): most freely available, conform to POSIX shell specification, use /bin/bash to create a sub-shell, prompt bash$ Note: use chsh to change to different login shell.

4 Linux Command Options There are 3 options are available for Linux commands: 1.Use – (Common used in Unix) 2.Use - - (POSIX form) 3.Use nothing (BSD form) Example: ls –l (common form) ls - - l (POSIX form) ls l (BSD form)

5 Shell Wildcard NameExplain *0 or more characters ?match any one character [...]match any one of the characters within [ ]

6 Shell Wildcard Examples: –ls * list all filenames –ls ? list filenames having one character

7 Shell Variables 1.Environment (or Global) variables: (upper case letter) copied automatically to sub-shell Built-in User-defined 2.Local Variables: used in individual shell only Built-in User-defined

8 Shell Variables – built-in Environment Built-in Environment Variables NameExplain $HOMEHome directory $PATHSearched path $MAILMailbox path $USERUsername $SHELLLogin shell $TERMTerminal type

9 Shell Variables – built-in Environment Examples: Check environment variables –env, an external command and runs in a child process –or echo $TERM

10 Shell Variables – set-up Environment Examples: Change environment variables –set TERM=vt100or –setenv TERM vt100

11 Shell Variables – built-in Local Built-in Local Variables NameExplain $$Shell PID $0, $1, …, $9 Shell command line parameters $*All shell command line parameters

12 Shell Variables – built-in Local Examples: Check Local variables –echo $variablename or –set a shell built-in command

13 Shell Variables – set-up Local Examples: Change Local variables –set variablename=value

14 Process Control - ps ps: check process status –Syntax: ps –option  Option:  e: list all currently running processes (A for Linux)  f: gives full listing (give ancestry for Linux)  l: gives long listing  u username: list processes for the user only

15 Process Control - ps Example: –ps –el list all process information with columns:  S: state  S: sleeping  O: running  R: on run queue  Z: zombie (parent didn’t wait for death of its child  T: stopped or suspended Note: A process is orphan if parent dies before its child

16 Process Control - ps Example: (Cont.)  UID  PID: process id  PPID: parent PID  PRI: priority  SZ: total pages in virtual memory  TIME: execution time  CMD: command

17 Process Control (Cont.) bg or &: sends foreground process to background Ctrl-z (or ctrl-]-z in DOS): stop process running jobs: shows all background processes id fg %id: sends background process id to foreground kill %id: terminate background process id kill -9 pid: terminate foreground process id Ctrl-c: terminate current running process

18 Process Control (Cont.) Note: for background process, no standard input is available, no way to see the standard output and standard error. You can either redirect output/error or let kernel to suspend the process right before standard output and standard error by setting as below: stty tostop

19 Process Control (Cont.) Example: use one Unix window –cat –Ctrl-z –Jobs –cat > file2 & –jobs –fg %1 –Ctrl-z –kill %1 (or kill %cat or kill %?file2) –jobs –tty find the terminal –fg

20 Process Control (Cont.) Example: (Cont.) use another one Unix window –ps –u username find the process id for the previous shell –kill -9 PID this will terminate the process: cat > file2

21 Create Sub-Shell 4 ways to create subshells: 1.group shell commands within ( ). Examples: pwd; (cd /; pwd); pwd The commands in ( ) are executed in a subshell. 2.invoke /bin/sh, /bin/csh, /bin/ksh, or /bin/bash 3.starts background processes. 4.starts a shell script

22 Common Shell Commands command substitution ` ` Examples: echo today is `date`

23 Common Shell Commands quoting(in pair): Rules: double quotes prevents wildcard replacement only. single quote prevents wildcard replacement, variable substitution and command substitution. only the outer quote works if quotes are nested

24 Common Shell Commands Examples: echo * The shell expands *. echo "*" The shell does not expands *. echo '*' The shell does not expands *. echo "the user is $USER and files are `ls *`" The shell expands $USER and *. echo 'the user is $USER and files are `ls *`' The shell does not expands either $USER or *.

25 Common Shell Commands sleep n1 The shell sleeps for n1 seconds. Examples: (sleep 5; echo subshell done)&; ps -f

26 Common Shell Commands wait [n] The shell suspends its processes until child PID n (background process) terminates. Examples: (sleep 10; echo done #1)&; (sleep 20; echo done #2)&; echo done #3; wait; echo done #4 the shell prints done #3 and wait until subshell prints done #1, them done #2. Then it prints done #4.

27 Common Shell Commands exit n The shell exits with return code n.

28 Common Shell Commands eval command It executes the output of the command as a regular shell command. Examples: eval `set echo x=2`; echo $x (Solaris) eval set echo x=2; echo $x (Linux)

29 Common Shell Commands exec command It executes the command and then terminates the shell. Examples: exec date

30 Common Shell Commands command1 && command2 command2 will be executed after the success of executing command1 Examples: gcc beep.c && a.out

31 Common Shell Commands command1 || echo error_message error_message will be printed after the failure of executing command1 Examples: gcc beep_syntaxerror.c || echo compilation error

32 Difference in Arithmetic Expression C ShellBourne Shell Korn Shell Bash Shell Start Up File.cshrc, then.login.profile.profile or.kshrc.bashrc or.bash_profile Arith metic Expre ssion set x=1 echo $x @ x ++ echo $x x=1 echo $x x=`expr $x + 1` echo $x let x=1 echo $x let x=x+1 echo $x declare -i x x=($x+1) echo $x x=($x+1) echo $x

33 Reference Misc. (for Linux other than Ubantu) killall –u uid: kills all other shell processes killall -9 tcsh: kills all tcsh shell processes Ch. 4


Download ppt "Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University."

Similar presentations


Ads by Google