1 / 33

Chapter 4 UNIX Common Shells Commands

Chapter 4 UNIX Common Shells Commands. By C. Shing ITEC Dept Radford University. 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

druce
Download Presentation

Chapter 4 UNIX Common Shells Commands

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • 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 $ • C (by Bill Joy): good for interactive work, use /bin/csh to create a sub-shell, has aliases and command history, prompt % • 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 $ • 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: • Use – (Common used in Unix) • Use - - (POSIX form) • Use nothing (BSD form) Example: ls –l (common form) ls - - l (POSIX form) ls l (BSD form)

  5. Shell Wildcard

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

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

  8. Shell Variables – built-in Environment • Built-in Environment Variables

  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=vt100 or • setenv TERM vt100

  11. Shell Variables – built-in Local • Built-in Local Variables

  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: • group shell commands within ( ). Examples: pwd; (cd /; pwd); pwdThe commands in ( ) are executed in a subshell. • invoke /bin/sh, /bin/csh, /bin/ksh, or /bin/bash • starts background processes. • 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 n1The 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 #4the shell prints done #3 and wait until subshell prints done #1, them done #2. Then it prints done #4.

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

  28. Common Shell Commands • eval commandIt 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 commandIt executes the command and then terminates the shell.Examples:exec date

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

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

  32. Difference in Arithmetic Expression

  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

More Related