欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

linux基础之Shell Script入门介绍

程序员文章站 2023-08-16 16:58:50
linux基础之shell script 1 shell scipt使用指令和基本程序设计结构写成的程序,可以完成复杂的处理流程 1.1 程序书写 复制代码 代码如下...

linux基础之shell script

1 shell scipt
使用指令和基本程序设计结构写成的程序,可以完成复杂的处理流程

1.1 程序书写

复制代码 代码如下:

#!/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

第一行 #!/bin/bash 说明使用的shell类型,不同shell语法可能不同,所以要说明使用的是哪种shell
其它#开始的表示注释,注释一般需要说明
程序功能
版本历史
作者及联系方式
设置好path变量,以便直接可以调用相应路径下的命令
程序主体部分
exit 0 表示程序执行成功,向环境返回0
1.2 程序执行
bash $bash sh01.sh #如果用sh sh01.sh而sh又不是指向bash,那么sh01.sh内的语法就会不一致,因为用 #sh去解释了bash语法写的shell script,针对这个程序,如果 #$type sh #得到sh is hashed (/bin/sh) #那么会输出-e hello world!,而非hello world!

复制代码 代码如下:

$./xxx.sh $chmod +x sh01.sh $./sh01.sh
source $ source sh01.sh

注:用bash和用source的不同在于,用bash执行时,shell script其实是在在父程序bash下新建了一个 bash子程序,这个子程序中执行,当程序执行完后,shell script里定义的变量都会随子程序的结束而消失, 而用source执行时,是在父程序bash中执行,shell script里定义的变量都还在。

2 简单shell练习

2.1 例1 接收用户输入

复制代码 代码如下:

# !/bin/bash
# program:
#       this program is used to read user's input
# site: www.jb51.net
# 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

2.2 例2 按日期建立相似名字的文件

复制代码 代码如下:

# !/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

3 判断式
3.1 测试文件是否存在
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

3.2 test常用选项
3.2.1 文件类型

复制代码 代码如下:

-e file :file是否存在
-f file :file是否存在且为文件
-d file :file是否存在且为目录

3.2.2 权限
-r file :file是否有读的权限

3.2.3 文件新旧比较
-nt file1 file2 : file1 是否比 file2新

3.2.4 整数,字符串,多重条件判断
-z string: string是否为空
例:输出指定文件类型及属性

复制代码 代码如下:

# !/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

3.3 使用[]判断

测试文件是否存在

复制代码 代码如下:

$ [ -e "sh01.sh" ] ; echo $?
0
$ [ -e "sh0x.sh" ] ; echo $?
1

注意[]内空格必须有
这种方法和test的test -e "sho1.sh" ; echo $? 是一致的

4 shell script 参数

复制代码 代码如下:

# !/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

注:从以上程序可以看出与参数有关的预设变量如何表示

5 条件表达式

5.1 if 结构

复制代码 代码如下:

# !/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

5.2 if else 结构

复制代码 代码如下:

# !/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

5.3 case

复制代码 代码如下:

# !/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

6 函数

复制代码 代码如下:

# !/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

7 循环
7.1 while

复制代码 代码如下:

# !/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

7.2 for

复制代码 代码如下:

# !/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

8 shell script的追踪与debug
sh -n xx.sh # 语法检查
sh -x xx.sh # 列出xx.sh的执行过程