设为首页 收藏本站
查看: 1057|回复: 0

一篇博客学会shell脚本

[复制链接]

尚未签到

发表于 2018-8-20 10:26:10 | 显示全部楼层 |阅读模式
  通过本章学习我们可以学会以下内容:
  一 什么是shell以及shell的好处
  二 shell脚本的格式以及变量的使用方法
  三 test测试语句和if流程控制
  四 case-for-while语句的使用方法
  五 中双小括号的使用方法
  六 循坏嵌套使用方法
  一 什么是shell以及shell的工作方式:
  shell简单来说可以当做人与计算机之间的翻译官,他作为用户与linux系统内部的通信媒介,除了能够支持各种变量与参数外,还可以提供循坏、分之等高级编程语言才有的控制结构特性。
  shell的工作方式有二种:
  交互式:用户每输入一条命令就立即执行。
  批处理:用户先编好一个完整的shell脚本,shell会一次执行脚本的多个命令。
  二 shell脚本的格式以及变量的使用方法
  创建shell脚本的步骤:
  第一步:创建一个包含命令和控制结构的shell文件,一般是后缀.sh
  第二部:修改文件的权限使它可以执行,一般使用chmod U+x 再加文件名
  第三步:执行脚本
  方法一: ./加脚本名称(常用这一种)
  方法二:使用绝对路径
  方法三:bash加文件名
  举例说明:创建一个脚本,查询一下他的路径和包含的文件内容。

  •   [root@baiyijie jiaoben]# vim example01.sh   创建脚本名称
  •   [root@baiyijie jiaoben]# chmod +x example01.sh加上执行权限
  •   [root@baiyijie jiaoben]# ./example01.sh   运行脚本
  •   our first example

  •   we are currently in the following directory.
  •   /root/jiaoben

  •   this directory contains the following files
  •   example01.sh
  •   [root@baiyijie jiaoben]# cat example01.sh查看脚本内容
  •   #!/bin/bash
  •   #this is to show what a example looks like.
  •   echo "our first example"
  •   echo # this inserts an empty line in output .
  •   echo "we are currently in the following directory."
  •   pwd
  •   echo
  •   echo "this directory contains the following files"
  •   ls

  shell变量的使用方法:
  用户定义变量:由字母或者下划线开头,由字母或者下划线组成,大小写字母意义不同,变量长度没有限制。
  使用变量值时:要在变量名前加上前缀"$"
  举例说明:2VAR就是非法变量
  变量赋值:赋值号 = 二边没有空格
  举例说明:
  [root@localhost ~]# A=aaa变量赋值这种正确的
  [root@localhost ~]# A = AAA  这种错误
  -bash: A: command not found
  [root@localhost ~]# echo $A 使用变量
  aaa
  [root@localhost ~]# A=`date` 将一个命令的执行结果赋予变量注意加``
  [root@localhost ~]# echo $A
  Thu Mar 22 10:33:58 CST 2018
  [root@localhost ~]# B=$(ls -l)   B的变量赋值就是一个ls -l的命令
  [root@localhost ~]# echo $B
  total 40 -rw-------. 1 root root 1409 Mar 14 22:00 anaconda-ks.cfg -rwxr-xr-x. 1 root root 75 Mar 16 15:26 backup.sh -rwxr-xr-x. 1 root root 115 Mar 19 16:00 baojing.sh -rwxr-xr-x. 1 root root 195 Mar 16 17:00 caipiao.sh drwxr-xr-x. 2 root root 24 Mar 22 10:11 jiaoben drwxr-xr-x. 4 root root 49 Mar 19 20:38 lianxi -rwxr-xr-x. 1 root root 184 Mar 20 15:46 mizhi.sh -rwxr-xr-x. 1 root root 113 Mar 20 15:05 pingfang.sh -rwxr-xr-x. 1 root root 235 Mar 20 21:26 sanjiao.sh -rwxr-xr-x. 1 root root 44 Mar 19 14:17 shifen.sh -rwxr-xr-x. 1 root root 92 Mar 19 15:53 wokao.sh -rwxr-xr-x. 1 root root 112 Mar 20 15:19 xingqitian.sh drwxr-xr-x. 2 root root 6 Mar 18 13:48 zuoye
  [root@localhost ~]# A=$B    A赋值变量B的内容
  [root@localhost ~]# echo $A
  total 40 -rw-------. 1 root root 1409 Mar 14 22:00 anaconda-ks.cfg -rwxr-xr-x. 1 root root 75 Mar 16 15:26 backup.sh -rwxr-xr-x. 1 root root 115 Mar 19 16:00 baojing.sh -rwxr-xr-x. 1 root root 195 Mar 16 17:00 caipiao.sh drwxr-xr-x. 2 root root 24 Mar 22 10:11 jiaoben drwxr-xr-x. 4 root root 49 Mar 19 20:38 lianxi -rwxr-xr-x. 1 root root 184 Mar 20 15:46 mizhi.sh -rwxr-xr-x. 1 root root 113 Mar 20 15:05 pingfang.sh -rwxr-xr-x. 1 root root 235 Mar 20 21:26 sanjiao.sh -rwxr-xr-x. 1 root root 44 Mar 19 14:17 shifen.sh -rwxr-xr-x. 1 root root 92 Mar 19 15:53 wokao.sh -rwxr-xr-x. 1 root root 112 Mar 20 15:19 xingqitian.sh drwxr-xr-x. 2 root root 6 Mar 18 13:48 zuoye
  [root@localhost ~]# A=date    看这里不加``会有什么后果
  [root@localhost ~]# echo $A
  date    只输出date
  这里要说一下单引号和双引号的区别:
  单引号之间的内容原封不动的指定给了变量
  双引号取消了空格的作用,特殊符号的含义保留。
  列出所有的变量用set
  位置变量和特殊变量
  位置变量:shell 解释执行用户的命令时,将命令执行的第一个字作为命令名,而其他字作为参数,由出现在命令行上的位置确定的参数称为位置参数。位置变量:使用$N来表示。
  举例说明:
  [root@localhost ~]# ./example.sh file1 file2 file3
  $0 这个程序的文件名 example.sh
  $n 这个程序的第n个参数值。n=1..n
  特殊变量:
  有些变量一开始执行脚本的时候就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量,这些变量当一执行程序就有了,以下是一些特殊变量:
  $*这个程序的所有参数
  $#这个程序参数的个数
  $$这个程序的PID
  $!执行上一个后台程序的PID
  $?执行上一个指令的返回值
  举例说明:写一个脚本表示出程序的所有参数进程ID和程序参数的个数。

  •   [root@baiyijie jiaoben]# vim z.sh
  •   [root@baiyijie jiaoben]# chmod +x z.sh
  •   [root@baiyijie jiaoben]# ./z.sh
  •   表示这个程序的所有参数
  •   0 表示这个程序的参数的个数
  •   3486 表示程序进程的ID
  •   3488 执行上一个就太指令的PID
  •   3486 表程序的进程ID
  •   [root@baiyijie jiaoben]# ./z.sh aaa bbb ddd  cccc加入一些参数
  •   aaa bbb ddd cccc 表示这个程序的所有参数
  •   4 表示这个程序的参数的个数
  •   3489 表示程序进程的ID
  •   3491 执行上一个就太指令的PID
  •   3489 表程序的进程ID
  •   [root@baiyijie jiaoben]# cat z.sh查看脚本
  •   #!/bin/bash
  •   echo "$* 表示这个程序的所有参数"
  •   echo "$# 表示这个程序的参数的个数"


  •   touch /tmp/a.txt
  •   echo "$$ 表示程序进程的ID"


  •   touch /tmp/b.txt &
  •   echo "$! 执行上一个就太指令的PID"
  •   echo "$$ 表程序的进程ID"

  三 test测试语句和if流程控制
  && 代表条件性的AND
  || 代表条件性的OR
  test也可以写成[]
  数值测试:-gt 是否大于
  -ge 是否大于等于
  -eq 是否等于
  -ne 是否不等于
  -lt 是否小于
  -le 是否小于等于
  IF语法:
  if条件
  then
  语句
  fi
  扩展; 分号,表示二个命令写在一行,互不影响。
  if语法:
  if 条件 ;then
  命令1
  else
  命令2
  fi
  举例说明;写一个脚本,可以判断他是目录,或者是普通文件,或者是设备文件。

  •   [root@localhost ~]# vim fuza.sh
  •   [root@localhost ~]# chmod +x fuza.sh
  •   [root@localhost ~]# ./fuza.sh      执行脚本
  •   ./fuza.sh: line 1: i#/bin/bash: No such file or directory
  •   input a file name    让我输入一个文件名
  •   /etc                  输入/etc
  •   /etc is a dir         系统判断他是一个目录
  •   [root@localhost ~]# ./fuza.sh
  •   ./fuza.sh: line 1: i#/bin/bash: No such file or directory
  •   input a file name
  •   /etc/passwd
  •   /etc/passwd is file
  •   [root@localhost ~]# ./fuza.sh
  •   ./fuza.sh: line 1: i#/bin/bash: No such file or directory
  •   input a file name
  •   hhhk
  •   hhhk is an unknow file
  •   [root@localhost ~]# ./fuza.sh
  •   ./fuza.sh: line 1: i#/bin/bash: No such file or directory
  •   input a file name
  •   /dev/sda
  •   /dev/sda is a device file      说它是一个设备文件
  •   [root@localhost ~]# cat fuza.sh
  •   i#/bin/bash
  •   echo "input a file name"
  •   read file_name
  •   if [ -d $file_name ] ; then
  •   echo "$file_name is a dir"
  •   elif [ -f $file_name ] ;then
  •   echo "$file_name is file"
  •   elif [ -c $file_name -o -b $file_name ] ; then
  •   echo "$file_name is a device file"
  •   else
  •   echo "$file_name is an unknow file"
  •   fi
  四 case-for-while语句的使用方法
  case: 流程控制语句,适用于多分支。
  格式:
  case 变量 in
  字符串1)命令列表1
  ;;
  ....
  字符串n)命令列表n
  ;;
  esac
