简单理解:运用很多工具,将复杂的步骤简单化,体现在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脚本初析的更多相关文章

  1. shell脚本报错:"[: =: unary operator expected"

    shell脚本报错:"[: =: unary operator expected" 在匹配字符串相等时,我用了类似这样的语句: if [ $STATUS == "OK&q ...

  2. 通过shell脚本来rerun一个oozie调度失败的job,从而可以跳过执行失败的节点

    标题很长:通过shell脚本来rerun一个oozie调度失败的job,从而可以跳过执行失败的节点 不过目前从oozie调度测试的例子来看,oozie本身的retry好像并没有参数可以控制跳过失败的节 ...

  3. shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory

    当我们把文件从windows系统中编辑的文件拷贝到linux系统中,如果我们执行文件会保存如下的错: shell脚本报错:-bash: xxx: /bin/bash^M: bad interprete ...

  4. 此贴告诉你:为啥shell脚本人,不建议学python

    py很强大,我承认.但在运维方面,py不但不强大,还有硬伤.正因为有下述硬伤,所以我们运维,还是用shell多,用py极少.我看到用shell的人很多,你建议人用python,人说py是很好,但下一秒 ...

  5. CRLF line terminators导致shell脚本报错:command not found

    Linux和Windows文本文件的行结束标志不同.在Linux中,文本文件用"/n"表示回车换行,而Windows用"/r/n"表示回车换行.有时候在Wind ...

  6. ubuntu终端执行shell脚本报command not found解决方法

    使用sudo执行脚本报错:sudo: myshell.sh: command not found 原因:发生这种情况的原因是因为您正在尝试执行的脚本需要正确的权限 解决:执行sudo chmod a+ ...

  7. 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'     ;;      ...

  8. Linux shell 脚本报错:/bin/bash^M: bad interpreter: No such file or directory

    今天遇到一个很诡异的问题,一直运行很正常的shell脚本失败了,只是昨天增加了一个参数而已. 报错信息: /bin/bash^M: bad interpreter: No such file or d ...

  9. CRLF line terminators导致shell脚本报错:command not found --转载

    Linux和Windows文本文件的行结束标志不同.在Linux中,文本文件用"/n"表示回车换行,而Windows用"/r/n"表示回车换行.有时候在Wind ...

随机推荐

  1. ASP.NET调用Office Com组件权限设置

    ASP.NET在调用Office Com组件时,经常会出现权限限制的问题,而出现如下错误: 现通过以下几步设置,可解决上述问题:(1)64位系统中,请在IIS应用程序池集成模式中应启用调用32位应用程 ...

  2. HDU-4511 小明系列故事——女友的考验 floyd变种-标号递增最短路

    题意:给定N个点,现在要求出从1号点到N号点的最短路.题目给的限制条件就是对于某条路径是不能够走的,但是可以选择某段路径走,另外就是所走的路径的标号必须是递增的. 分析:由于给定的是一些列的坐标点,这 ...

  3. struts2 if标签示例

    下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...

  4. Windows_cmd_命令

    1. netstat -ano  查看端口占用情况 netstat -anp // 命令来查看一下,Linux系统是否在监听 3306 这个端口号 2.

  5. poj1474Video Surveillance(半平面交)

    链接 半平面交的模板题,判断有没有核.: 注意一下最后的核可能为一条线,面积也是为0的,但却是有的. #include<iostream> #include <stdio.h> ...

  6. hdu4720Naive and Silly Muggles

    链接 一直理解的最小覆盖圆就是外接圆..原来还要分钝角和锐角... 钝角的话就为最长边的中点,对于这题分别枚举一下外接圆以及中点的圆,判一下是不是在园外. #include <iostream& ...

  7. Java字符串处理函数

    substring() 它有两种形式,第一种是:String substring(int startIndex)第二种是:String substring(int startIndex,int end ...

  8. matplotlib库的常用知识

    看看matplotlib是什么? matplotlib是python上的一个2D绘图库,它可以在夸平台上边出很多高质量的图像.综旨就是让简单的事变得更简单,让复杂的事变得可能.我们可以用matplot ...

  9. Java后端开发

    Java后端开发 名称 内容 基本框架 Spring.Mybatis Linux服务器   数据库优化   消息服务 rabbitMQ.activeMq rocketMq 缓存服务 memcached ...

  10. STM32学习笔记(三) STM32的GPIO的深入学习

    STM32的开发学习主要涉及软硬件两个部分的实现,包含众多外设和总线的理解配置.STM32的整个学习曲线并不陡峭,但入门却相当困难,因此在学习之初,多动手实验和测试相当重要,GPIO作为整个STM32 ...