我的第一个shell脚本

我的第一个shell脚本, 今天在linux写了第一个shell脚本,分享一下。

test.sh

vi test.sh

插入如下内容

#!/bin/bash
if [ “$2” = “vi” ]; then
vi “/opt/tomcat7_test/logs/catalina.$1.out”
elif [ “$2” = “tail” ] || [ “$2” = “” ]; then
tail -f “/opt/tomcat7_test/logs/catalina.$1.out”
fi

保存。

更改权限

chmod u+x test.sh

执行

-- vi

sh test.sh 2018-07-03 vi

-- tail -f

sh test.sh 2018-07-03 tail

注意

1. $1接收第一个参数, $2接收第二个参数,以此类推。

2. if elseif 的语法: 在shell中, 应为

if [ 条件 ] ; then

命令

elif [ 条件 ]; then

命令

fi

3. 判断字符是否相等

=和 == 一样, = 前后都必须有空格。  

|| 的前后必须是 [ ] 对,或者 [ 条件A or 条件B ]

[的后面 和 ]的前面都必须有空格

0%