1 shell 脚本文件

shell 解释器可当作人与计算机硬件的“翻译官”, 他作为用户与linux系统内部的通信媒介,除了能够支持各种变量与参数外,还提供了像判断,循环等高级编程语言有的控制流程的特性。 想要正确高效的做好系统运维工作,脚本的使用至关重要。

shell脚本的工作方式有两种:交互式和批处理。

  • 交互式(interactive) : 用户输入一条就立即执行
  • 批处理(batch): 由用户事先编写好一个完整的shell脚本,shell会一次性执行脚本中诸多的命令。

脚本中不仅会用到一般的linux命令、管道符、重定向,还需要把内部功能模块化后通过逻辑语句进行处理,最终形成日常使用的脚本。

2 编写简单的脚本

实际上,使用vim将命令写入到一个文件中,就是一个简单的脚本了

[root@linuxlearner Desktop]# vim demo.txt
#!/bin/bash
# this is a demo
pwd
ls
[root@linuxlearner Desktop]# bash demo.txt
/home/redhat/Desktop
demo.txt

shell脚本的名称可以任意,但是为了区分,一般以.sh后缀,来表明这是一个脚本文件。

一般脚本的第一行脚声明 # !用来告诉系统使用哪种shell解释器来执行该脚本
第二行是对该脚本的注释信息,以便让后来使用脚本的人了解该脚本的功能或警告信息
之后是脚本的命令

另外也可以通过脚本的路径来执行脚本。

3 脚本参数

上述的脚本只能执行一些预先定义好的命令,让shell脚本程序更好地满足用户的实时需求,以便灵活完成工作,必须要让脚本像交互式执行命令一样,能够接收用户的参数。
实际上,脚本文件就可以接受参数,每个参数以空格隔开。

  1. $0代表当前脚本程序的名称
  2. $N 代表第N个参数, 如$1为第一个参数,$2为第二个参数。。。
  3. $* 为所有的参数
  4. $?为上一次命令执行的返回值

比如下例:

[root@linuxlearner Desktop] vim example.sh
#!/bin/bash
# this a example scrip to demonstrate the usage of arguments
echo the name of this script is $0
echo the first argument of this script is $1
echo the second argument of this script is $2
echo there are $# arguments totally, they $* [root@linuxlearner Desktop]# bash example.sh hello world
the name of this script is example.sh
the first argument of this script is hello
the second argument of this script is world
there are 2 arguments totally, they hello world

4 脚本参数的判断

系统可以对用户输出的参数进行判断和测试,按照测试对象来划分,测试语句可以分为四种:

4.1 文件测试语句

用来判断文件是否存在或权限是否满足等情况的运算符。 具体用法如下

运算符 作用
-d 测试文件是否目录
-e 测试文件是否存在
-f 判断是否问一般文件
-r 测试当前用户是否有权限读取
-w 测试当前用户是否有权限写入
-x 测试当前用户是否有权限执行

测试 /etc/fstab 是否为一个目录类型的文件,然后通过shell解释器内设的$?来显示上一条命令的执行就结果。如果返回值为0,则存在。非0值,则不存在。

[root@linuxlearner Desktop]# [ -d /etc/fstab ]
[root@linuxlearner Desktop]# echo $?
1

[ 与 ] 本身也是命令,所以要空格隔开

4.2 逻辑测试语句

逻辑语句对于测试结果进行逻辑分析,可根据测试结果可以实现不同的效果。

4.2.1 并 &&

逻辑并表示两者都为真则为真,所以第一条为假则为假,第一条为真时要判断第二句,也就是说,&&前面的语句执行成功后才会执行后面的命令。

[root@linuxlearner Desktop]# [ -d /home/redhat/Desktop ] && echo "this file is a directory"
this file is a directory

4.2.2 或 ||

逻辑或表示有一个为真则为真,所以第一条为真则为真,第一条为假时要判断第二句,也就是说,||前面的语句执行为假才会执行后面的命令。

[root@linuxlearner Desktop]# [ -d /home/redhat/Desktop/example.sh ] || echo "this file is not a directory"
this file is not a directory

4.2.3 非 !

它表示把判断结果取相反值 。

[redhat@linuxlearner /]$ [ ! $USER = root ] && echo "this is not administrator"
this is not administrator

4.3 整数值比较语句

由于大于号小于号与定向符号冲突了,所以需要使用整数比较运算符:

运算符 含义
-eq (equal) 等于
-ne(not equal) 不等于
-gt(greater then ) 大于
-lt(less than) 小于
-ge(greater than or equal) 大于等于
-le(less than or equal) 小于等于
[redhat@linuxlearner /]$ [ 1 -lt 2 ] && echo "right"
right
[redhat@linuxlearner /]$ [ 1 -lt 2 ] && [ 1 -eq 1 ]
[redhat@linuxlearner /]$ echo $?
0

4.4 字符串比较语句

字符串的比较语句用于判断测试字符串是否为空值,或者两个字符串是否相同,它经常用来判断某个变量是否被定义(即内容为空值)。

