鸟哥私房菜shellscript的脚本学习


鸟哥私房菜shell script的脚本学习,为了方便联系,去掉了所有的注释

[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 Should 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\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

========================================================

[root@fwq prac]# cat sh06-2.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

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

========================================================

[root@fwq prac]# cat sh06-3.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

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

========================================================

[root@fwq prac]# cat sh09-2.sh
#!/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 demobilization date at how many days
# later...
# History:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 1. 告知使用者这支程序的用途,并且告知应该如何输入日期格式?
echo "This program will try to calculate :"
echo "How many days about your demobilization date..."
read -p "Please input your demobilization date (YYYYMMDD ex>20050401): " date2

# 2. 测试一下,这个输入的内容是否正确?利用正规表示法啰~
date_d=`echo $date2 |grep '[0-9]\{8\}'`
if [ "$date_d" == "" ]; then
echo "You input the wrong format of date...."
exit 1
fi

# 3. 开始计算日期啰~
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 demobilization before: " $((-1*$date_d)) " ago"
else
declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
echo "You will be demobilized 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

========================================================

[root@fwq prac]# cat sh12-2.sh
#!/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

========================================================

[root@fwq prac]# cat sh12-3.sh
#!/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."

========================================================

[root@fwq prac]# cat sh13-2.sh
#!/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 1 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

========================================================


相关内容