SHELL 脚本基本知识

shell脚本的首行标识的意义
#!/bin/sh 
#!/bin/bash
其中#表示该行是注释,叹号“!”告诉shell运行叹号之后的命令并用文件的其余部分作为输入,
也就是运行/bin/bash并让/bin/bash去执行shell程序的内容。
我们的linux系统中,sh是bash的一个软链接:
lrwxrwxrwx 1 root root 4 Sep 20  2009 sh -> bash

shell脚本的执行跟踪
  1 #!/bin/sh
  2 
  3 set -x
  4 
  5 echo "test1"
  6 
  7 echo "test2"
  8 
  9 set +x
 10 
 11 echo "test3"
 12 
 13 echo "end"


执行结果
+ echo test1
test1
+ echo test2
test2
+ set +x
test3
end
++++++++++++++++++++++++
set -x表示打开追踪
set +x表示关闭追踪


简单的脚本使用while,if else,case例子
#!/bin/sh
echo "enter passwd"
read passwd
while [ $passwd != "aaa" ]
do
echo "try again"
read passwd
done
echo "passwd sucess"


echo "enter a number"
read num
if [ $num = "1" ];then
echo "num = 1"
elif [ $num = "2" ];then
echo "num =2"
else
echo "no";
fi
echo "end"


case $1 in --help | -h)
usage;
;;
--fun) fun;
;;
esac


$打头的变量其后最好要加{}


SHELL脚本中
SHELL 判断中-a -z意义
[ -a FILE ]  如果 FILE 存在则为真。  
[ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真。  
[ -c FILE ]  如果 FILE 存在且是一个字特殊文件则为真。  
[ -d FILE ]  如果 FILE 存在且是一个目录则为真。  
[ -e FILE ]  如果 FILE 存在则为真。  
[ -f FILE ]  如果 FILE 存在且是一个普通文件则为真。  
[ -g FILE ]  如果 FILE 存在且已经设置了SGID则为真。  
[ -h FILE ]  如果 FILE 存在且是一个符号连接则为真。  
[ -k FILE ]  如果 FILE 存在且已经设置了粘制位则为真。  
[ -p FILE ]  如果 FILE 存在且是一个名字管道(F如果O)则为真。  
[ -r FILE ]  如果 FILE 存在且是可读的则为真。  
[ -s FILE ]  如果 FILE 存在且大小不为0则为真。  
[ -t FD ]  如果文件描述符 FD 打开且指向一个终端则为真。  
[ -u FILE ]  如果 FILE 存在且设置了SUID (set user ID)则为真。  
[ -w FILE ]  如果 FILE 如果 FILE 存在且是可写的则为真。  
[ -x FILE ]  如果 FILE 存在且是可执行的则为真。  
[ -O FILE ]  如果 FILE 存在且属有效用户ID则为真。  
[ -G FILE ]  如果 FILE 存在且属有效用户组则为真。  
[ -L FILE ]  如果 FILE 存在且是一个符号连接则为真。  
[ -N FILE ]  如果 FILE 存在 and has been mod如果ied since it was last read则为真。  
[ -S FILE ]  如果 FILE 存在且是一个套接字则为真。  
[ FILE1 -nt FILE2 ]  如果 FILE1 has been changed more recently than FILE2, or 如果 FILE1 exists and FILE2 does not则为真。  
[ FILE1 -ot FILE2 ]  如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在则为真。  
[ FILE1 -ef FILE2 ]  如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。  
[ -o OPTIONNAME ]  如果 shell选项 “OPTIONNAME” 开启则为真。  
[ -z STRING ]  “STRING” 的长度为零则为真。  
[ -n STRING ] or [ STRING ]  “STRING” 的长度为非零 non-zero则为真。  
[ STRING1 == STRING2 ]  如果2个字符串相同。 “=” may be used instead of “==” for strict POSIX compliance则为真。  
[ STRING1 != STRING2 ]  如果字符串不相等则为真。 
[ STRING1 < STRING2 ]  如果 “STRING1” sorts before “STRING2” lexicographically in the current locale则为真。  
[ STRING1 > STRING2 ]  如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。  
[ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers. 


如下:
#!/bin/sh

if [ -e "/home/bps/susz/shell_test/file_exist" ]; then
echo "file exist"
else
touch "/home/bps/susz/shell_test/file_exist"
echo "create file"
fi


判断文件存不存在,不存在就创建


判断的使用
-eq 等于
-ne 不等于
-gt 大于
-ge 大于等于
-lt 小于
-le 小于等于



变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。


脚本中函数调用
#!/bin/sh


fun()
{
echo "fun_test"
}


usage()
{
echo "i don't wan't help you"
}


if [ $# -gt 0 ]; then
case $1 in --help | -h)
usage;
;;
--fun) fun;
;;
esac
else
# usage();
echo "input error"
fi


shift命令:

#测试 shift 命令(x_shift.sh)
until [ $# -eq 0 ]
do
echo "第一个参数为: $1 参数个数为: $#"
shift
done
执行以上程序x_shift.sh:
$./x_shift.sh 1 2 3 4

结果显示如下:

第一个参数为: 1 参数个数为: 4
第一个参数为: 2 参数个数为: 3
第一个参数为: 3 参数个数为: 2
第一个参数为: 4 参数个数为: 1

从上可知 shift 命令每执行一次,变量的个数($#)减一,而变量值提前一位,




Read的一些选项
 Read可以带有-a, -d, -e, -n, -p, -r, -t, 和 -s八个选项。
read命令 -n(不换行) -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显)
echo -n "Enter your name:"       //参数-n的作用是不换行,echo默认是换行
read -p "Enter your name:" name  //-p(提示语句)
if read -t 5 -p "please enter your name:" name   //倒计时5s 提示输入
详见
http://blog.163.com/niuxiangshan@126/blog/static/170596595201271215933154/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值