200 likes | 628 Views
Lab #2 Shell Script. Operating System Lab. What is Shell Script?. It is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. Example #1. #!/bin/bash
E N D
Lab #2 Shell Script Operating System Lab
What is Shell Script? • It is often considered a simple domain-specific programming language. • Typical operations performed by shell scripts include file manipulation, program execution, and printing text. NCHU System & Network Lab
Example #1 #!/bin/bash #use to print out “Hello ! How are you?” #2007/09 hello=Hello\!\ How\ are\ you\? echo $hello Output: [root@localhost~ ]#sh eample1.sh Hello! How are you? NCHU System & Network Lab
Example #2: echo #!/bin/bash #use to differentiate “ and ‘ #2007/09 name=“oslab” name1=“test is $name” name2=‘test is $name’ echo $name echo $name1 echo $name2 Output: [root@localhost~ ]#sh eample2.sh oslab test is oslab test is $name NCHU System & Network Lab
Example #3: declare(1/2) • declare –afir • a for array • f for function • i for integer • r for read only NCHU System & Network Lab
Example #3: declare(2/2) #!/bin/bash #use to learn “declare” #2007/09 number1=2*3+5*13-32+25 declare –i number2=2*3+5*13-32+25 echo ”answer is $number1” echo ”answer is $number2” Output: [root@localhost~ ]#sh eample3.sh answer is 2*3+5*13-32+25 answer is 64 NCHU System & Network Lab
Example #4: read #!/bin/bash #use to learn “read” #2007/09 echo “keyin your name and Enter to start” read name echo “This is the data you keyin=>$name” Output: [root@localhost~ ]#sh eample4.sh keyin your name and Enter to start oslab This is the data you keyin=>oslab NCHU System & Network Lab
Example #5: If…then…elseif…then…endif(1/3) Syntax: If[ … ] &&(||) [ … ]; then …… elif[ … ] &&(||) [ … ]; then …… else …… fi #!/bin/bash #use to learn “if …then…fi” #2007/09 echo “press y to continue” read yy if[ “$yy”=“y” ]; then echo “continue…” else echo”stop…” fi Output: [root@localhost~ ]#sh eample5.sh press y to continue y continue… NCHU System & Network Lab
Example #5: Exercise(2/3) • When we keyin “y”, the script will continue. • But when we keyin ”Y”, the script will stop. • How to improve the script that let this script print “continue…” when we keyin “Y” or “y”. NCHU System & Network Lab
Example #6: parameter(1/3) #!/bin/bash #use to define parameter #2007/09 echo “This script’s name => $0” echo “parameters $1 $2 $3” Output: [root@localhost~ ]#sh example6.sh num1 num2 num3 This script’s name => example6 arameters num1 num2 num3 NCHU System & Network Lab
Example #6: Exercise(2/3) • Use example #5 and example #6 • Keyin “hello” then print “Hello! how are you?” • If keyin “null”, print “please input parameters” • If keyin any words except “hello”, print “only accept parameter is hello” NCHU System & Network Lab
Example #7: case(1/5) • Direction and interaction • Direction uses parameters that are written in a script. • Interaction lets user keyin the parameters. NCHU System & Network Lab
Example #7: case(2/5) #!/bin/bash #using case mode:direction #2007/09 echo “this script will print your selection” case $1 in one) echo “your choice is one” ;; two) echo “your choice is two” ;; three) echo “your choice is three” ;; *) echo “keyin parameters: one | two | three” esac • Syntax: case (type) in type1) …... ;; type2) …... ;; type3) …... ;; . . . *) esca NCHU System & Network Lab
Example #7: case(3/5) Output: [root@localhost~ ]#sh example7.sh this script will print your selection keyin parameters: one | two | three [root@localhost~ ]#sh example7.sh three this script will print your selection your choice is three NCHU System & Network Lab
Example #7: Exercise(4/5) • How to use interaction? • This script will ask you enter a selection form keyboard • Example: press your selection one, two, or three. two your choice is two NCHU System & Network Lab
#!/bin/bash #using for and loop 1+2+3+…+100 #2007/09 declare –i i declare –i s for ((i=1; i<=100; i++)) do s=s+i done while [ “$i” != “101”] do s=s+i i=i+1 done echo “the count is $s” until [“$i” = “101”] do s=s+i i=i+1 done Example #8: for and loop Output: [root@localhost~ ]#sh example8.sh The count is 5050 NCHU System & Network Lab
Final Exercise • User management • “adduser” command allows us to create a new user • Please write a shell script to accomplish the user creation task interactively. • Let’s say that the executable file is named myadduser. NCHU System & Network Lab
myadduser • The user adding procedure includes • (i) a user adding prompt (such as “adding a new user….” ), • (ii) user’s login, group, password, home directory, and login shell for establishing the necessary attributes for the new user, and • (iii) a message of whether continue to add more users or not (such as “adding more users (y/n)?”). • Note that the password has to be typed twice and be verified for its coincidence. • Running myadduser should produce the following interactive outcome: NCHU System & Network Lab
Reference • Wikipedia • http://en.wikipedia.org/wiki/Shell_script • 鳥哥的 Linux 私房菜 • http://linux.vbird.org/ • Neil Matthew, Richard Stones, “Beginning Linux Programming,2nd Edition ”, Wrox, 2002 NCHU System & Network Lab