Shell脚本初步学习-鸟哥Linux私房菜基础学习篇

   Shell脚本对于Linux下的系统管理员和运维的人来说很重要。最近看了一下Shell脚本,为了系统地学习一下Shell脚本,我看了一下《鸟哥的Linux私房菜基础学习篇》第三版,其中的第13章讲了一下Shell script的学习。可以到鸟哥的网站:第十三章、學習 Shell Scripts看看这一章的示例,对于学习Shell脚本初学者入门很不错!

   编写shell script的良好习惯 
1、script的功能;
2、script的版本信息;
3、script的作者与联络方式;
4、script的版本声明方式;
5、script的History  (历史记录)
6、script内较特殊的命令,使用“绝对路径”的方式来执行;
7、script执行时需要的环境变量预先声明和设置。

sh01.sh

#!/bin/bash
# program:
# 	This program show "Hello World!" in your screen
# History:
# 2013/04/21 15:31 ccf First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
exit 0

 

sh02.sh

sh02.sh
#!/bin/bash
# Program
# 	User inputs his first name and last name.   Program shows his full name.
# History
# 2013/04/21,by ccf,First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "Please input your first name: " firstname   # 提示用户输入
read -p "Please input your last name:  " lastname    # 提示用户输入
echo -e "\nYour full name is: $firstname $lastname"   #结果由屏幕输出


 

sh03.sh

#!/bin/bash
# Program
#	Program creates three files, which named by user's input
#	and date command
# History:
# 2013/04/21 Sunday ccf First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin:~/bin
export PATH

# 1.让用户输入文件名,并取得fileuser这个变量
echo -e "I will use 'touch' command to create 3 files." # 纯粹显示信息
read -p "Please input your filename: " fileuser         # 提示用户输入

# 2.为了避免用户随意按【Enter】,利用变量功能分析文件名是否有设置
filename=${fileuser:-"filename"}                        # 开始判断是否有配置文件名

# 3.开始利用date命令来取得所需要的文件名了
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}

# 4.创建文件名
touch "$file1"
touch "$file2"
touch "$file3"


 

sh04.sh

#!/bin/bash
# Program:
# 	User inputs 2 integer numbers; program will cross these two numbers.
# History:
# 2013/04/21 ccf19881030  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "You should input 2 numbers, I will cross them!\n"
read -p "first number: "  firstnum
read -p "second number: " secondnum
total=$(($firstnum * $secondnum))
echo -e "\nThe result of $firstnum * $secondnum is ==> $total"


 

sh05.sh

#!/bin/bash
#program:
#	User input a filename, program will check the follwing:
#	1.) exist? 2.) file/directory? 3.) file permissions
#History:
# 2013/05/14 ccf First Release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#1、让用户输入文件名,并且判断用户是否真的有输入字符串
echo -e "Please input a filename, I will check the filename's type and permission. \n\n"  #纯粹显示信息
read -p "Input a filename : " filename       #提示用户输入

test -z $filename && echo "You MUST input a filename." && exit 0

#2、判断文件是否存在,若不存在则显示信息并结束脚本
test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0

#3、开始判断文件类型和属性
test -f $filename && filetype="regular file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writeable"
test -x $filename && perm="$perm executable"

#4、开始输出信息!
echo "The filename: $filename is a $filetype"
echo "And the permission are : $perm"


 

sh06.sh

#!/bin/bash
# Program:
#     This program shows the user's choice
# History:
# 2013/05/14 ccf First release
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


可以通过sh sh01.sh或者chmod a+x sh01.sh;./sh01.sh来执行shell脚本。

你可能感兴趣的:(系统集成,Linux,编程语言学习)