shell 编程基础

本文介绍了Shell脚本的基础知识,包括基本结构、变量赋值、条件判断、循环控制语句等,并通过实例演示了如何使用这些功能进行脚本编写。
shell 结构:
1.#!指令执行脚本的shell
2.#注释
3.程序

shell 程序:
1.创建shell程序
2.修改执行权限 chmod u+x
3.执行 ./example.sh   或  sh ./example.sh

设置定时器:
crontab
 -e  编辑该用户的计时器设置。
 -l  列出该用户的计时器设置。
 -r  删除该用户的计时器设置。
 -u<用户名称>  指定要设定计时器的用户名称。

分 时 天 月 周 sh
0  3   ×  × 2,5  /bin/sh /shell/autobak.sh /etc  #每周2和5凌晨3点备份etc

调试:sh -x test.sh
检查语法:sh -n test.sh

*/10 * * * * command  每10分钟一次
0 * * * * command  每小时一次
0 */3 * * * command  每三小时一次



变量赋值:不能有空格
[root@localhost shell]# date="2011-05-27"
[root@localhost shell]# echo $date
2011-05-27
[root@localhost shell]# date=`date`
[root@localhost shell]# echo $date
2011年 05月 28日 星期六 08:58:23 CST
[root@localhost shell]# date=$(date +%F)
[root@localhost shell]# echo $date
2011-05-28
[root@localhost shell]# date=$(date +'%Y-%m-%d %H:%M:%S')
[root@localhost shell]# echo $date
2011-05-28 09:06:33
[root@localhost shell]# time=$date
[root@localhost shell]# echo $time
2011-05-28 09:06:33
[root@localhost shell]# time="date is $date"
[root@localhost shell]# echo $time
date is 2011-05-28 09:06:33
[root@localhost shell]# time='date is $date'
[root@localhost shell]# echo $time
date is $date


查看变量:set | more
删除变量:unset time

位置变量 $# $* $? $$ :
[root@localhost shell]#sh bianliang.sh file1 file2 file3
$# is: 3 #统计参数个数
$* is: file1 file2 file3 #所有参数
$? is: 0 #执行返回值(0成功,非0失败)
$$ is: 3500 #pid
$2 is: file2 #第二个参数
$0 is: bianliang.sh #脚本

交互:
[root@localhost shell]# vim read.sh
#!/bin/sh
read first second third
echo "first is $first"
echo "second is $second"
echo "second is $third"
[root@localhost shell]# chmod u+x read.sh
[root@localhost shell]# sh read.sh
100 200 300
first is 100
second is 200
second is 300

运算:
expr 8 + 3 #加
expr 8 - 3 #减
expr 8 \* 3 #乘
expr 8 / 3 #除
[root@localhost shell]# var=`expr 8 % 3` #余
[root@localhost shell]# echo $var
2
[root@localhost shell]# expr `expr 5 - 2` \* 8
24

变量测试:test -d $1 等价于 [ -d $1 ]
[root@localhost shell]# test 2 = 2
[root@localhost shell]# echo $?
0
[root@localhost shell]# test 2 = 3
[root@localhost shell]# echo $?
1
字符串测试:
test str1 = str2 #判断字符串是否相等
test str1 != str2 #判断字符串是否不相等
test str1  #判断字符串是否不为空
test -n str1  #判断字符串是否不为空
test str1  #判断字符串是否为空
整数测试:
test int1 -eq int2 #测试是否相等
test int1 -ne int2 #测试是否不相等
test int1 -ge int2 #测试是否int1>=int2
test int1 -gt int2 #测试是否int1>int2
test int1 -le int2 #测试是否int1<=int2
test int1 -lt int2 #测试是否int1<int2
文件测试:
test -d file #文件是否目录
test -f file #文件是否常规文件
test -x file #文件是否可执行
test -r file #文件是否可读
test -w file #文件是否可写
test -a file #文件是否存在
test -s file #文件大小是否非0

流程控制语句if_else:
[root@localhost shell]# vim if_else.sh
#!/bin/sh
if [ $1 -eq 1 ];then
   echo "a"
elif [ $1 -eq 2 ];then
   echo "b"
elif [ $1 -eq 3 -o $1 -eq 4 ];then#等于3或等于4
   echo "c"
elif [ $1 -gt 0 -a $1 -lt 100 ];then#大于0且小于100
   echo "d"
else
   echo "e"
fi

流程控制语句for:
[root@localhost shell]# vim for.sh
#!/bin/sh
day="Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
for d in $day
do
  echo "the day is:$d"
done

流程控制语句select:
[root@localhost shell]# ./select.sh
what is your os?
1) linux
2) unux
3) windows
4) other
#? 1
is linux
[root@localhost shell]# cat select.sh
#!/bin/sh
echo "what is your os?"
select s in linux unux windows other
do
  break
done
echo "is $s"

流程控制语句case:
[root@localhost shell]# ./case.sh
*******************
please select your operation:
press c to copy
press d to delete
press b to backup
*******************
d
is delete
[root@localhost shell]# cat case.sh
#!/bin/sh
echo "*******************"
echo "please select your operation:"
echo "press "c" to copy "
echo "press "d" to delete "
echo "press "b" to backup "
echo "*******************"
read o
case $o in
   c)
    echo "is copy"
   ;;
   d)
    echo "is delete"
   ;;
   b)
    echo "is backup"
   ;;
   *)
    echo "invalide"
    exit 1#退出
esac

流程控制语句while:
[root@localhost shell]# ./while.sh
1
4
9
16
25
36
49
64
81
[root@localhost shell]# cat while.sh
#!/bin/sh
num=1
while [ $num -lt 10 ]
do
 sum=`expr $num \* $num`
 echo $sum
 num=`expr $num + 1`
done


流程控制语句until:
[root@localhost shell]# sh until.sh
press Y/y to stop...
y
stop here
[root@localhost shell]# cat until.sh
#!/bin/sh
echo "press Y/y to stop..."
read i
until [ $i = 'y' ] || [ $i = 'Y' ]
do
  echo "error input"
  read i
done
echo "stop here"

流程控制语句break:
[root@localhost shell]# ./break.sh
**************************
please select your operation:
1 copy
2 delete
3 backup
4 quit
**************************
4
is exit...
[root@localhost shell]# cat break.sh
#!/bin/sh
while true
do
 echo "**************************"
 echo "please select your operation:"
 echo "1 copy"
 echo "2 delete"
 echo "3 backup"
 echo "4 quit"
 echo "**************************"
 read o
 case $o in
   1)
   echo "is copy";;
   2)
   echo "is delete";;
   3)
   echo "is back";;
   4)
   echo "is exit..."
    break;;
   *)
   echo "invalide"
   # continue;;
  esac
done

流程控制语句shift:
[root@localhost shell]# ./shift.sh 1 2 3
6
[root@localhost shell]# cat shift.sh
#!/bin/sh
sum=0
while [ $# -gt 0 ]
do
 sum=`expr $sum + $1 `
 shift
done
echo $sum

函数的调用:
[root@localhost shell]# ./function.sh zsc xhj
zsc
xhj
[root@localhost shell]# cat function.sh
#!/bin/sh

help(){
  for s in $*
  do
      echo $s
  done
}

help $1 $2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值