【shell】shell编程总结
总结一下在写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
【shell】shell编程总结的更多相关文章
- Shell脚本编程30分钟入门
Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- Shell高级编程视频教程-跟着老男孩一步步学习Shell高级编程实战视频教程
Shell高级编程视频教程-跟着老男孩一步步学习Shell高级编程实战视频教程 教程简介: 本教程共71节,主要介绍了shell的相关知识教程,如shell编程需要的基础知识储备.shell脚本概念介 ...
- Linux shell脚本编程基础之练习篇
shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...
- 【Linux】Shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- 学习笔记之Linux Shell脚本教程:30分钟玩转Shell脚本编程
Linux Shell脚本教程:30分钟玩转Shell脚本编程 http://c.biancheng.net/cpp/shell/
- Shell脚本编程总结及速查手册
Shell是一种编程语言, 它像其它编程语言如: C, Java, Python等一样也有变量/函数/运算符/if语句/循环控制/… 但在开始之前, 我想先理清Shell语言与Shell之间的关系. ...
- 跟着老男孩一步步学习Shell高级编程实战
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://oldboy.blog.51cto.com/2561410/1264627 本sh ...
随机推荐
- 【.Net】C# 将Access中时间段条件查询的数据添加到ListView中
一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Columns集合中添加表头中的文字. 二.利用代码给ListView添加 ...
- 解决:warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;找到 MSIL .netmodule 或使用 /GL 编译的模块;正在。。;LINK : warning LNK4075: 忽略“/INCREMENTAL”(由于“/LTCG”规范)
原文链接地址:https://www.cnblogs.com/qrlozte/p/4844411.html 参考资料: http://blog.csdn.net/laogaoav/article/de ...
- Alpha 冲刺 —— 十分之一
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作,对多个目标检测及文字识别模型进行评估.实验,选取较 ...
- 浴谷金秋线上集训营 T11738 伪神(树链剖分)
先树链剖分,一棵子树的编号在数组上连续,一条链用树链剖分,把这些线段全部取出来,做差分,找到有多少点被>=t条线段覆盖即可. #include<iostream> #include& ...
- [HEOI2015]公约数数列
不错的分块题 gcd和xor其实并没有联系 这里,xor的按位性质没有半点卵用 gcd的性质却很关键: 一个数组,前缀gcd最多logn个不同的 gcd不太多,(暴力的基础) 所有考虑分块. 分块,每 ...
- 解决eclipse快捷键Ctrl+Alt+Down冲突问题办法
解决eclipse快捷键Ctrl+Alt+Down冲突问题办法 时间:2016-01-18 21:11:08 阅读:376 评论:0 收藏:0 [点我收藏+] ...
- Topcoder SRM570 D1L3 CurvyonRails
几个样例: 5 5wCCwwwCC....w......www..wReturns: 0 3 3C.w....C.Returns: 1 21 20CC..CCCw.CwC..CC.w.CC.CCCwC ...
- 解决SQL Server 2008 64位系统无法导入Access/Excel的问题
最近更换了新服务器,操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasou ...
- SpringCloud学习(2)——Rest微服务案例
创建父工程: microservicecloud 创建公共模块api:microservicecloudapi SQL脚本: 此学习路线总共创建3个库, 分别为clouddb01, clouddb0 ...
- Handlebars 使用
引入js <script src="js/json3.min.js"></script> <script src="js/handlebar ...