运算符 含义
= 字符串内容是否相同
!= 是否不同
-z 判断字符串是否为空, 空为真
[root@linuxlearner /]# [ ! -z $SHELL  ] &&  echo "this variable exist"
this variable exist

5 脚本的流程控制

尽量上述的测试语句可以完成基本的流程控制,但这并不够灵活,在生产环境中我们需要通过if、 for、while、case四种流程控制语句编写更灵活复杂的脚本。这四个语句逻辑上和其他语言的用法并没有区别,只是语法的区别。这里不再赘述了,直接给出例子展示语法。

if

测试是否能与主机通信

[root@linuxlearner Desktop]# vim chkhost.sh
#!/bin/bash
# determine whether host is online
ping -c 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "host $1 is online"
else
echo "host $1 is offline"
fi[root@linuxlearner Desktop]# bash chkhost.sh
host  is offline
[root@linuxlearner Desktop]# bash chkhost.sh 192.168.10.10
host 192.168.10.10 is online
[root@linuxlearner Desktop]# bash chkhost.sh 192.168.10.20
host 192.168.10.20 is offline

多分支例子,输入分数后输出评价。

[root@linuxlearner Desktop]#vim chksceore.sh
#! /bin/bash
# input an interger score, output an evalution
read -p "enter your score(0-100):" GRADE
if [ $GRADE -gt 100 ] || [ $GRADE -lt 0 ]
then echo "score is invalid"
elif [ $GRADE -ge 85 ] && [ $GRADE -le 100 ]
then echo "excellent"
elif [ $GRADE -ge 60 ] && [ $GRADE -lt 85 ]
then echo "pass"
else
echo "fail"
fi
[root@linuxlearner Desktop]# bash chksceore.sh 100
enter your score(0-100):100
excellent
[root@linuxlearner Desktop]# bash chksceore.sh
enter your score(0-100):120
score is invalid
[root@linuxlearner Desktop]# bash chksceore.sh
enter your score(0-100): -1
score is invalid
[root@linuxlearner Desktop]# bash chksceore.sh
enter your score(0-100):40
fail

for

从列表文件中读取多个用户名,然后逐一创建用户账户并设置密码。

#新建带创建用户名单
[root@linuxlearner Desktop]# vim users.txt
andy
barry
jack
simmen
tracy
# 新建脚本
#!/bin/bash
# read name then cerate an acount
read -p "enter the users password:" PASSWD
for UNAME in `cat users.txt`
doid $UNAME &> /dev/nullif [ $? -eq 0 ]thenecho "already exists"elseuseradd $UNAME &> /dev/nullecho $PASSWD | passwd --stdin $UNAME &> /dev/nullif [ $? -eq 0 ]thenecho "$UNAME, create success" elseecho "$UNAME, cerate failure" fifi
done# 运行脚本
[root@linuxlearner Desktop]# bash createacount.sh
enter the users password:101650
andy, create success
barry, create success
jack, create success
simmen, create success
tracy, create success

while

编写一个猜数字大小的脚本

[root@linuxlearner Desktop]# vim guess.sh 、
#! /bin/bash
# guess which the integer is
INTEGER=$(expr $RANDOM % 1000)
TIMES=0
echo "there is a integer between 0 and 999, guess who it is"
echo $INTEGER
while true
do
read -p "please guess:" INT
let TIMES++
if [ $INT -eq  $INTEGER ]
then
echo "congratulations, your answar is right, the true integer is $INTEGER"
echo "you guessed $TIMES times"
exit 0
elif [ $INT -gt $INTEGER ]
then
echo "too big"
else
echo "too small"
fi
done
[root@linuxlearner Desktop]# bash guess.sh
there is a integer between 0 and 999, guess who it is
please guess:500
too big
please guess:250
too small
please guess:375
too big
please guess:320
too big
please guess:300
too big
please guess:265
congratulations, your answar is right, the true integer is 265
you guessed 6 times

case

[root@linuxlearner Desktop]# vim checkkey.sh
#!/bin/bash
read -p "please anter a string:" KEY
case "$KEY" in
[a-z] | [A-Z])
echo "what your input is alphabeta"
;;
[0-9])
echo "what you input is number"
;;
*)
echo "what you input is space or other control string"
esac
[root@linuxlearner Desktop]# bash checkkey.sh
please anter a string:1
what you input is number
[root@linuxlearner Desktop]# bash checkkey.sh
please anter a string:t
what your input is alpha
[root@linuxlearner Desktop]# bash checkkey.sh
please anter a string:
what you input is spass or other control string

