IT244 - Introduction to Linux / Unix Instructor: Bo Sheng Shell (ch 5) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Outline Command line syntax and processing Standard I/O and device files Redirection and pipes
Command Line Syntax Arguments command [arg1] [arg2] … [argn] RETURN Separated by space Could be optional ls ls -l cp hello hello.copy command [arg1] [arg2] … [argn] RETURN
Command Line Syntax Arguments (options) ls –l, rm –i Combining options ls -l -r = ls –lr Option arguments gcc -o hello hello.c
Command Line Syntax Non-option arguments starting with ‘-’ Create a file ‘-l’ and then list it ls –l ls ./-l, ls -- -l Create a file ‘-i’ and then delete it rm –i rm ./-i, rm -- -i
Command Line Processing e.g., /bin/ls Arguments are processed by programs, NOT the shell
Standard I/O
Standard I/O
Device Files The screen and keyboard as a file who, w cat CTRL+D (EOF) to terminate
Device Files The screen and keyboard as a file An example with two terminals Use “who” to find out their device files, e.g., /dev/pts/1 and /dev/pts/2 In terminal 2, type in echo hello > /dev/pts/1 cat /dev/pts/1 Then type in terminal 1
Redirection Redirecting standard output ( > ) command [arguments] > filename
Redirection Redirecting standard output ( > ) Examples echo hello world > hello cat > sample cat hello > sample cat days months > sample
Redirection Redirecting standard output ( > ) Examples ls > filelist Append (>>) echo append >> hello
Redirection Redirecting standard input ( < ) command [arguments] < filename
Pipes and Redirection command1 [arguments] | command2 [arguments] command1 [arguments] > temp command2[arguments] < temp rm temp
Some New Utilities Translate (tr) tr abc ABC tr hld xyz < hello tr “[A-Z]” “[a-z]” linux Examples cat month colors.1|tr “[A-Z]” “[a-z]” | sort
Some New Utilities Split directions (tee) tee filename ls –l | tee filelist cat colors.1 | tee file1 > file2 grep e colors.1 | tee /dev/pts/1 | sort
More Examples Use tr to replace or delete characters echo “this is a test” > test wc -c test tr -d “ ” < test | wc –c
More Examples Redirection echo “***” >> hello Insert a line before the first line echo “***” > temp cat temp hello > hello.new mv hello.new hello cat hello >> temp mv temp hello cat temp hello > hello ?
Filename Generation/Wildcards “*” and “?” “?” : single character “*” : any number of characters (including 0) ls colors.? echo col* ls hello*
Filename Generation/Wildcards Examples echo ????? echo ~/.* cat hello*
Filename Generation/Wildcards “[]” for range echo [aeiou]* cat colors.[135] echo [a-z]* echo *[a-l] “^” for “not” echo *[^a-z] echo [^a-o]*
Execute Commands Foreground and background jobs cp ~shengbo/it244/exetest . ./exetest (CTRL+C to terminate) ./exetest & (running in background) jobs
Execute Commands Foreground and background jobs Multitasking One foreground in a terminal Only foreground gets input from keyboard Multitasking
Execute Commands Job number / PID number Job number Process ID
Execute Commands Job number / PID number Job number Process ID
Execute Commands Suspend a foreground job ./exetest CTRL+Z jobs Different from a running background job
Execute Commands Resume a suspended job In the background bg [%job_number] In the foreground fg [%job_number] Bring background job to foreground
Execute Commands Abort a job Foreground Background CTRL+C kill process_ID kill %job_number tail –f hello &