1 / 38

Unix Operating System

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. ".

nyla
Télécharger la présentation

Unix Operating System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unix Operating System Shell Script Programming อ้างอิงจาก http://apollo.lsc.vsc.edu Computer Science Department (FLAS-KU) - Prasertsak U.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. Sample: redirect stderr to file • จะนำข้อความแสดงความผิดพลาดที่เกิดขึ้น เก็บลงแฟ้มชื่อว่า grep-err.txt โดยข้อความแสดงความผิดพลาดที่เกิดขึ้น จะไม่ปรากฏบนจอภาพ grep xx * 2> grep-err.txt Computer Science Department (FLAS-KU) - Prasertsak U.

  11. Sample: redirect stdout to stderr • ผลลัพธ์ที่ปรากฏผ่าน stdout จะย้ายไปปรากฏผ่านช่องทาง stderr แทน grep xx * 1>&2 Computer Science Department (FLAS-KU) - Prasertsak U.

  12. Sample: redirect stderr to stdout • ผลลัพธ์ที่ปรากฏในช่องทาง stderr จะย้ายไปปรากฏผ่านช่องทาง stdoutแทน grep xx * 2>&1 Computer Science Department (FLAS-KU) - Prasertsak U.

  13. 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.

  14. 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.

  15. < 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.

  16. 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.

  17. Sample: Pipes

  18. 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.

  19. A Command Related with Process

  20. 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.

  21. More About Quotes Computer Science Department (FLAS-KU) - Prasertsak U.

  22. 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.

  23. 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.

  24. 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.

  25. The Single Quote • เมื่อใช้คำสั่ง grep $ grep ‘Susan Goldberg’ phonebook Susan Goldberg 201-555-7776 Computer Science Department (FLAS-KU) - Prasertsak U.

  26. 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.

  27. 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.

  28. 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.

  29. 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.

  30. 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.

  31. 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.

  32. 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.

  33. 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.

  34. 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.

  35. 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.

  36. 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.

  37. 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.

  38. 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.

More Related