Shell script

什么是Shell script?

shell script 是利用shell的功能所写的一个程序,是纯文本文件,将一些shell的语法与指令(含外部指令)写在里面,搭配正则表达式、grep管道命令和数据流重定向等功能,以达到我们想要的目的。

shell script是系统管理上的很好的工具,但是不适合用在大量数值的运算。因为shell scripts的速度较慢,且使用的CPU的资源较多,容易造成主机资源的分配不良。

在执行的时候,可以使用sh test.sh 或者 bash test.sh或者./test.sh

其中./test.sh需要具备x权限才可以执行。另外两种只需要有r权限即可。这是sh test.sh与bash test.sh的区别

良好的程序撰写习惯中,第一行要宣告 shell (#!/bin/bash) ,第二行以后则宣告程序用途、版本、作者等。

简单的shell scripts

1.hello word

#!/bin/bash
PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/sbin/:/usr/local/bin:~/bin
export PATH
echo -e "Hello word ! \a \n"
exit 0

2.交互式脚本

使用read

#!/bin/bash
read -p "Please input your first name: " firstname
read -p "Please input your last name:" lastname
echo -e "\n Your full name is ${firstname} ${lastname}"
exit 0

利用内置变量

/path/to/scriptname opt1 opt2 opt3 opt4
        $0           $1   $2   $3   $4

这里的$0为脚本名称,$1,2,3,4...为参数

$# :代表后接的参数『个数』以上面为例这里显示为『 4 』

$@ :代表『 "$1" "$2" "$3" "$4" 』之意,每个变量是独立的(用双引号括起来);

$* :代表『 "$1c$2c$3c$4" 』其中 c 为分隔字符,默认为空格键, 所以本例中代表『 "$1 $2 $3 $4" 』之意。

3.数值加减乘除运算

使用$((计算式))来进行计算。需要注意这里是两层括号

#!/bin/bash
declare a=5
declare b=1
declare c=$((${a}-${b}))
echo ${c}

判断式

1.括号判断

[ "${a}" == "${b}" ]需要注意这里包括四个空格,假设_为空格。[_"${a}"_==_"${b}"_],若相等返回true。

2.test判断

test -e /dmtasi  检查某文件是否存在 -e

-e 该『档名』是否存在?(常用)
-f 该『档名』是否存在且为文件(file)?(常用)
-d 该『文件名』是否存在且为目录(directory)?(常用)

test str1 == str2 判定 str1 是否等于 str2 ,若相等,则回传 true
test str1 != str2 判定 str1 是否不等于 str2 ,若相等,则回传 false

-a (and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,才回传 true。
-o (or)两状况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 或 x 权限时,就可回传 true。
! 反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true

注意正确(true)返回的是0(因为执行正确表示0,错误表示1),&&表示and  ||表示or

3.条件判断语句

if...then

if [conditon];then

        语句

elif [conditon];then

        语句

else

        语句

fi

#!/bin/bash
if [ "$#" == "0" ] ; then 
	echo "Please input parameter,ex> {${0} hello}" 
elif [ "$1" == "hello" ] ; then
	echo "Hello,how are you"
else
	echo "Please input hello"
fi
exit 0
#!/bin/bash
#包含了date的使用与数字计算
echo "this program is order to caculate one time you input from one date to now!"                                         
read -p "please input one date " date2
echo ${date2}

date_d=$(echo ${date2} | grep '[0-9]\{8\}')
if [ "${date_d}" == "" ] ; then
    echo "You must input the wrong date formate...."
    exit 1
fi
declare -i date_dem=$(date --date="${date2}" +%s)
declare -i date_now=$(date +%s)
declare -i date_total_s=$((${date_dem}-${date_now}))
echo ${date_total_s}
declare 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 demobilize after ${date_d} days and ${date_h} hours."
fi

case ... esac

case ${1} in
    "hello")
        echo "Hello, how are you ?"
        ;;
    "")
        echo "You MUST input parameters, ex> {${0} someword}"
        ;;
    *)
    # 其实就相当于通配符,0~无穷多个任意字符之意!
    echo "Usage ${0} {hello}"
    ;;
esac

function函数

类似于自定义命令,因为shell脚本的执行顺序为由上而下,由左而右,因此在shell脚本当中的function的设置一定要在程序的最前面。

function的内置变量

与shell脚本的内置变量类似,函数名称为$0,后续的变量为$1,$2...

循环(loop)

1.while do done,until do done(不定循环,不确定要循环多少次)

以1+2+3+4....+num为例

这里的while 当i!=maxnum的时候就不会执行程序段

#!/bin/bash
#this program is order to learn while 
#the sum of one to one number you input
read -p "please input one number to caculate the sum " maxnum                   
i=0
sum=0
while [ "${i}" != "${maxnum}" ] 
do
    i=$((${i}+1))
    sum=$((${sum}+${i}))
done
echo "the sum of one to the number of you input is ${sum}"
exit 0

而until是当i==maxnum的时候还会执行一次程序段

#!/bin/bash
#this program is order to learn until 
#the sum of one to one number you input
read -p "please input one number to caculate the sum " maxnum                   
i=0                                                                             
sum=0
until [ "${i}" == "$((${maxnum}))" ]
do
    i=$((${i}+1))
    sum=$((${sum}+${i}))
done    
echo "the sum of one to the number of you input is ${sum}"
exit 0

2.for...do...done(固定循环)

for var in con1 con2 con3 ...
do
        程序段
done

3.for...do...done的数值处理

for (( 初始值; 限制值; 执行步阶 ))
do
        程序段
done

#!/bin/bash                                                                     
#this program is order to learn for  
#the sum of one to one number you input
read -p "please input one number to caculate the sum " maxnum
sum=0
for (( i=1; i<=${maxnum}; i=i+1))
do
    sum=$((${sum}+${i}))
done
echo "the sum of one to the number of you input is ${sum}"
exit 0

随机数与数组

随机数RANDOM是一个0-32767之间的随机整数

eat[1]=9
eat[2]=8
eat[3]=7
eatnum=3
echo "${eat[${eatnum}]}"#输出7

只是一个数组格式的变量

shell scrips的追踪与debug

由于sh的局限性,尝试使用bash的debug

-n不执行脚本,只是检查语法问题

-v只是简单的将脚本内容输出而没有中间过程

-x可以将执行过程,执行到哪一句的输出结果显示出来。


参考鸟的的Linux私房菜(第四版)

多翻阅,多练习

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值