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

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

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

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

Chapter 4 UNIX Common Shells Commands

By C. Shing

ITEC Dept

Radford University

Page 2: 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• Understand common shell commands• Understand the difference of using

arithmetic expressions in all four shells

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

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.

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

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)

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

Shell Wildcard

Name Explain

* 0 or more characters

? match any one character

[...] match any one of the characters within [ ]

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

Shell Wildcard

• Examples:– ls *

list all filenames– ls ?

list filenames having one character

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

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

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

Shell Variables – built-in Environment

• Built-in Environment Variables

Name Explain

$HOME Home directory

$PATH Searched path

$MAIL Mailbox path

$USER Username

$SHELL Login shell

$TERM Terminal type

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

Shell Variables – built-in Environment

• Examples: Check environment variables– env,

an external command and runs in a child process

– or echo $TERM

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

Shell Variables – set-up Environment

• Examples: Change environment variables– set TERM=vt100 or– setenv TERM vt100

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

Shell Variables – built-in Local

• Built-in Local Variables

Name Explain

$$ Shell PID

$0, $1, …, $9

Shell command line parameters

$* All shell command line parameters

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

Shell Variables – built-in Local

• Examples: Check Local variables– echo $variablename or – set

a shell built-in command

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

Shell Variables – set-up Local

• Examples: Change Local variables– set variablename=value

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

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 listingu username: list processes for the user only

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

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

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

Process Control - ps

• Example: (Cont.)UIDPID: process idPPID: parent PIDPRI: prioritySZ: total pages in virtual memoryTIME: execution timeCMD: command

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

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

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

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

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

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

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

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

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

Create Sub-Shell

• 4 ways to create subshells: 1. group shell commands within ( ).

Examples: pwd; (cd /; pwd); pwdThe 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

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

Common Shell Commands

• command substitution ` ` Examples: echo today is `date`

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

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

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

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

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

Common Shell Commands

• sleep n1The shell sleeps for n1 seconds.

• Examples:(sleep 5; echo subshell done)&; ps -f

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

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.

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

Common Shell Commands

• exit nThe shell exits with return code n.

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

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)

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

Common Shell Commands

• exec commandIt executes the command and then terminates the shell.Examples:exec date

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

Common Shell Commands

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

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

Common Shell Commands

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

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

Difference in Arithmetic Expression

C Shell Bourne Shell

Korn Shell

Bash Shell

Start Up File

.cshrc, then .login

.profile .profile or .kshrc

.bashrc or .bash_profile

Arithmetic Expression

set x=1echo $x@ x ++echo $x

x=1echo $xx=`expr $x + 1`echo $x

let x=1echo $xlet x=x+1echo $x

declare -i xx=($x+1)echo $xx=($x+1)echo $x

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

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