Shellscript script Learning

Source: Internet
Author: User
Tags date1 touch command

Shellscript script Learning

[Root @ fwq prac] # cat sh01.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHecho-e "Hello World! \ A \ n "exit 0 ==================================== ================================ [root @ fwq prac] # cat sh02.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHread-p "Please input your firstname:" firstnameread-p "Please input your lastname: "lastnameecho-e" \ n Your Full name is $ firstname $ lastname "================== ================================ [root @ fwq prac] # cat sh03.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHecho-e "I will use touch command to create 3 files. "read-p" Please input your filename: "fileuserfilename =$ {fileuser: -"filename"} date1 = $ (date -- date = '2 days ago '+ % Y % m % d) date2 = $ (date -- date = '1 days ago '+ % Y % m % d) date3 = $ (date + % Y % m % d) file1 =$ {filename }$ {date1} file2 =2 {filename }$ {date2} file3 =3 {filename }$ {date3} touch "$ file1" touch "$ file2" touch "$ file3" ====================================== ================================ [Root @ fwq prac] # cat sh04.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHecho-e "Your shoshould input 2 number, I will cross them! \ N "read-p" first number: "firstnuread-p" second number: "secnutotal = $ ($ firstnu * $ secnu )) echo-e "\ n The result of $ firstnu X $ secnu is ==>$ total" ================ ============================================ [root @ fwq prac] # cat sh05.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Binexport PATHecho-e "Please input a filename, I will check the filename's type and permissions. \ n "read-p" Input a filename: "filenametest-z $ filename & echo" You Must input a filename. "& exit 0 test! -E $ filename & echo "The filename '$ filename 'do not exist" & exit 0 test-f $ filename & filetype = "regular file" tset-d $ filename & filetype = "directory" test-r $ filename & perm = "readable" test-w $ filename & perm = "perm writable" test-x $ filename & perm = "perm executable" echo "The filename: "$ filename is a $ filetypeecho" And the permissions are: "$ perm ========================================== ==== ================ [Root @ fwq prac] # cat sh06.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Binexport PATHread-p "Please input (y/n ): "yn [" $ yn "=" y "-o" $ yn "=" Y "] & echo" OK, continue "& exit 0 [" $ yn "=" N "-o" $ yn "=" n "] & echo" Oh, interrupt "& exit 0 echo" I don't know what your choice is. "& exit 0 ======================================== ================================ [root @ fwq prac] # cat sh06-2.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Binexport PATHread-p "Please input (y/n ): "ynif [" $ yn "=" y "] | [" $ yn "=" Y "]; thenecho" OK, continue "exit 0 fiif [" $ yn "=" N "] | [" $ yn "=" n "]; thenecho" Oh, interrupt "exit 0 fiecho" I don't know what your choice is. "& exit 0 ======================================== ================================ [root @ fwq prac] # cat sh06-3.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Binexport PATHread-p "Please input (y/n ): "ynif [" $ yn "=" y "] | [" $ yn "=" Y "]; thenecho" OK, continue "elif [" $ yn "=" N "] | [" $ yn "=" n "]; thenecho" Oh, interrupt "elseecho" I don't know what your choice is. "& exit 0fi ======================================== ============================== [root @ fwq prac] # cat sh07.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHecho "The Script name is ==>0 0" echo "Total parameter number is ==>$ #" ["$ #"-lt 2] & echo" number of parameter is less than 2. stop here. "\ & exit 0 echo" Your parameter is ==> '$ @' "echo" The 1st para is ==>1 1 "echo" The 2rd para is ==> $2 "========================================== ======================== [root @ fwq prac] # cat sh08.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHecho "Total parameter number is ==>$ #" echo "Your whole parameter is ==> '$ @'" shiftecho "Total parameter number is ==>$ #" echo "Your whole parameter is ==>' $ @ '" shift 3 echo "Total parameter number is ==>$ #" echo "Your whole parameter is ==>' $ @ '"============================================== ====================== [root @ fwq prac] # cat sh09.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Binexport patlif ["$1" = "hello"]; thenecho "Hello, how are you? "Elif [" $1 "=" "]; thenecho" You must input parameters, ex> {$0 someword} "elseecho" The Only parameter is 'hello ', ex> {$0 hello} "fi ================================== ================ [root @ fwq prac] # cat sh09-2.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Binexport PATHcase $1 in "hello") echo "Hello, how are you? ";" ") Echo" You must input parameters, ex >{$ 0 someword} "; *) echo" Usage $0 {hello }";; esac = ==================== [root @ fwq prac] # cat sh10.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHecho "Now, I will detect your linux server's service" echo-e "the WWW, FTP, SSH, and MAIL will be detect "testing = $ (netstat-tuln | grep": 80 ") if [" $ testing "! = ""]; Thenecho "WWW is running in your system." fitesting = $ (netstat-tuln | grep ": 22") if ["$ testing "! = ""]; Thenecho "SSH is running in your system." fitesting = $ (netstat-tuln | grep ": 21") if ["$ testing "! = ""]; Thenecho "FTP is running in your system." fitesting = $ (netstat-tuln | grep ": 25") if ["$ testing "! = ""]; Thenecho "MAIl is running in your system. "fi ================================================ ====================== [root @ fwq prac] # cat sh11.sh #! /Bin/bash # Program: # Tring to calculate your demo-ization date at how many days # later... # History: PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATH #1. Inform the user of the purpose of this program and how to enter the date format? Echo "This program will try to calculate:" echo "How many days about your demo-ization date... "read-p" Please input your demo-ization date (YYYYMMDD ex> 20050401): "date2 #2. test whether the input content is correct? Use regular notation ~ Date_d = 'echo $ date2 | grep '[0-9] \ {8 \} ''if [" $ date_d "=" "]; thenecho "You input the wrong format of date .... "exit 1fi #3. start date calculation ~ Declare-I date_dem = 'date -- date = "$ date2" + % s' declare-I date_now = 'date + % s' declare-I date_total_s = $ ($ date_dem- $ date_now )) declare-I date_d =$ ($ date_total_s/60/60/24) if ["$ date_total_s"-lt "0"]; thenecho "You had been demo-ization before: "$ (-1 * $ date_d)" ago "elsedeclare-I date_h = $ ($ date_total_s-$ date_d * 60*60*24 )) /60/60) echo "You will be demo-ized after $ date_d days and $ date_h h Ours. "fi ================================================ ====================== [root @ fwq prac] # cat sh12.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHecho "This program will print your selection! "Read-p" Input your choice: "choicecase $ choice in" one ") echo" Your choice is one ";" two ") echo" your choice is two ";; "three") echo "your choice is three"; *) echo "usage $0 {one | two | three }";; esac = ======================= [root @ fwq prac] # cat sh12-2.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHfunction printit () {echo-n "Your choice is"} echo "This program will print your selection! "Case $1 in" one ") printit; echo $1 | tr 'a-z'' a-Z';" two ") printit; echo $1 | tr 'a-z'' a-Z'; "three") printit; echo $1 | tr 'a-z'' A-Z ';; *) echo "usage $0 {one | two | three }";; esac = ======================= [root @ fwq prac] # cat sh12-3.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHfunction printit () {echo "Your choice is $1"} echo "This program will print your selection! "Case $1 in" one ") printit 1;" two ") printit 2;" three ") printit 3 ;;*) echo "usage $0 {one | two | three }";; esac = ==================== [root @ fwq prac] # cat sh13.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHwhile ["$ yn "! = "Yes"-a "$ yn "! = "YES"] doread-p "Please input yes/YES to stop this program:" yndoneecho "OK! You input the correct answer. "============================================== ======================= [root @ fwq prac] # cat sh13-2.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHuntil ["$ yn" = "yes"-o "$ yn" = "YES"] doread-p "Please input yes/YES to stop this program: "yndoneecho" OK! You input the correct answer. "============================================== ==================== [root @ fwq prac] # cat sh14.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHs = 0i = 0 while [$ I! = 100] doi = $ ($ I + 1) s = $ ($ s + $ I )) doneecho "The result of 1 + 2 + 3 ...... + 100 = "$ s ====================================== ================================ [root @ fwq prac] # cat sh15.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHfor animal in dog cat elephantdoecho "there are $ {animal} s... "done ============================================ ====================== [root @ fwq prac] # cat sh16.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHusers = $ (cut-d': '-f1/etc/passwd) for username in $ usersdoid $ usernamefinger $ usernamedone ========================== ================================== [root @ fwq prac] # cat sh17.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ // Binexport PATHnetwork = "10.6.7" for sitenum in $ (seq 1 100) doping-c 1-w 1 $ {network }. $ {sitenum} &>/dev/null & result = 0 | result = 1if ["$ result" = 0]; thenecho "Server $ {network }. $ {sitenum} is up. "elseecho" Server $ {network }. $ {sitenum} is down. "fidone ============================================ ====================== [root @ fwq prac] # cat sh18.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHread-p "Please input a directory:" dirif ["$ dir" = ""-o! -D "$ dir"]; thenecho "The $ dir is NOT exist in your system. "exit 1 using ilelist =$ (ls $ dir) for filename in $ filelistdoperm = "" test-r "$ dir/$ filename" & perm = "$ perm readable" test-w "$ dir/$ filename" & perm = "$ perm writable" test-x "$ dir/$ filename" & perm = "$ perm executable" echo "The file $ dir/$ filename's permission is $ perm "done ============================================ ========================= [root @ fwq prac] # Cat sh19.sh #! /Bin/bashPATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Binexport PATHread-p "Please input a number, I will count for 1 + 2 +... your input: "nus = 0for (I = 1; I <$ nu; I = I + 1) dos =$ ($ s + $ I )) doneecho "The result of 1 + 2 + 3 +... + $ nu = "$ s ==================================== ======================================

 

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.