总结一下在写shell脚本时的常见注意事项:

1.shell脚本中的命令最好用命令的全路径,如果不知道全路径可以用which cmd查找命令的全路径。

2.shell脚本中定义环境变量用export xxx=/dir1/dir2.....

3.shell脚本中取变量所以变量前都需加$,或者最好是${变量}

4.掌握常见的if、for、case语法的使用方法

5.shell脚本中最好写清楚注释

6. shell脚本中善于使用函数

7.用 $? 来判断上一个shell命令的执行结果,返回值是0代表正常结束,返回值是其他则代表不正常

8. 将一些命令的执行结果重定向到/dev/null,错误结果也需要重定向到/dev/null (/dev/null是linux的无底洞,我们丢进去的东西就都找不回来了)

9.善于使用ps 加grep命令判断一些服务是否启动,根据是否有相应的进程判断对应的服务是否启动。

10 善于使用awk命令提取一些需要的信息

killUser.sh  (输入登录的用户名,然后强制退出用户的脚本)

#!/bin/bash
#input username and kill relactive process for kill user
echo "please input username for kill"
read username
#if username is root ,exit
if [ ${username} = 'root' ]
then
echo "root can not kill"
exit
fi
#get user PID
PID=`/usr/bin/ps -aux | /usr/bin/grep qlq | /usr/bin/awk '$1="qlq" {print $2}'`
for killpid in $PID
do
kill - $killpid
done
echo "killed ok!"

tomcat.sh    (测试tomcat服务是否启动,开启、关闭tomcat)

#!/bin/bash
#chkconfig:
#description:tomcat
#input(start stop status) to operate tomcat service
#start funciton(start tomcat service use /opt/apache-tomcat/apache-tomcat-7.0./bin/start.sh)
export CATALINA_HOME=/opt/apache-tomcat/apache-tomcat-7.0.
start(){
/usr/bin/sh "${CATALINA_HOME}"/bin/startup.sh
if [ "$?" != "" ]
then
echo "service is not success start"
else
echo "service is success start" fi
exit
}
#stop function
stop(){
/usr/bin/sh "${CATALINA_HOME}"/bin/shutdown.sh
if [ "$?" != "" ]
then
echo "service is not success stop"
else
echo "service is success stop" fi
}
#status function
status(){
/usr/bin/ps -le | /usr/bin/grep java >/dev/null > /dev/null
if [ "$?" != "" ]
then
echo "service is not start"
else
echo "service is running" fi
}
#read input and dispose function
input=${}
case ${input} in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
echo "please use {start to start tomcat,stop to stop tomcat,status to read tomcat status!}"
esac

webmin.ssh  (测试webmin服务是否启动、开启、关闭webmin服务)

#!/bin/bash
#start webmin service
start()
{
/etc/webmin/start >/dev/null > /dev/null
if [ $? = '' ]
then
echo "webmin is success start"
fi
} #stop webmin service
stop()
{
/etc/webmin/stop > /dev/null > /dev/null
if [ $? = '' ]
then
echo "webmin is success stop"
fi
} #status webmin
status()
{
/usr/bin/netstat -ano | /usr/bin/grep > /dev/null
if [ $? != '' ]
then
echo "webmin is not start"
else
echo "webmin is running"
fi
} ########read input##############
str=$
if [ ${str} = 'start' ]
then
start
elif [ ${str} = 'stop' ]
then
stop
elif [ ${str} = 'status' ]
then
status
else
echo "please use {start,stop,status}"
fi

addUserBatch.sh     (批量添加用户的脚本)

#!/bin/bash
#adduser batch
echo "please input username:"
read username
echo "please input number to create:"
read number
#start to create user
for(( i=;i<="${number}";i++ ))
do
/usr/sbin/adduser "${username}${i}" > /dev/null > /dev/null
done
#add finished
echo "add OK!"
echo "please input passwd for users"
read password
for(( i=;i<="${number}";i++ ))
do
/usr/sbin/usermod -p "${password}" "${username}${i}" > /dev/null > /dev/null
done

delUserBatch.sh      (批量删除用户的脚本)

#!/bin/bash
#delete user batch
echo "please input username word to delete"
read word
#get All users like word*
users=`/usr/bin/grep ${word} /etc/passwd | awk -F: -v word1=${word} 'index($1,word1)>0 {print $1}'`
if [ "${users}" = '' ]
then
echo "user is does not exist!"
exit
fi
for username in ${users}
do
/usr/sbin/userdel -rf ${username} > /dev/null >/dev/null
done
if [ "" = "$?" ]
then
echo "delete ok!"
else
echo "delete failed!"
fi

最新文章

  1. webService-cxf
  2. SharePoint常用目录介绍
  3. atexit函数
  4. Linux diable ipv6
  5. 缓存插件 EHCache 页面缓存CachingFilter
  6. JVM的生命周期
  7. C语言中函数有输出参数
  8. Chrome 开发者工具的Timeline和Profiles提高Web应用程序的性能
  9. 关于KOBE 退役
  10. C语言获取键盘按键
  11. Qss样式(二)
  12. IP 首部检验和算法
  13. WEB标准了解
  14. 防止微信浏览器video标签全屏的问题
  15. #openstack故障处理汇总
  16. 短信外部浏览器H5链接一键跳转微信打开任意站
  17. SAP事物代码
  18. Html表单标签:
  19. 同一台机器安装多个MySQL服务
  20. 如何修改电脑的本地网卡(非笔记本无限网卡)的mac地址

热门文章

  1. mysql中一些表选项
  2. hash值
  3. 两个float 怎么比较大小
  4. SPOJ PGCD
  5. Django新手图文教程-转发
  6. 数百种编程语言,而我为什么要学 Python?
  7. Tajo--一个分布式数据仓库系统(设计架构)
  8. 【JavaScript】函数表达式
  9. 微软.NET Framework cve-2017-8759 复现
  10. [CodeVs1515]跳(lucas定理+费马小定理)