380 likes | 680 Views
Unix Operating System. Shell Script Programming. อ้างอิงจาก http://apollo.lsc.vsc.edu. What is shell Script?. " Shell Script is series of command written in plain text file . Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file. ".
E N D
Unix Operating System Shell Script Programming อ้างอิงจาก http://apollo.lsc.vsc.edu Computer Science Department (FLAS-KU) - Prasertsak U.
What is shell Script? • "Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file." Computer Science Department (FLAS-KU) - Prasertsak U.
Why to write shell script? • Useful to create our own commands. • Save lots of time. • To automate some task of day today life. • System Administration part can be also automated. Computer Science Department (FLAS-KU) - Prasertsak U.
How to write shell script? • Use any editor like vi to write shell script. • After writing shell script set execute permission for your script Examples:$ chmod +x your-script-name$ chmod 755 your-script-name Computer Science Department (FLAS-KU) - Prasertsak U.
How to execute your script? • Execute your script assyntax: bash your-script-namesh your-script-name./your-script-name . your-script-name (run on current shell without copy of shell) Examples:$ bash script-name$ sh script-name$ ./script-name Computer Science Department (FLAS-KU) - Prasertsak U.
Very Simple Script • Traditional Hello World script #!/bin/bash echo Hello World If you get something like ./hello.sh: Command not found. Probably the first line '#!/bin/bash' is wrong, issue whereis bash to find the correct path. Computer Science Department (FLAS-KU) - Prasertsak U.
Very Simple Backup Script • A simple Backup your home directory #!/bin/bash tar -cZf /var/my-backup.tgz /home/me/ Computer Science Department (FLAS-KU) - Prasertsak U.
Redirection • stderr, stdout, stdin • redirect stdout to a file • redirect stderr to a file • redirect stdout to a stderr • redirect stderr to a stdout • redirect stderr and stdout to a file • redirect stderr and stdout to stdout • redirect stderr and stdout to stderr • 1 = stdout • 2 = stderr Computer Science Department (FLAS-KU) - Prasertsak U.
Sample: redirect stdout to file • จะนำผลลัพธ์ที่ได้จากคำสั่ง ls –l เก็บไว้ในแฟ้มชื่อ ls-out.txt โดยไม่ปรากฎผลลัพธ์บนจอภาพ ls –l > ls-out.txt ls –l 1> ls-out.txt Computer Science Department (FLAS-KU) - Prasertsak U.
Sample: redirect stderr to file • จะนำข้อความแสดงความผิดพลาดที่เกิดขึ้น เก็บลงแฟ้มชื่อว่า grep-err.txt โดยข้อความแสดงความผิดพลาดที่เกิดขึ้น จะไม่ปรากฏบนจอภาพ grep xx * 2> grep-err.txt Computer Science Department (FLAS-KU) - Prasertsak U.
Sample: redirect stdout to stderr • ผลลัพธ์ที่ปรากฏผ่าน stdout จะย้ายไปปรากฏผ่านช่องทาง stderr แทน grep xx * 1>&2 Computer Science Department (FLAS-KU) - Prasertsak U.
Sample: redirect stderr to stdout • ผลลัพธ์ที่ปรากฏในช่องทาง stderr จะย้ายไปปรากฏผ่านช่องทาง stdoutแทน grep xx * 2>&1 Computer Science Department (FLAS-KU) - Prasertsak U.
Sample: redirect stderr and stdout to file • ผลลัพธ์ที่ปรากฏในช่องทาง stderr และ stdout จะถูกเก็บไว้ในแฟ้มข้อมูลแทนช่องทางเดิม แต่ในที่นี้ การอ้างอิงแฟ้ม /dev/null หมายถึง ให้ทิ้งค่าผลลัพธ์ที่ปรากฏทั้งหมด rm -f $(find / -name core) &> /dev/null Computer Science Department (FLAS-KU) - Prasertsak U.
Different between symbols >,>> • > Redirector Symbol, it will be overwritten else new file is created. • >> Redirector Symbol, it will be opened and new information/data will be written to END of file, without losing previous information/data, And if file is not exist, then new file is created. Computer Science Department (FLAS-KU) - Prasertsak U.
< Redirector Symbol • Syntax:command < filenameTo take input to command from file instead of key-board $sort < sname > sorted_names $ tr "[a-z]" "[A-Z]" < sname > cap_names$ cat cap_namesVIVEKASHISHZEBRABABU Computer Science Department (FLAS-KU) - Prasertsak U.
Pipes • Pipes let you use the output of a program as the input of another one ls -l | sed -e "s/[:]/./g" ls -l | grep “\.txt$” Computer Science Department (FLAS-KU) - Prasertsak U.
What is Processes? • "A process is program (command given by user) to perform specific Job. when you start process, it gives a number to process (called PID or process-id)” Computer Science Department (FLAS-KU) - Prasertsak U.
Shell Arithmetric • Use to perform arithmetic operations. Syntax:expr <op1> math-operator <op2>Examples: $ expr 1 + 3$ expr 2 - 1$ expr 10 / 2$ expr 20 % 3$ expr 10 \* 3$ echo `expr 6 + 3` Back quote is generally found on the key under tilde (~) on PC keyboard Computer Science Department (FLAS-KU) - Prasertsak U.
More About Quotes Computer Science Department (FLAS-KU) - Prasertsak U.
The Single Quote • สมมติเรามีแฟ้มชื่อว่า phonebook มีรายละเอียดดังนี้ $ cat phonebook Alice Chebba 973-555-2015 Barbara Swingle 201-555-9257 Liz Stachiw 212-555-2298 Susan Goldberg 201-555-7776 Susan Topple 212-555-4932 Tony Iannino 973-555-1295 Computer Science Department (FLAS-KU) - Prasertsak U.
The Single Quote • เมื่อใช้คำสั่ง grep $ grep Alice phonebook Alice Chebba 973-555-2015 $ grep Susan phonebook Susan Goldberg 201-555-7776 Susan Topple 212-555-4932 Computer Science Department (FLAS-KU) - Prasertsak U.
The Single Quote • เมื่อใช้คำสั่ง grep $ grep Susan Goldberg phonebook grep: can't open Goldberg Susan Goldberg 201-555-7776 Susan Topple 212-555-4932 Computer Science Department (FLAS-KU) - Prasertsak U.
The Single Quote • เมื่อใช้คำสั่ง grep $ grep ‘Susan Goldberg’ phonebook Susan Goldberg 201-555-7776 Computer Science Department (FLAS-KU) - Prasertsak U.
The Single Quote • เมื่อใช้คำสั่ง echo $ echo one two three four one two three four $ echo 'one two three four' one two three four Computer Science Department (FLAS-KU) - Prasertsak U.
The Single Quote • เมื่อใช้คำสั่ง echo $ file=/users/steve/bin/prog1 $ echo $file /users/steve/bin/progl $ echo '$file' $ not interpreted $file $ echo '*' * $ echo '< > | ; ( ) { } >> " ` &' < > | ; ( ) { } >> " ` & Computer Science Department (FLAS-KU) - Prasertsak U.
The Double Quote • พิจารณาตัวอย่าง $ x=* $ echo $x addresses intro lotsaspaces names nu numbers phonebook stat $ echo '$x' $x $ echo "$x" * Computer Science Department (FLAS-KU) - Prasertsak U.
Command Substitution • `command` $ echo The date and time is: `date` The date and time is: Wed Aug 28 14:28:43 EDT 2002 $ echo Your current working directory is `pwd` Your current working directory is /users/steve/shell/ch6 Computer Science Department (FLAS-KU) - Prasertsak U.
Command Substitution • The $(...) Construct $ echo The date and time is: $(date) The date and time is: Wed Aug 28 14:28:43 EDT 2002 $ echo There are $(who | wc –l) users logged in There are 13 users logged in Computer Science Department (FLAS-KU) - Prasertsak U.
Variables • There are no data types. A variable in bash can contain a number, a character, a string of characters. • no need to declare a variable, just assigning a value to its reference will create it. Computer Science Department (FLAS-KU) - Prasertsak U.
Sample: Shell Script with variable • Traditional Hello World script with variable • Backup Script with Variable #!/bin/bash STR="Hello World!" echo $STR #!/bin/bash OF=/var/my-backup-$(date +%Y%m%d).tgz tar -cZf $OF /home/me/ Computer Science Department (FLAS-KU) - Prasertsak U.
Local Variables • Local variables can be created by using the keyword local. #!/bin/bash HELLO=Hello function hello { local HELLO=World echo $HELLO } echo $HELLO hello echo $HELLO Hello World Hello Computer Science Department (FLAS-KU) - Prasertsak U.
How to define User defined variable • To define UDV use following syntaxSyntax: <variable name>=<value> Example:$ no=10 # this is ok$ 10=no # Error, NOT Ok, Value must be on right side of = sign. Computer Science Department (FLAS-KU) - Prasertsak U.
Rules for Naming variable name • Variable name must begin with Alphanumeric character or underscore character (_) • Don't put spaces on either side of the equal sign when assigning value to variable $ no=10But there will be problem for any of the following variable declaration:$ no =10$ no= 10$ no = 10 Computer Science Department (FLAS-KU) - Prasertsak U.
Rules for Naming variable name • Variables are case-sensitive $ no=10$ No=11$ NO=20$ nO=2Above all are different variable name$ echo $no # will print 10 but not 20$ echo $No # will print 11 but not 20$ echo $nO # will print 2 but not 20 Computer Science Department (FLAS-KU) - Prasertsak U.
Rules for Naming variable name • You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) • Do not use ?,* etc, to name your variable names. $ result=$ result="" Computer Science Department (FLAS-KU) - Prasertsak U.
Sample $ i=1 $ expr $i + 1 2 $ i=1 $ i=$(expr $i + 1) Add 1 to i $ echo $i 2 $ i=`expr $i + 1` Add 1 to i $ echo $i 3 Computer Science Department (FLAS-KU) - Prasertsak U.