210 likes | 303 Views
Learn shell redirection techniques in Linux, including standard output and input redirection, pipes, and new utilities such as tr and tee. Discover how to manipulate text efficiently using redirection and wildcards.
E N D
Lecture 8: Shell (ch 5) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
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) • trabc ABC • trhld 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 • Control characters count • echo hi(CTRL+V, CTRL+A) > hello • wc -c hello
More Examples • Redirection • echo “***” >> hello • Insert a line before the first line • echo “***” > temp • cat temp hello > hello.new • mvhello.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]*
Filename Generation/Wildcards • Shell expands ambiguous filenames • ls hello? • ls hello\? The shell searches for matching filenames: hello1, hello2 hello1, hello2 ls hello? shell ls
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 • 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 • CTRL+C • Background • kill process_ID • tail –f hello &