Bai tap voi shell script nhom10

5 486 0
Bai tap voi shell script nhom10

Đang tải... (xem toàn văn)

Thông tin tài liệu

sourLab 4: Working With Shell Script Goal: In this lab, you will learn how to write a shell script Introduction to scripting Write a script that outputs the name of a city $ echo 'echo Antwerp' > first.bash $ chmod +x first.bash $ /first.bash Antwerp Make sure the script runs in the bash shell $ cat first.bash #!/bin/bash echo Antwerp Make sure the script runs in the Korn shell $ cat first.bash #!/bin/ksh echo Antwerp Note that while first.bash will technically work as a Korn shell script, the name ending in bash is confusing Create a script that defines two variables, and outputs their value $ cat second.bash #!/bin/bash var33=300 var42=400 echo $var33 $var42 The previous script does not influence your current shell (the variables not exist outside of the script) Now run the script so that it influences your current shell source second.bash Is there a shorter way to source the script ? /second.bash Comment your scripts so that you know what they are doing $ cat second.bash #!/bin/bash # script to test variables and sourcing # define two variables var33=300 var42=400 # output the value of these variables echo $var33 $var42 Scripting tests and loops Write a script that uses a for loop to count from to #!/bin/bash for i in echo Counting from to 7, now at $i done Write a script that uses a for loop to count from to 17000 #!/bin/bash for i in `seq 17000` echo Counting from to 17000, now at $i done Write a script that uses a while loop to count from to #!/bin/bash i=3 while [ $i -le ] echo Counting from to 7, now at $i let i=i+1 done Write a script that uses an until loop to count down from to #!/bin/bash i=8 until [ $i -lt ] echo Counting down from to 4, now at $i let i=i-1 done Write a script that counts the number of files ending in txt in the current directory #!/bin/bash let i=0 for file in *.txt let i++ done echo "There are $i files ending in txt" Wrap an if statement around the script so it is also correct when there are zero files ending in txt #!/bin/bash ls *.txt > /dev/null 2>&1 if [ $? -ne ] then echo "There are files ending in txt" else let i=0 for file in *.txt let i++ done echo "There are $i files ending in txt" fi Parameters and options Write a script that receives four parameters, and outputs them in reverse order echo $4 $3 $2 $1 Write a script that receives two parameters (two filenames) and outputs whether those files exist #!/bin/bash if [ -f $1 ] then echo $1 else echo $1 fi if [ -f $2 ] then echo $2 else echo $2 fi exists! not found! exists! not found! Write a script that asks for a filename Verify existence of the file, then verify that you own the file, and whether it is writable If not, then make it writable Make a configuration file for the previous script Put a logging switch in the config file, logging means writing detailed output of everything the script does to a log file in /tmp More scripting Write a script that asks for two numbers, and outputs the sum and product (as shown here) Enter a number: Enter another number: Sum: + = Product: x = 10 #!/bin/bash echo -n "Enter a number : " read n1 echo -n "Enter another number : " read n2 let sum="$n1+$n2" let pro="$n1*$n2" echo -e "Sum\t: $n1 + $n2 = $sum" echo -e "Product\t: $n1 * $n2 = $pro" Improve the previous script to test that the numbers are between and 100, exit with an error if necessary echo read if [ then echo exit fi -n "Enter a number between and 100 : " n1 $n1 -lt -o $n1 -gt 100 ] Wrong number Improve the previous script to congratulate the user if the sum equals the product if [ $sum -eq $pro ] then echo Congratulations $sum == $pro fi Write a script with a case insensitive case statement, using the shopt nocasematch option The nocasematch option is reset to the value it had before the scripts started #!/bin/bash # # Wild Animals Case Insensitive Helpdesk Advice # if shopt -q nocasematch; then nocase=yes; else nocase=no; shopt -s nocasematch; fi echo -n "What animal did you see ? " read animal case $animal in "lion" | "tiger") echo "You better start running fast!" ;; "cat") echo "Let that mouse go " ;; "dog") echo "Don't worry, give it a cookie." ;; "chicken" | "goose" | "duck" ) echo "Eggs for breakfast!" ;; "liger") echo "Approach and say 'Ah you big fluffy kitty.'" ;; "babelfish") echo "Did it fall out your ear ?" ;; *) echo "You discovered an unknown animal, name it!" ;; esac if [ nocase = yes ] ; then shopt -s nocasematch; else shopt -u nocasematch; fi Scoring • Part - 25% • Part - 25% • Part - 25% • Part - 25% ...5 The previous script does not influence your current shell (the variables not exist outside of the script) Now run the script so that it influences your current shell source second.bash... for the previous script Put a logging switch in the config file, logging means writing detailed output of everything the script does to a log file in /tmp More scripting Write a script that asks... variables echo $var33 $var42 Scripting tests and loops Write a script that uses a for loop to count from to #!/bin/bash for i in echo Counting from to 7, now at $i done Write a script that uses a for

Ngày đăng: 13/03/2016, 23:35

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan