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 MVC开发过程中,使用到的方法(内置方法及使用说明)
® 视图的返回使用案例: [HttpGet] [SupportFilter] public ActionResult UserTopic(string type, string TopPicId, s ...
- Spring 读书笔记-----使用Spring容器(一)
pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...
- mysql 权限控制
1.mysql的权限是,从某处来的用户对某对象的权限. 2.mysql的权限采用白名单策略,指定用户能做什么,没有指定的都不能做. 3.权限校验分成两个步骤: a.能不能连接,检查从哪里来,用户名和密 ...
- 转:C++中Static作用和使用方法
转自:http://blog.csdn.net/artechtor/article/details/2312766 1.什么是static? static 是C++中很常用的修饰符,它被用 ...
- Sqlserver_left join 、right join、 inner join 用法
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- IEnumerable接口的实现
对象要实现可以迭代需IEnumerable接口并实现GetEnumerator方法.一下简单例子 public class SPEnumerable<T> : IEnumerable { ...
- 能源项目xml文件标签释义--DataSource
<bean id="dataSource1" class="org.apache.tomcat.jdbc.pool.DataSource" destroy ...
- JavaScript document
window -- document用于表现HTML页面当前窗体的内容 document,中文"文档" document是BOM中最重要对象之一 document对象是window ...
- program testy data
做项目得用数据吧,拿去.... 1.Data.gov搜索 美国政府去年承诺使所有政府数据都能在网上免费获得.这个网站是第一阶段,作为一个门户网站,囊括了从气候到犯罪的一切惊人的信息. 2. 美 ...
- 3D正方体
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...