DSC0000.jpg

  举例说明:
  制作一个菜单,可以选择bcd,分别代表backup copy delete
DSC0001.jpg

  文字部分

  •   [root@localhost lianxi]# vim case.sh
  •   [root@localhost lianxi]# chmod +x case.sh
  •   [root@localhost lianxi]# ./case.sh     执行脚本
  •   ****************************
  •   Please selet you operation:
  •   1 Copy
  •   2 Delete
  •   3 Backup
  •   *****************************
  •   C                 我输入C
  •   your seletion is copy  说我选择的是copy
  •   [root@localhost lianxi]# ./case.sh
  •   ****************************
  •   Please selet you operation:
  •   1 Copy
  •   2 Delete
  •   3 Backup
  •   *****************************
  •   D
  •   your seletion is delete
  •   [root@localhost lianxi]# ./case.sh
  •   ****************************
  •   Please selet you operation:
  •   1 Copy
  •   2 Delete
  •   3 Backup
  •   *****************************
  •   B
  •   your seletion is backup
  •   [root@localhost lianxi]# cat case.sh
  •   #!/bin/bash
  •   echo "****************************"
  •   echo "Please selet you operation:"
  •   echo " 1 Copy"
  •   echo " 2 Delete"
  •   echo " 3 Backup"
  •   echo "*****************************"
  •   read op
  •   case $op in
  •   C)
  •   echo "your seletion is copy"
  •   ;;
  •   D)
  •   echo "your seletion is delete"
  •   ;;
  •   B)
  •   echo "your seletion is backup"
  •   ;;
  •   *) # 参数*, 匹配所有参数。
  •   echo "invalide selection"
  •   ;;
  •   esac

  whlie 是循坏语句,用在循坏中
  格式:
  while 条件
  do
  命令
  done
  话不多说,继续举例说明:
  算出一到十的平方,用脚本写出来。
  脚本内容:
