下面是工作中用到的链接数据库的shell程序。
#!/bin/bash if [ $# -le ] ; then
echo "prase is wrong ,please check first"
exit
fi
#知识点:
1,$#代表了命令行的参数数量
2,-le表示小于等于,拓展 lt表示小于,gt表示大于,ge表示大于等于,eq表示等于,ne表示不等于
if [ "$1"x != "online"x ] && [ "$1"x != "neibu"x ] ; then
echo "you must input online or neibu !"
exit
fi
#知识点:
1,注意到"$1"x最后的x,这是特意安排的,因为当$1为空的时候,上面的表达式就变成了x = onlinex,显然是不相等的。而如果没有这个x,表达式就会报错:[: =: unary operator expected
2,注意到等号两边各有一个空格
3,&&符号链接
if [ "$1"x = "online"x ] ; then
case $ in
pm)
mysql -h xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;;
admin)
mysql -h xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;;
esac
elif [ "$1"x = "neibu"x ] ; then
case $ in
pm)
mysql -h xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;;
admin)
mysql -h xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ;;
esac
fi 知识点:
case语法
ase ... in
...) do something here
;;
esac
mysql启动和停止脚步
#!/bin/bash case "$1" in
start)
echo "mysql start..."
/etc/init.d/mysqld start
;;
stop)
echo "mysql stop..."
/etc/init.d/mysqld stop
;;
restart)
echo "mysql restart..."
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
;;
esac exit