Linux in respect of such a study - written script SHELL

Acquaintance SHELL

Shell script command works :

Interactive: each user enter a command will be executed immediately

Batch Processing: pre-programmed by the user a complete SHELL good script, shell script in a lot of one-time script command execution.

Note :

1. See SHELL variable can be found in the current system has been used as the Bash default command interpreter terminal;

2. SHELL can interpreter terminal as a "translator" between people and computers, as communication media with user inside the Linux system, in addition to support a variety of variables and parameters, but also provides services such as cyclic , branched advanced programming language unique to control structural properties .

SHELL early script of my first experience of a SHELL script

  1. If you want to see where the current working directory and list all files in the current directory and attribute information

 

The picture shows the result of the command execution bash example.sh

 

Figure execution result of the command ./example.sh

Shell receives a user's experience of the advanced script parameters

The following variables corresponding to each of the meaning of

$ 0: Current script name

$ #: There are several parameters

$ *: Parameter values ​​corresponding to all positions is

$? Corresponding command is executed on a display of the return value

$ 1, $ 2, $ 3, $ 4, ...: the parameter values ​​correspond to the N-th position

Example:

 

 

 

Figure execute bash example.sh one two three four five six operating results

 Shell determine the user's experience of the advanced script parameters (focus)

introduction

系统在执行mkdir命令时会判断用户输入的信息,即判断用户指定的文件夹名称是否已经存在,如果存在则提示报错;反之则自动创建.shell脚本中的条件测试语法可以判断表达式是否成立,若条件成立则返回数字0,否则便返回其他随机数值.

测试语句格式 : [ 条件表达式 ]

注 : 测试语句格式中的中括号两边应有一个空格

条件测试语句的种类 : 文件测试语句/逻辑测试语句/整数值比较语句/字符串比较语句

文件测试语句 :

即使用指定条件来判断文件是否存在或者权限是否满足等情况的运算符.

表 文件测试所用的参数

运算符

作用

-d

测试文件是否为目录类型

-e

测试文件是否存在

-f

测试文件是否为一般文件

-r

测试当前用户是否有权限读取

-w

测试当前用户是否有权限写入

-x

测试当前用户是否有权限执行

 

例子1 :

判断/etc/fstab是否为目录类型的文件,然后通过shell解释器的内设的$?变量显示上一条命令执行后的返回值.若返回值为0,则目录存在;如果返回值为非零的值,则意味着目录不存在.

 

例子2 :

判断/etc/fstab是否为一般文件,如果返回值为0,则代表文件存在,而且为一般文件.

 

逻辑测试语句

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

逻辑测试语句的符号 :

与(&&) : 表示当前命令执行成功后才会执行它后面的命令

或(||) : 表示当前命令执行失败后才会执行他后面的命令

非(!) : 表示把条件测试中的判断结果取相反值.

例子1 :

判断/dev/cdrom是否存在,如存在则输出Exist字样

 

例子2

判断当前是否为非管理员身份

 

例子3

判断当前是否是管理员

 

例子4

当前我们正在登录的即为管理员用户—root。下面这个示例的执行顺序是,先判断当前 登录用户的 USER 变量名称是否等于 root,然后用逻辑运算符“非”进行取反操作,效果就 变成了判断当前登录的用户是否为非管理员用户了。后若条件成立则会根据逻辑“与”运 算符输出 user 字样;或条件不满足则会通过逻辑“或”运算符输出 root 字样,而如果前面的 &&不成立才会执行后面的||符号。

 

整数值比较语句

整数比较仅是对数字的操作.

注: 1. 不能将数字和字符串/文件等内容一起操作

2. 不能想当然地使用日常生活中的等号/大于号/小于号等来判断

3. 因为等号与赋值命令符冲突,大于号和小于号分别是输出重定向和输

入重定向冲突,因此一定要使用规范的整数比较运算符来进行操作

可用的整数比较运算符

参数

作用

-eq

是否等于

-ne

是否不等于

-gt

是否大于

-lt

是否小于

-le

是否小于等于

-ge

是否大于等于

 

例子1 :

判断10是否大于10以及10是否等于10

 

例子2 :

在 2.4 节曾经讲过 free 命令,它可以用来获取当前系统正在使用及可用的内存量信息。 接下来先使用 free -m 命令查看内存使用量情况(单位为 MB),然后通过 grep Mem:命令过滤 出剩余内存量的行,再用 awk '{print $4}'命令只保留第四列,后用 FreeMem=`语句`的方式把语句内执行的结果赋值给变量。 这个演示确实有些难度,但看懂后会觉得很有意思,没准在运维工作中也会用得上。我们使用整数运算符来判断内存可用量的值是否小于 1024,若小 于则会提示“Insufficient Memory”(内存不足)的字样:

 

字符串比较语句

字符串比较语句主要用于判断字符串是否为空值,或两个字符串是否相同.它经常用于判断某个变量是否未被定义(即内容为空值).

常见的字符串比较运算符

运算符

作用

=

比较字符串内容是否相同

!=

比较字符串内容是否不同

-z

判断字符串内容是否为空

 

例子1 :

判断String变量是否为空,进而判断是否定义了这个变量

 

例子2 :

当用于保存当前语系的环境变量值 LANG 不是英语 (en.US)时,则会满足逻辑测试条件并输出“Not en.US”(非英语)的字样:

 

Guess you like

Origin www.cnblogs.com/studyandstudy/p/12182584.html