先看这个:https://blog.csdn.net/gsh_hello_world/article/details/56277182
https://blog.csdn.net/gsh_hello_world/article/details/81335955
#case练习
#!/bin/bash
case $1 in
"hello")
echo "Hello, how are you?"
;;
"")
echo "You MUST input parameters, ex>{$0 someword}"
;;
*)
echo "You should input {$0 hello}"
esac
#创建不同时间的文件
#!/bin/bash
echo -e "I will use 'touch' to create three new files."
read -p "Please input the file name:" filename
filename=${filename:-"file"}
date1=$(date --date='2 days ago' +%Y%m%d)
date2=$(date --date='1 days ago' +%Y%m%d)
date3=$(date +%Y%m%d)
filename1=${filename}${date1}
filename2=${filename}${date2}
filename3=${filename}${date3}
touch $filename1
touch $filename2
touch $filename3
#两个数字相乘
#!/bin/bash
echo -e "Please input two numbers, I will cross them!"
read -p "first number:" firstnu
read -p "second number:" secondnu
res=$(($firstnu * $secondnu))
echo -e "\nThe result of $firstnu * $secondnu is $res"
#if练习
#!/bin/bash
if [ "$1" == "hello" ];then
echo "Hello, how are you?"
elif [ "$1" == "" ];then
echo "You must input parameters, example : {$0 someword}"
else
echo "You must input 'hello', example : {$0 hello}"
fi
#netstat网络服务端口
#!/bin/bash
echo -e "I will detect your Linux server's services!"
echo -e "the www(80), ftp(21), ssh(22) and mail(25) will be detected!\n"
test=$(netstat -tuln | grep ":80")
if [ "$test" != " " ]; then
echo "WWW is running in your system."
fi
test=$(netstat -tuln | grep ":21")
if [ "$test" != "" ]; then
echo "ftp is running in your system."
fi
test=$(netstat -tuln | grep ":22")
if [ "$test" != "" ]; then
echo "ssh is running in your system."
fi
test=$(netstat -tuln | grep ":25")
if [ "$test" != "" ]; then
echo "mail is running in your system."
fi
#默认变量,读取参数
#!/bin/bash
echo "The shell script name is: $0"
echo "The total parameter number is: $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0
echo "Your whole parameter is: $@"
echo "The first parameter is: $1"
echo "The second parameter is: $2"
#输入名称,输出是否存在,是文件还是目录,以及属性
#!/bin/bash
echo -e "Please input a filename, I will check the filename's type and perimission.\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="regulare file"
test -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"
#判断退役时间
#!/bin/bash
read -p "please input you time:" date_input
echo $date_input
date_input=$( ( echo $date_input | grep '[0-9]\{8\}' ))
if [ "$date_input" == "" ]; then
echo "you input the wrong data format..."
exit 1
fi
date_dem=$( (date --date="$date_input" +%s) ) #second format of input date
echo $date_dem
date_now=$( ( date +%s ) )
echo $date_now
date_total_s=$(($date_dem-$date_now))
date_total_d=$(($date_total_s/60/60/24))
if [ "$date_total_s" -lt "0" ]; then
echo "you had been demobilization $date_total_d days ago"
else
date_h=$(($(($date_total_s-$date_total_d*24*60*60))/60/60))
echo "you will demobilize after $date_total_d days and $date_h hours."
fi
# 函数1
#!/bin/bash
function printit(){
echo "Your choice is $1."
}
echo "This program will print your selection!"
case $1 in
"one")
printit one
;;
"two")
printit two
;;
"three")
printit three
;;
*)
echo "Usage $0 {one|two|three}"
;;
esac
#函数2
#!/bin/bash
function printit(){
echo -n "Your choice is " ##-n display on one line
}
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
#while
#!/bin/bash
while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! You input correct answer."
#until
#!/bin/bash
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."
#计算1-100的和
#!/bin/bash
a=1
sum=0
while [ "$a" -lt "101" ]
do
sum=$(( $sum+$a ))
a=$(( $a+1 ))
done
echo "The result of 1+2+3+......+100 is $sum"
#for
#!/bin/bash
for animal in dog cat elephant
do
echo "There are ${animal}s.."
done
#for & cut
#! /bin/bash
users=$( cut -d ':' -f1 /etc/passwd )
for username in $users
do
id $username
finger $username
done
#ping
#!/bin/bash
network="192.168.1"
for sitenu in $(seq 1 100)
do
ping -c 1 -w 1 $network.$sitenu &> ~/shell_test/ping.log && result=0 || result=1
if [ $result == 0 ]; then
echo "$network.$sitenu is UP."
else
echo "$network.$sitenu is DOWN."
fi
done
#read_dir
#!/bin/bash
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 file in $filelist
do
perm=""
test -r "$dir/$file" && perm="readable"
test -w "$dir/$file" && perm="writable"
test -x "$dir/$file" && perm="executable"
echo "The file $dir/$file's permission is $perm."
done
#!/bin/bash
read -p "Please input a number, I will count 1+2+...+your_input:" nu
if false; then
sum=0
for count in $( seq 1 $nu )
do
echo $count
sum=$(($count+$sum))
done
else
sum=0
for ((i=1; i<=$nu; i=i+1))
do
sum=$(($i+$sum))
done
fi
echo $sum
#计算还有几天过生日
#!/bin/bash
read -p "Please input your birthday, for example 0709:" birthday
birthday=$( ( echo $birthday | grep '[0-9]\{4\}' ) )
echo $birthday
if [ "$birthday" = "" ]; then
echo "your input is wrong format!";
exit 1;
fi
date_now=$(date +%m%d)
if [ "$date_now" == "$birthday" ]; then
echo "Happy Birthday to you!"
elif [ "$date_now" -gt "$birthday" ]; then
year=$((`date +%Y`+1))
total_day=$(($((`date --date="$year$birthday" +%s`-`date +%s`))/60/60/24))
echo $total_day
else
year=$(date +%Y)
total_day=$(($((`date --date="$year$birthday" +%s`-`date +%s`))/60/60/24))
echo $total_day
fi
#!/ein/bash
if [ ! -e logical ]; then
touch logical
echo "make a file 'logical'"
exit 0
elif [ -f logical ]; then
rm logical
mkdir logical
echo "remove file 'logical' and make directory 'logical'"
exit 0
elif [ -d logical ]; then
rm -rf logical
echo "remove directory 'logical'"
exit 0
else
echo "does here have anything?"
fi
#!/bin/bash
echo "I will output account name in /etc/passwd:"
userlist=$(cut -d ':' -f1 /etc/passwd)
count=0
for user in $userlist
do
count=$(($count+1))
echo "The $count account is \"$user\""
done