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 ...
随机推荐
- 详解 ASP.NET异步
在前文中,介绍了.NET下的多种异步的形式,在WEB程序中,天生就是多线程的,因此使用异步应该更为谨慎.本文将着重展开ASP.NET中的异步. [注意]本文中提到的异步指的是服务器端异步,而非客户端异 ...
- SQL中char、varchar、nvarchar的区别(zhuan)
char char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符. nvarcha ...
- Win7x64_chromeX86_相关路径
1. C:\Users\33\AppData\Local\Google 里面有2个文件夹:“Chrome”.“CrashReports” 2. C:\Program Files (x86)\Googl ...
- [转载] zookeeper faq
Zookeeper FAQ1. 如何处理CONNECTION_LOSS?在Zookeeper中,服务器和客户端之间维持一个长连接,CONNECTION_LOSS意味着这个连接断开了.客户端API返回C ...
- MyEclipse8.5启动无法选择工作空间的问题
方法一:打开Window---Preferences---General---Startup and Shutdown,勾选Prompt for workspace on startup 选项,再次登 ...
- Android开发面试经——3.常见Java基础笔试题
Android开发(29) 版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...
- 【转载】Linux系统,设置Oracle开机启动,待整理
http://www.cnblogs.com/mophee/archive/2013/06/03/3115805.html
- Python 调用自定义包
创建包 # mkdir -p /python/utils # touch /python/utils/__init__.py # vi /python/utils/Log.pyimport timed ...
- 删除github账号的方法
如果你不想使用自己的github账号了,github官网允许你删除账号,具体操作步骤为: 1.登录github网站,网站右上角的头像,在下拉菜单中选择"Settings"(设置): ...
- QT对话框模式与非模式
QT模态对话框及非模态对话框 非模态对话框(Modeless Dialog)的概念不是模态对话框就是在其没有被关闭之前,用户不能与同一个应用程序的其他窗口进行交互,直到该对话框关闭.对于在模态来显示对 ...