shell脚本初析
简单理解:运用很多工具,将复杂的步骤简单化,体现在shell脚本中
框架:必须有备注,写的别人能够看得懂
开头:#! /bin/bash 代表的意思是改文件使用的是bash语法
要想使用该种方法运行shell脚本,前提是脚本本身有执行权限,所以需要给脚本加一个 ‘x’ 权限。另外使用sh命令去执行一个shell脚本的时候是可以加-x选项来查看这个脚本执行过程的,这样有利于我们调试这个脚本哪里出了问题
chmod +x first.sh #加权限
. /first.sh #执行
date +"%Y-%m-%d %H:%M:%S" #这个例子很实用,查看时间
1.shell脚本之加法
read -p "Please input a number: " x
read -p "Please input another number: " y
sum=$[$x+$y]
echo "The sum of the two numbers is: $sum"
2.shell脚本中的逻辑判断之else
if 判断语句; then
command
fi
for example:
read -p "Please input your score: " a
if (($a<60)); then
echo "You didn't pass the exam."
fi
(($a<60)) 这样的形式,这是shell脚本中特有的格式,用一个小括号或者不用都会报错,记住这个格式
带有else
if 判断语句 ;then
command
else
command
fi
for example:
read -p "please input your score: "a
if(($a<60)); then
echo "you didn't pass the exam"
else
echo "you pass the exam.good!"
fi
带有elif
if 判断语句一 ; then
command
elif 判断语句二 ;then
command
else
command
fi
for example:
read -p "Please input your score: " a
if (($a<60)); then
echo "You didn't pass the exam."
elif (($a>=60)) && (($a<85)); then
echo "Good! You pass the exam."
else
echo "very good! Your socre is very high!"
fi
利用case判断:
case 变量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac
for example:
read -p "Input a number: " n
a=$[$n%2]
case $a in
1)
echo "The number is odd."
;;
0)
echo "The number is even."
;;
*)
echo "It's not a number!"
;;
esac
3.shell 脚本中if还经常判断关于档案属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的也就几个选项:
-e :判断文件或目录是否存在
-d :判断是不是目录,并是否存在
-f :判断是否是普通文件,并存在
-r :判断文档是否有读权限
-w :判断是否有写权限
-x :判断是否可执行
使用if判断时,具体格式为:if [ -e filename ] ; then
[root@localhost sbin]# if [ -f /root/test.txt ]; then echo ok; fi
ok
注意:-lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)
4.shell中的循环
for循环的基本结构:
for 变量名 in 循环条件; do
command
done
for example:
for i in `seq 1 5`; do
echo $i
done
while循环基本结构:
while 条件;do
command
done
for example:
a=5
while [ $a -ge 1 ]; do
echo $a
a=$[$a-1]
done
可以把循环条件拿一个冒号替代,这样可以做到死循环
while :; do
command
sleep 3
done
5.shell中的函数调用
function 函数名() {
command
}
for example:
function sum()
{
sum=$[$1+$2]
echo $sum
}
sum $1 $2
注意:函数一定要写在最前面,不能出现在中间或者最后,因为函数是要被调用的。
shell脚本初析的更多相关文章
- shell脚本报错:"[: =: unary operator expected"
shell脚本报错:"[: =: unary operator expected" 在匹配字符串相等时,我用了类似这样的语句: if [ $STATUS == "OK&q ...
- 通过shell脚本来rerun一个oozie调度失败的job,从而可以跳过执行失败的节点
标题很长:通过shell脚本来rerun一个oozie调度失败的job,从而可以跳过执行失败的节点 不过目前从oozie调度测试的例子来看,oozie本身的retry好像并没有参数可以控制跳过失败的节 ...
- shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory
当我们把文件从windows系统中编辑的文件拷贝到linux系统中,如果我们执行文件会保存如下的错: shell脚本报错:-bash: xxx: /bin/bash^M: bad interprete ...
- 此贴告诉你:为啥shell脚本人,不建议学python
py很强大,我承认.但在运维方面,py不但不强大,还有硬伤.正因为有下述硬伤,所以我们运维,还是用shell多,用py极少.我看到用shell的人很多,你建议人用python,人说py是很好,但下一秒 ...
- CRLF line terminators导致shell脚本报错:command not found
Linux和Windows文本文件的行结束标志不同.在Linux中,文本文件用"/n"表示回车换行,而Windows用"/r/n"表示回车换行.有时候在Wind ...
- ubuntu终端执行shell脚本报command not found解决方法
使用sudo执行脚本报错:sudo: myshell.sh: command not found 原因:发生这种情况的原因是因为您正在尝试执行的脚本需要正确的权限 解决:执行sudo chmod a+ ...
- Shell脚本报错:-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory
在学习shell中测试case参数命令代码如下 #!/bin/bash #switch测试 case $1 in start) echo 'start' ;; ...
- Linux shell 脚本报错:/bin/bash^M: bad interpreter: No such file or directory
今天遇到一个很诡异的问题,一直运行很正常的shell脚本失败了,只是昨天增加了一个参数而已. 报错信息: /bin/bash^M: bad interpreter: No such file or d ...
- CRLF line terminators导致shell脚本报错:command not found --转载
Linux和Windows文本文件的行结束标志不同.在Linux中,文本文件用"/n"表示回车换行,而Windows用"/r/n"表示回车换行.有时候在Wind ...
随机推荐
- MyEclipse JCO tomcat 提示查找不到sapjco3.dll
java.lang.UnsatisfiedLinkError: no sapjco3 in java.library.path 1.system32添加sapjco3.dll 2.tomcat bin ...
- 抓包工具__Windows
我常使用的: 1. SoftPerfect Network Protocol Analyzer 2. fiddler . fiddler2 3.
- 集群--LVS的DR模型配置
1.查看内核是否有IPVS内核模块 grep -i 'ip_vs' /boot/config-2.6.32-431.el6.x86_64
- The Zen Programmer (zhuan)
http://blog.csdn.NET/marksinoberg/article/details/52460725 ***************************************** ...
- OpenGL的glScalef缩放变换函数详解
glScalef是openGL中的模型缩放函数,就是把当前矩阵与一个表示延各个轴对物体进行拉伸.压缩.反射的矩阵相乘.这个物体的每个点的x,y,z坐标与对应的xyz参数相乘. 先看函数定义void g ...
- python 打包与部署
环境:win10.eclipse-jee-mars.python2.7 打包在linux上进行安装测试 1.1 打包 项目目录结构如下: 打包对象:utils.reg 在P1项目的顶级目录,也就是ut ...
- 搭建一个简单的Struts2框架
1 创建一个web项目. 2 导入必要的JAR文件. 放在WEB-INF下lib包里. 3 添加web.xml配置,添加启动配置. <?xml version="1.0" ...
- SqlServer_Sql防止注入
注入式攻击的详细解释SQL下面我们将以一个简单的用户登陆为例,结合代码详细解释一下SQL注入式攻击,与及他的防范措施.对于一个简单的用户登陆可能的代码如下:try{ string strUserNam ...
- python操作mongodb之六自定义类型存储
from pymongo.mongo_client import MongoClient client=MongoClient('192.168.30.252',27017) client=drop_ ...
- 腾讯云从零部署nodejs站点
版权声明:本文由袁飞翔原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/176 来源:腾云阁 https://www.qclo ...