linux编写脚本的基本方法相关推荐

  1. linux y脚本,Linux中脚本的使用方法

    Linux中脚本的使用方法 一.前言 关于Linux中的脚本的用法,一直没有时间去好好地总结,正好今天下雨,就好好的整理一下思路吧,其实精通了一门语言,比如C语言,学习其他语言需要的成本是非常少的,同 ...

  2. Linux编写脚本实现统计文件和目录

    Linux编写脚本实现统计任意目录和文件功能 任务 实现 练习 任务 编写实现统计任意目录下有多少个文件和文件夹(命令:egrep.wc.ll) 1.需要使用到位置变量$1.$2.$3,用来给脚本传递 ...

  3. Linux编写脚本查看mod,Linux shell脚本编写基础

    在进行linux测试时编写脚本是必不可少的,Shell脚本的名称可以随便定义,也不要什么后缀名,例如可以写abc,smartzip这类名称,运行时只要键入 ./smartzip就能运行脚本了.. 每行 ...

  4. Linux编写脚本nsum求和,详解Linux Shell脚本编写技巧,附实例说明

    原标题:详解Linux Shell脚本编写技巧,附实例说明 Linux Shell是一个很难的知识板块.虽然大家都认真学,基本的语法很都掌握,但有需求时,很难直接上手编程,要么写了很久,要么写不好!对 ...

  5. Linux编写脚本 Hello word

    今天我们通过Linux 来编写shell script脚本文件.脚本文件主要是帮助我们进行一些指令处理,以实现某些功能,功能跟我们编写的脚本有关,跟驱动有一点类似. 一.编写脚本 二.运行脚本 一.编 ...

  6. linux 编写脚本示范,Linux-scripts-简单脚本和脚本的执行

    9.Scripts 9.1什么是 Shell scripts shell script 是利用 shell 的功能所写的一个『程序 (program)』,这个程序是使用纯文本文件,将一些 shell ...

  7. linux编写脚本读取txt文件,用bash脚本或者批处理 bat脚本 读取一个目录下.txt文件第一行内容存放到新文件a.txt...

    起因:我把cmd 控制台说成了dos! 发展:人类生气了,跟我扯了一堆 dos 还有什么玩意儿,在我第二次还说那个是dos界面以后:( 后续:人类要我写 批处理bat 还有linux bash 脚本去 ...

  8. linux常用脚本的使用方法,Linux常用命令用法100个

    Linux常用命令用法100个 发布时间:2013-05-25 13:53:27   作者:佚名   我要评论 平时用linux时,我有一个习惯就是把遇到的,比较有用,并且容易忘的命令,放到一个文本文 ...

  9. Linux编写脚本监控系统负载,linux系统管理-编写Shell脚本监测服务器状态

    --为了持续观察服务器每天的基本运行状况,提供方便易读的集中的日志记录数据,需要结合Shell脚本和计划任务设置,定期记录不同时间段服务器的CPU负载.内存和交换空间.磁盘使用率等各种信息 需求描述 ...

  10. linux++查找隐藏文件,使用find命令查找Linux中的隐藏文件的方法

    我们可以在Linux 或者 Unix 系统上使用 find 命令去查询所有(全部)隐藏文件 基本语法如下: 复制代码 代码如下: [root@dabu.info ~]#find /要查找的文/件/夹/ ...

最新文章

  1. matlab中ss函数_matlab ss函数 tf函数
  2. boost::describe模块实现pp_call的测试程序
  3. java 数据库工厂_月光软件站 - 编程文档 - Java - 简单的数据库连接工厂实现
  4. sscanf的常见用法
  5. 负债十五万左右,信用卡十万,网贷四五万,怎么上岸?
  6. 怎么更换锁定计算机的图片,Win10系统下怎样对锁定界面的背景图片进行更换
  7. hdu 4738 Caocao's Bridges 求无向图的桥【Tarjan】
  8. php传递字符串给python,用PHP和Python生成短链接服务的字符串ID
  9. spark学习-29-源代码解析从start-all.sh脚本开始
  10. Windows中查找命令的路径 (类似Linux中的which命令)
  11. 【白皮书分享】2021中国数字经济时代人工智能生态白皮书.pdf(附下载链接)...
  12. [翻译] InstagramPhotoPicker
  13. 计算机内存4gb是多大,手机内存多大才不卡?专家:4GB足矣
  14. 如何让网页字体文件大瘦身?前端字体优化知多D
  15. 8 9区别 endnote7_EndNoteX9使用进阶七:全文查找下载和统计分析
  16. 卡巴斯基最新激活码,卡巴斯基免费下载
  17. 苹果与宝马和奔驰关于自动驾驶汽车的谈判告吹,退而求其次选择与大众汽车合作
  18. /etc/login.defs配置文件详解
  19. 戴尔Dell EMC S5048-ON交换机光模块解决方案
  20. 实例7:stc8a8k定时器0,定时50ms,进入中断之后,加数20次到1秒,计算时间。

热门文章

  1. 杰控连接mysql_杰控FameView在数据库连接和查询方面的应用———FameView组态软件在数据库处理方面的...
  2. JPG在线压缩工具分享
  3. Spring-data ElasticSearch的使用
  4. 人脸识别门禁系统java实现_java实现人脸识别源码【含测试效果图】——前台显示层(index.jsp)...
  5. 冒泡排序满分代码(C语言),附源代码,可直接运行
  6. 教你如何使用抖音直播间数据分析工具
  7. TCP滑动窗口协议作用
  8. 如何用C语言编写字母游戏,怎么样用C语言编写一个小游戏?
  9. 51单片机直流电机调速
  10. 2015年二级c语言真题及答案,2015年计算机二级C语言考试试题及答案