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 ...
随机推荐
- iOS - Photo Album 图片/相册管理
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...
- xml配置文件详解
1:bean的基本属性配置: <!-- id是bean的标识符,必须唯一,如果没有配置id,name默认为标识符 如果配置了id,有配置了name,那么name为别名 name可以设置多个别名, ...
- JavaWeb学习总结(十二)--事务
一.事务的介绍 1.1 什么是事务 银行转账!张三转10000块到李四的账户,这其实需要两条SQL语句: 给张三的账户减去10000元: 给李四的账户加上10000元. 如果在第一条SQL语句执行成功 ...
- JavaWeb学习总结(九)--JDBC入门
一.什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库 ...
- Python学习笔记17—Tornado
实例 #!/usr/bin/env Python #coding:utf-8 import tornado.httpserver import tornado.ioloop import tornad ...
- iOS 开发之照片框架详解(1)
http://kayosite.com/ios-development-and-detail-of-photo-framework.html/comment-page-1 一. 概要 在 iOS 设备 ...
- 值类型,引用类型,栈,堆,ref,out
在网上收集... C#的值类型,引用类型,栈,堆,ref,out C# 的类型系统可分为两种类型,一是值类型,一是引用类型,这个每个C#程序员都了解.还有托管堆,栈,ref,out等等概念也是每个C# ...
- EMV技术学习和研究(转)
刚开始学习EMV&PBOC,磕磕碰碰,感谢xuture的<EMV技术学习和研究>给了很大帮助,让我少走了很多弯路,也感谢广俊.surge.艾零.小SO.Spinach.龙行天下的帮 ...
- IIS_各种问题
IIS7中默认是已经加载了脚本映射处理.但今天装了个WIN7,装好IIS后却发现没有.于是手动去这安装,在添加html映射时提示:模块列表中必须要有IsapiModule或cgiModule 因为 I ...
- DRUPAL点滴
1 admin/config/user-interface/xxx在hook_menu里定义这样一个path,就会在configuration界面看到xxx的链接 2 $news_items = db ...