「学习笔记-Linux」学习Shell Script
- - CSDN博客系统运维推荐文章学习Shell Script. 1 什么是Shell Scipt. 2.2 例2 按日期建立相似名字的文件. 3.2.4 整数,字符串,多重条件判断. 4 Shell Script 参数. 5.2 if else 结构. 8 shell script的追踪与Debug. 1 什么是Shell Scipt.
使用指令和基本程序设计结构写成的程序,可以完成复杂的处理流程
# !/bin/bash
# Program:
# This program shows "Hello Wrold" in your screen.
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
echo -e "Hello World!\a\n"
exit 0
注:用bash和用source的不同在于,用bash执行时,shell script其实是在在父程序bash下新建了一个 bash子程序,这个子程序中执行,当程序执行完后,shell script里定义的变量都会随子程序的结束而消失, 而用source执行时,是在父程序bash中执行,shell script里定义的变量都还在。
# !/bin/bash
# Program:
# This program is used to read user's input
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Your first name:" firstname # tell user to input
read -p "Your last name:" lastname # tell user to input
echo -e "\nYour full name: $firstname $lastname"
exit 0
$ bash sh02.sh Your first name:Minix Your last name:007 Your full name: Minix 007
# !/bin/bash
# Program:
# This program is used to create files according to date
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
# Get filename from user
echo -e "I will use 'touch' to create three files."
read -p "Please input your filename:" tmpfilename
# Prevent the user input [Enter]
# Check whether filename exists or not
filename=${tmpfilename:-"filename"}
# Get the final filename according to date
date1=$(date --date='2 days ago' +%Y%m%d) # date of 2 days ago
date2=$(date --date='1 days ago' +%Y%m%d) # date of yesterday
date3=$(date +%Y%m%d) # date of today
filename1=${filename}${date1}
filename2=${filename}${date2}
filename3=${filename}${date3}
# Create file
touch "$filename1"
touch "$filename2"
touch "$filename3"
exit 0
$ bash sh03.sh I will use 'touch' to create three files. Please input your filename:WhoKnows $ ls W* WhoKnows20130201 WhoKnows20130202 WhoKnows20130203
test -e filename会根据filename是否存在返回0或1,再交由echo显示结果
$ test -e sh01.sh && echo "Exists" || echo "Not exists" Exists $ test -e sh0x.sh && echo "Exists" || echo "Not exists" Not exists
例:输出指定文件类型及属性
# !/bin/bash
# Program:
# This program is used to output type and permission of the target file
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
# Get filename from user
echo -e "Input name of the file that you want to check.\n"
read -p "Filename:" filename
test -z $filename && echo "You must input a filename." && exit 0
# Check whether the file exists or not
test ! -e $filename && echo "The file '$filename' DO NOT exists" && exit 0
# Check type and permission of the file
test -f $filename && filetype="regular file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
# Output result
echo "The filename:$filename is a $filetype"
echo "And Permissions are :$perm"
exit 0
$ bash sh04.sh Input name of the file that you want to check. Filename:sh01.sh The filename:sh01.sh is a regular file And Permissions are :readable writable executable
$ [ -e "sh01.sh" ] ; echo $? 0 $ [ -e "sh0x.sh" ] ; echo $? 1
# !/bin/bash
# Program:
# This program is used to ouput parameter of the shell script
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
echo "The script's name is ==> $0"
echo "Total parameter number is ==> $#"
# Check whether number of the parameter is less than 2
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2.Stop here." && exit 0
echo "The whole parameter is ==> '$@'"
echo "The first parameter is ==> $1"
echo "The first parameter is ==> $2"
exit 0
$ bash sh05.sh 1a 2b 3c 4d The script's name is ==> sh05.sh Total parameter number is ==> 4 The whole parameter is ==> '1a 2b 3c 4d' The first parameter is ==> 1a The first parameter is ==> 2b
注:从以上程序可以看出与参数有关的预设变量如何表示
# !/bin/bash
# Program:
# This program is used to show if else expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Please input [Y/N]" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
echo "OK, continue"
exit 0
fi
if [ "$choice" == "N" ] || [ "$choice" == "n" ];then
echo "Oh, interupt"
exit 0
fi
exit 0
$ bash sh06.sh Please input [Y/N]y OK, continue $ bash sh06.sh Please input [Y/N]n Oh, interupt
# !/bin/bash
# Program:
# This program is used to show if else expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Please input [Y/N]" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
echo "OK, continue"
exit 0
elif [ "$choice" == "N" ] || [ "$choice" == "n" ];then
echo "Oh, interupt"
exit 0
else
echo "Input [Y/N]"
fi
exit 0
# !/bin/bash
# Program:
# This program is used to show case expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Tell me your choice:[1-3]=>" choice
case $choice in
"1")
echo "Your choice is ONE"
;;
"2")
echo "Your choice is TWO"
;;
"3")
echo "Your choice is THREE"
;;
esac
exit 0
$ bash sh08.sh Tell me your choice:[1-3]=>2 Your choice is TWO $ bash sh08.sh Tell me your choice:[1-3]=>1 Your choice is ONE $ bash sh08.sh Tell me your choice:[1-3]=>3 Your choice is THREE
# !/bin/bash
# Program:
# This program is used to test function
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
function myprint(){
echo -n "Your choice is "
}
read -p "Tell me your choice:[1-3]=>" choice
case $choice in
"1")
myprint; echo "ONE"
;;
"2")
myprint; echo "TWO"
;;
"3")
myprint; echo "THREE"
;;
esac
exit 0
$ bash sh09.sh Tell me your choice:[1-3]=>1 Your choice is ONE $ bash sh09.sh Tell me your choice:[1-3]=>2 Your choice is TWO $ bash sh09.sh Tell me your choice:[1-3]=>3 Your choice is THREE
# !/bin/bash
# Program:
# This program shows while expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
while [ "$choice" != "yes" ]
do
read -p "Give your choice [yes/no]:" choice
done
exit 0
$ bash sh10.sh Give your choice [yes/no]:no Give your choice [yes/no]:no Give your choice [yes/no]:nx Give your choice [yes/no]:yes
# !/bin/bash
# Program:
# This program is used to demo for expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
for choice in 1 2 3
do
echo "your choice is $choice"
done
exit 0
$ bash sh11.sh your choice is 1 your choice is 2 your choice is 3