【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 ...
随机推荐
- android面试(5)---SQL数据库
SQL基础: 1.如何查询table1从20到30条记录: select * from table1 limit 19,11 2.替换id=1,name =deman的记录? replace into ...
- [poi2011]bzoj 2277 —— strongbox·[洛谷3518]
·问题描述· 有一个密码箱,0到n-1中的某些数是它的密码.且满足:如果a和b都是它的密码,那么(a+b)%n也是它的密码.某人试了k次密码,前k-1次都失败了,最后一次成功. 问:该密码箱最多有多少 ...
- P2764 最小路径覆盖问题(网络流24题之一)
题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...
- 创建Django工程-Day19
1. 新建一个day19的工程和app01. 2. 新建templates和static的文件夹. 3. 去settings.py中去做配置. 1)注释掉csrf 2)配置模板路径 'DIRS': [ ...
- php+memcached缓存技术实例
一.memcached 简介 在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东.这里简单介绍一下,memcached 是高 ...
- 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告
P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...
- 【bzoj3567】江南乐
Portal -->bzoj3567 Solution 今天开始啃博弈论了qwq 先mark一篇很棒的博客Portal -->博弈论学习资料 稍微总结一下两个自己容易混 ...
- 流媒体协议之JRTPLIB的使用20170919
主要介绍JRTPLIB 2.x系列和3.x系列两种版本,它们的区别是2.x系列代码量少使用简单,但是只支持RFC 1889不支持RFC 3550,3.x支持RFC 3550,但代码量稍多,以及使用也稍 ...
- laravel 5.1 Eloquent常见问题
1.新增一条记录以及判断是否新增成功 $instance = XxxModel::create(['a' => 1, 'b' => 2]); if ($instance->exist ...
- TCP/IP地址格式转换API
1.htonl ()和ntohl( ) ntohl( )-----网络顺序转换成主机顺序(长整型) u_long PASCAL FAR ntohl (u_long netlong); htonl ( ...