Upload
adrian-benson
View
212
Download
0
Embed Size (px)
Citation preview
Advanced UNIX Shell
Dr. Tran, Van Hoai
Faculty of Computer Science and Engineering HCMC Uni. of Technology
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Redirection (1)
“>” redirect inputEx:
$ cat aaa.txt
Sends the content of “aaa.txt” to stdout (normally screen)
$ cat aaa.txt > bbb.txt
Sends the content of “aaa.txt” to “bbb.txt”
or copy file “aaa.txt” to a new file “bbb.txt”
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Redirection (2)
“<” redirect inputEx: hello.c
#include <stdio.h>
int main( int argc, char *argv[] ){ double d; scanf( "%lf", &d );
printf( "My input number: %lf\n", d ); return 0;}
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Redirection (3)
$ hello
10005.6<enter>
My input number: 10005.6
$ hello < aaa.txt
My input number: 10006.5
$
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Appending to file
“>>” appending to file
Ex:
$ cat aaa.txt >> bbb.txt
Appends the content of “aaa.txt” to the end of “bbb.txt”
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Pipe Connect several commands together UNIX commands get input from stdin
and pass output to stdout “|” directs UNIX to connect stdout from the
first command to the stdin of the second command
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Pipe (2) Ex:
$ cat aaa.txt | more
Sends the content of “aaa.txt” to command “more”
$ ls * | grep pdf
Lists all files and choose only files which have pattern “pdf”
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Pipe (3)
$ ls /usr/bin/ | grep pdf | wc –l
“wc –l” count the number of lines
The whole command counts the number of ?
files in “/usr/bin” having a pattern “pdf” in names
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell
Other symbols
& Run in background (daemon)
~ Home directory
" " Partial quote (variable/command expansion)
' ' Full quote (no expansion)
$var Value of variable
$$ Process Id
$n nth argument ($0=command)
$* All arguments
*,? Wildcard characters
Dr. Tran, Van Hoai2007
Ad
van
ce
d U
NIX
Sh
ell Basic shell script programming
is NEXT