Script learning for laruence's private dish shellscript
For the convenience of contact, the script of shell script is removed from all comments.
[Root @ fwq prac] # cat sh01.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo-e "Hello World! \ A \ n"
Exit 0
========================================================== ======================
[Root @ fwq prac] # cat sh02.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Read-p "Please input your firstname:" firstname
Read-p "Please input your lastname:" lastname
Echo-e "\ n Your Full name is $ firstname $ lastname"
========================================================== ======================
[Root @ fwq prac] # cat sh03.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo-e "I will use touch command to create 3 files ."
Read-p "Please input your filename:" fileuser
Filename =$ {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 =$ {filename }$ {date2}
File3 =$ {filename }$ {date3}
Touch "$ file1"
Touch "$ file2"
Touch "$ file3"
========================================================== ======================
[Root @ fwq prac] # cat sh04.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo-e "Your shocould input 2 number, I will cross them! \ N"
Read-p "first number:" firstnu
Read-p "second number:" secnu
Total = $ ($ firstnu * $ secnu ))
Echo-e "\ n The result of $ firstnu X $ secnu is ==> $ total"
========================================================== ======================
[Root @ fwq prac] # cat sh05.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Bin
Export PATH
Echo-e "Please input a filename, I will check the filename's type and permissions. \ n"
Read-p "Input a filename:" filename
Test-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 $ filetype
Echo "And the permissions are:" $ perm
========================================================== ======================
[Root @ fwq prac] # cat sh06.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Bin
Export PATH
Read-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
========================================================== ======================
# Cat sh06-2.sh [root @ fwq prac] #
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Bin
Export PATH
Read-p "Please input (y/n):" yn
If ["$ yn" = "y"] | ["$ yn" = "Y"]; then
Echo "OK, continue"
Exit 0
Fi
If ["$ yn" = "N"] | ["$ yn" = "n"]; then
Echo "Oh, interrupt"
Exit 0
Fi
Echo "I don't know what your choice is." & exit 0
========================================================== ======================
# Cat sh06-3.sh [root @ fwq prac] #
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Bin
Export PATH
Read-p "Please input (y/n):" yn
If ["$ yn" = "y"] | ["$ yn" = "Y"]; then
Echo "OK, continue"
Elif ["$ yn" = "N"] | ["$ yn" = "n"]; then
Echo "Oh, interrupt"
Else
Echo "I don't know what your choice is." & exit 0
Fi
========================================================== ======================
[Root @ fwq prac] # cat sh07.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo "The Script name is ==>$ 0"
Echo "Total parameter number is ==>$ #"
["$ #"-Lt 2] & echo "The number of parameter is less than 2. stop here ."\
& Exit 0
Echo "Your parameter is ==> '$ @'"
Echo "The 1st para is ==>$ 1"
Echo "The 2rd para is ==>$ 2"
========================================================== ======================
[Root @ fwq prac] # cat sh08.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo "Total parameter number is ==>$ #"
Echo "Your whole parameter is ==> '$ @'"
Shift
Echo "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/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Bin
Export PATH
If ["$1" = "hello"]; then
Echo "Hello, how are you? "
Elif ["$1" = ""]; then
Echo "You must input parameters, ex> {$0 someword }"
Else
Echo "The Only parameter is 'hello', ex> {$0 hello }"
Fi
========================================================== ======================
# Cat sh09-2.sh [root @ fwq prac] #
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :/~ /Bin
Export PATH
Case $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/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo "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 "! = ""]; Then
Echo "WWW is running in your system ."
Fi
Testing = $ (netstat-tuln | grep ": 22 ")
If ["$ testing "! = ""]; Then
Echo "SSH is running in your system ."
Fi
Testing = $ (netstat-tuln | grep ": 21 ")
If ["$ testing "! = ""]; Then
Echo "FTP is running in your system ."
Fi
Testing = $ (netstat-tuln | grep ": 25 ")
If ["$ testing "! = ""]; Then
Echo "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 :~ /Bin
Export 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" = ""]; then
Echo "You input the wrong format of date ...."
Exit 1
Fi
#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"]; then
Echo "You had been demo-ization before:" $(-1 * $ date_d) "ago"
Else
Declare-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 hours ."
Fi
========================================================== ======================
[Root @ fwq prac] # cat sh12.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo "This program will print your selection! "
Read-p "Input your choice:" choice
Case $ 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
========================================================== ======================
# Cat sh12-2.sh [root @ fwq prac] #
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Function 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
========================================================== ======================
# Cat sh12-3.sh [root @ fwq prac] #
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Function 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/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
While ["$ yn "! = "Yes"-a "$ yn "! = "YES"]
Do
Read-p "Please input yes/YES to stop this program:" yn
Done
Echo "OK! You input the correct answer ."
========================================================== ======================
# Cat sh13-2.sh [root @ fwq prac] #
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Until ["$ yn" = "yes"-o "$ yn" = "YES"]
Do
Read-p "Please input yes/YES to stop this program:" yn
Done
Echo "OK! You input the correct answer ."
========================================================== ======================
[Root @ fwq prac] # cat sh14.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
S = 0
I = 0
While [$ I! = 100]
Do
I = $ ($ I + 1 ))
S = $ ($ s + $ I ))
Done
Echo "The result of 1 + 2 + 3 ...... + 100 =" $ s
========================================================== ======================
[Root @ fwq prac] # cat sh15.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
For animal in dog cat elephant
Do
Echo "there are $ {animal} s ..."
Done
========================================================== ======================
[Root @ fwq prac] # cat sh16.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Users = $ (cut-d': '-f1/etc/passwd)
For username in $ users
Do
Id $ username
Finger $ username
Done
========================================================== ======================
[Root @ fwq prac] # cat sh17.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Network = "10.6.7"
For sitenum in $ (seq 100)
Do
Ping-c 1-w 1 $ {network}. $ {sitenum} &>/dev/null & result = 0 | result = 1
If ["$ result" = 0]; then
Echo "Server $ {network}. $ {sitenum} is up ."
Else
Echo "Server $ {network}. $ {sitenum} is down ."
Fi
Done
========================================================== ======================
[Root @ fwq prac] # cat sh18.sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Read-p "Please input a directory:" dir
If ["$ dir" = ""-o! -D "$ dir"]; then
Echo "The $ dir is NOT exist in your system ."
Exit 1
Fi
Filelist = $ (ls $ dir)
For filename in $ filelist
Do
Perm = ""
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/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin: usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Read-p "Please input a number, I will count for 1 + 2 +... your input:" nu
S = 0
For (I = 1; I <$ nu; I = I + 1 ))
Do
S = $ ($ s + $ I ))
Done
Echo "The result of 1 + 2 + 3 +... + $ nu =" $ s
========================================================== ======================