10
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology [email protected]

Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology [email protected]

Embed Size (px)

Citation preview

Page 1: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Advanced UNIX Shell

Dr. Tran, Van Hoai

Faculty of Computer Science and Engineering HCMC Uni. of Technology

[email protected]

Page 2: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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”

Page 3: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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;}

Page 4: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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

$

Page 5: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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”

Page 6: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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

Page 7: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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”

Page 8: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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

Page 9: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

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

Page 10: Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

Ad

van

ce

d U

NIX

Sh

ell Basic shell script programming

is NEXT