shell脚本中的变量$,运算符,自定义函数

1.脚本格式

我们一般将shell脚本写在xxx.sh文件中,执行的时候bash/sh  xxx.sh  注意文件路径

 xxx.sh文件中的第一行为   #!/usr/bin/bash 注代表我们使用的是bin文件夹下的bash解释器(此条为注释语句,不写也可以)

2.echo用法   相当与print

示例1:定义变量a,输出变量a的值(都是在终端中输入)
a=5
echo $a


示例2:创建一个Shell脚本 helloworld.sh,输出 helloworld

[atguigu@hadoop101 shells]$ touch helloworld.sh   #新建 helloworld.sh 文件
[atguigu@hadoop101 shells]$ vim helloworld.sh

在 helloworld.sh 中输入如下内容
#!/bin/bash
echo "helloworld"

[atguigu@hadoop101 shells]$ bash helloworld.sh    #执行 helloworld.sh 文件,输出helloworld

3.变量的定义 

(1)定义变量:变量名=变量值,注意,=号前后不能有空格,如有空格,会当成指令
(2)撤销变量:unset 变量名
(3)声明局部变量:readonly 变量名=变量值,注意:局部变量不能 unset    
  (4)  export 变量名:可把变量提升为全局环境变量

变量定义规则

(1)变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建
         议大写。
(2)等号两侧不能有空格
(3)在 bash 中,变量默认类型都是字符串类型,无法直接进行数值运算。
(4)变量的值如果有空格,需要使用双引号或单引号括起来。 

4.特殊变量 $,$n,$#,$*,$@

$:      $将指令的结果作为字符输出
$n : n 为数字, $0 代表该脚本名称, $1-$9 代表第一到第九个参数,十以 上的参数,十以上的参
         数需要用大括号包含,如${10}
$#:   功能描述:获取所有 输入参数个数 ,常用于循环 , 判断参数的个数是否正确以及加强脚本的健
        壮性
$* :这个变量代表命令行中所有的参数, $* 把所有的参数看成一个整体
$@ :这个变量也代表命令行中所有的参数,不过 $@ 把每个参数区分对待
$ ? :最后一次执行的命令的返回状态。如果这个变量的值为 0 ,证明上一 个命令正确执行;如果这个变量的值为非 0 (具体是哪个数,由命令自己来决定),则证明 上一个命令执行不正确了。
示例1
[atguigu@hadoop101 shells]# S=$(((2+3)*4))  #此处的$是运算符特别的表达
[atguigu@hadoop101 shells]# echo $S      #s为数值,不能直接输出,使用$将数值变换为字符型输出

示例2
[atguigu@hadoop101 shells]$ touch parameter.sh
[atguigu@hadoop101 shells]$ vim parameter.sh

#!/bin/bash                                #parameter.sh中的内容
echo '==========$n=========='
echo $0
echo $1
echo $2
echo '==========$#=========='
echo $#
echo '==========$*=========='
echo $*
echo '==========$@=========='
echo $@

[atguigu@hadoop101 shells]$ bash parameter.sh cls xz   #其中cls,xz分别为变量1,变量2

==========$n==========             #下面为输出内容
./parameter.sh
cls
xz
==========$#==========
2
==========$*==========
cls xz
==========$@==========
cls xz



示例3
[atguigu@hadoop101 shells]$ bash helloworld.sh
hello world
[atguigu@hadoop101 shells]$ echo $?       #判断helloworld.sh有没有正常执行
0

5.运算符

 5.1运算式

语法:“$(( 运算式 ))” “$[ 运算式 ]”
示例:计算(2+3)* 4 的值
[atguigu@hadoop101 shells]# S=$[(2+3)*4]
[atguigu@hadoop101 shells]# S=$(((2+3)*4))

[atguigu@hadoop101 shells]# echo $S

 5.2条件判断

语法: test condition  或 [ condition ]注意 condition 前后要有空格

注意:条件非空即为 true[ atguigu ]返回 true[ ] 返回 false

下图为常用的判断条件:

shell脚本中的变量$,运算符,自定义函数_第1张图片

23 是否大于等于 22
[atguigu@hadoop101 shells]$ [ 23 -ge 22 ]
[atguigu@hadoop101 shells]$ echo $?
0

helloworld.sh 是否具有写权限
[atguigu@hadoop101 shells]$ [ -w helloworld.sh ]
[atguigu@hadoop101 shells]$ echo $?
0

多条件判断(&& 表示前一条命令执行成功时,才执行后一条命令,|| 表示上一
条命令执行失败后,才执行下一条命令)
&&表示与,||表示或,类似于C++
[atguigu@hadoop101 ~]$ [ atguigu ] && echo OK || echo notOK
OK
[atguigu@hadoop101 shells]$ [ ] && echo OK || echo notOK
notOK

6.自定义函数

语法:

[ function ] funname[()]
{
    Action;
    [return int;]
}
1 )必须在调用函数地方之前,先声明函数, shell 脚本是逐行运行。不会像其它语言一
样先编译。
2 )函数返回值,只能通过 $? 系统变量获得,可以显示的加: return 返回,如果不加,将
以最后一条命令运行结果,作为返回值。 return 返回的 数值区间只能为  0-255
计算两数之和

[atguigu@hadoop101 shells]$ touch fun.sh
[atguigu@hadoop101 shells]$ vim fun.sh

#!/bin/bash
function sum()
{
s=0
s=$[$1+$2]
echo "$s"
}
read -p "Please input the number1: " n1;
read -p "Please input the number2: " n2;
sum $n1 $n2;


[atguigu@hadoop101 shells]$ chmod 777 fun.sh
[atguigu@hadoop101 shells]$ ./fun.sh
Please input the number1: 2
Please input the number2: 5
7

 

你可能感兴趣的:(Linux,linux,运维,服务器)