Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Dr. Tran, Van Hoai 2007 Advanced UNIX Shell Redirection (1) “ > ” redirect input Ex: $ 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 Hoai 2007 Advanced UNIX Shell Redirection (2) “ < ” redirect input Ex: hello.c #include int main( int argc, char *argv[] ) { double d; scanf( "%lf", &d ); printf( "My input number: %lf\n", d ); return 0; }
Dr. Tran, Van Hoai 2007 Advanced UNIX Shell Redirection (3) $ hello My input number: $ hello < aaa.txt My input number: $
Dr. Tran, Van Hoai 2007 Advanced UNIX Shell 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 Hoai 2007 Advanced UNIX Shell 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 Hoai 2007 Advanced UNIX Shell 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 Hoai 2007 Advanced UNIX Shell 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 Hoai 2007 Advanced UNIX Shell Other symbols &Run in background (daemon) ~Home directory " Partial quote (variable/command expansion) ' Full quote (no expansion) $varValue of variable $$Process Id $nnth argument ($0=command) $*All arguments *,?Wildcard characters
Dr. Tran, Van Hoai 2007 Advanced UNIX Shell Basic shell script programming is NEXT