Basic Shell script operation exercises

Source: Internet
Author: User
Shell script basic operation exercises here are mainly familiar with shell basic operations, including variable value reference modification, function usage, signal acquisition and some judgment methods. for details, see the code :#! /Bin/shstr & quot; HelloWorld! & Quot; echo & quot; $ {str} aha & quot; # & amp; #39; & amp; #39; echo & am Shell script basic operation exercises here are mainly familiar with shell basic operations, including variable assignment reference modification, function usage, signal acquisition and some judgment methods, etc, for details, see the code :#! /Bin/sh str = "Hello World! "Echo" $ {str} aha "# ''echo '$ str' # file nameecho" the name of this shell script is $0 "# first parameter, $1 --- $ Necho "the first parameter is $1" # all of the parameterecho "all of the parameter is $ *" # num of the parameterecho "the num of the parameter is $ # "# PID of this processecho" this running pid is $ "# the return value of the front one echo" return value of the front one is $? "# The reference of the return valecho" the current directory is "'pwd' #-eq = str = 5str2 = 5if [" $ {str} "-eq" ${str2} "]; then echo "$ str equal $ str2" else echo "no equal" fi #-ge> = str = 5str2 = 10if ["$ str2"-ge "$ str"]; then echo "$ str2 >=$ str" else echo "no ge" fi #-gt >;- le <=;-ne! === ;! =! =; # Str no nullif ["$ str"]; then echo "$ str is not null" else echo "null" fi #-n str len of str> 0if [-n "$ str"]; then echo "$ str len> 0" else echo "len <0" fi #-z str len of str = 0 # dirfilename = ". /test_dir "if [-d $ filename]; then echo" $ filename is dir "else echo" $ filename is not dir "fi # filefilename = ". /test_dir "if [-f $ filename]; then echo" $ filename is file "else echo" $ filename is not file "fi #-r file: is read #-s file: len of file> 0 #-w file: is write #-x file: is run # case switchstr = 5 case $ str in1) echo "$ str is 1"; 2) echo "$ str is 2"; 5) echo "$ str is 5 ";;*) echo "unknow val" esac # funadd () {local temp; temp = 'expr $1 + $ 2' sum = 'expr $ temp + $ 3' return $ sum} # more information about expr, access http://www.cnblogs.com/kelin1314/archive/2011/01/11/1933019.html Echo "we will use function add 3 + 8 + 10" add 3 8 10 echo "the sum val is $? "# Trap: catch the signal (sighup sigint sigquit sigkill sigterm) trap catch_signal INTcatch_signal () {echo" I catch the SIGINT !!! "Sleep 1} for I in 6 5 4 3 2 1; doecho" $ I seconds until system failure, you can use CTRL + C ~ "Sleep 1 doneecho" system failure "run the following results: tiger @ ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/SHELL $./test_shell.sh Hello World! Aha $ strthe name of this shell script is. /test_shell.shthe first parameter is all of the parameter is the num of the parameter is 0 this running pid is 7736 return value of the front one is 0the current directory is/mnt/hgfs/e/ lessons/MyExercise/UtilLibs/SHELL5 equal 510> = 55 is not null5 len> 0. /test_dir is dir. /test_dir is not file5 is 5we will use function add 3 + 8 + 10the sum val is 216 seconds u Ntil system failure, you can use CTRL + C ~ 5 seconds until system failure, you can use CTRL + C ~ ^ CI catch the SIGINT !!! 4 seconds until system failure, you can use CTRL + C ~ ^ CI catch the SIGINT !!! 3 seconds until system failure, you can use CTRL + C ~ ^ CI catch the SIGINT !!! 2 seconds until system failure, you can use CTRL + C ~ 1 seconds until system failure, you can use CTRL + C ~ System failuretiger @ ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/SHELL $
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.