DSC0002.jpg

  文字说明:

  •   [root@localhost ~]# vim pingfang.sh
  •   [root@localhost ~]# chmod +x pingfang.sh
  •   [root@localhost ~]# ./pingfang.sh
  •   1
  •   4
  •   9
  •   16
  •   25
  •   36
  •   49
  •   64
  •   81
  •   100
  •   [root@localhost ~]# cat pingfang.sh
  •   #!/bin/bash
  •   num=1
  •   while [ $num -le 10 ]      当变量小于10的时候执行下面的动作
  •   do
  •   square=`expr $num \* $num`     变量的值相乘。中间要用转移符
  •   echo $square
  •   num=`expr $num + 1`     每次执行加一下
  •   done

  五 中双小括号的使用方法
  使用(())扩展shell中算数运算的使用方法,使用[]的时候,必须保证运算符与算数之间有空格,四则运算也只能借助expr命令来完成,我们今天的说的(())结构语句,就是对shell中算数及赋值运算的扩展。
  举例说明:
  依次输出小于100以内2的幂值,输出的结果应该为:2 4 8 16。
  脚本的内容:
DSC0003.jpg

  文字解释:

  •   [root@localhost ~]# vim mizhi.sh
  •   [root@localhost ~]# chmod +x mizhi.sh
  •   [root@localhost ~]# ./mizhi.sh
  •   the while loop example
  •   value of the variable is : 1
  •   value of the variable is : 2
  •   value of the variable is : 4
  •   value of the variable is : 8
  •   value of the variable is : 16
  •   value of the variable is : 32
  •   value of the variable is : 64
  •   the loop execution is finished
  •   [root@localhost ~]# cat mizhi.sh  查看一下脚本内容
  •   #!/bin/bash
  •   echo "the while loop example"
  •   echo
  •   var1=1
  •   while ((var1

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-554168-1-1.html 上篇帖子: 用shell写的99乘法表 下篇帖子: shell脚本之全库热备份
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表