linux---shell变量及函数

1.变量定义

变量即在程序中运行过程中它的值是允许改变的量,变量是用一串固定的字符来标志不固定的值的一种方法,变量是一种使用方便的占位符,用于引用计算机内存地址,该地只可以村粗scripts运行时可更改的程序信息。在shell中变量是不可能永久保存在体统中的,必须在文件中声明。

2.变量分类

在shell中变量分为环境及变量,用户级变量,系统级变量,环境及变量只在当前shell中生效,shell关闭变量丢失,用户级变量写在用户的骨文件中,只针对当前用户有效,系统级变量被写在配置文件/etc/profile中 变量即在程序运行时保存在内存中。硬盘永久,内存则是临时的。

3.变量名称的规范

变量名称中通常包含大小写字母,数字,下划线

变量格式如下:

WESTOS---LINUX

westos_linux

westos_linux

4.变量的定义方法

环境级:

export a=1

用户级:

vim ~/.bash_profile

系统级:

vim /etc/profile

export a=1


4.1环境级:

[[email protected] mnt]# echo $PATH   (查看环境变量)

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin

linux---shell变量及函数

[[email protected] mnt]# a=1           (定义变量为1)

[[email protected] mnt]# echo $a         (输出变量)

1

[[email protected] mnt]# ps          (进入另一个进程)

  PID TTY          TIME CMD

 1622 pts/0    00:00:00 bash

31126 pts/0    00:00:00 ps

[[email protected] mnt]# sh

sh-4.2# ps

  PID TTY          TIME CMD

 1622 pts/0    00:00:00 bash

31131 pts/0    00:00:00 sh

31133 pts/0    00:00:00 ps

sh-4.2# echo $a              (输出a,此时没有值,是因为这种定义只能用于当前的进程中,后一个进程的分配的内存段不同,无法访问a的值)

linux---shell变量及函数

sh-4.2# export a=1    (声明a=1,将其变为公共)

sh-4.2# echo $a

1

linux---shell变量及函数


4.2用户级:

[[email protected] ~]# vim .bash_profile   (打开配置文件添加export a=1)

linux---shell变量及函数

[[email protected] ~]# source .bash_profile  (刷新文件)

[[email protected] ~]# echo $a  (输出a的值)

1

[[email protected] ~]# su - student  (切换到student用户)

[[email protected] ~]$ echo $a   (输出a的值,但是没有,是因为不同的shell环境会加载不一样的配置文件覆盖原来的文件,导致定义失效)

linux---shell变量及函数

4.3系统级:

[[email protected] ~]# vim /etc/profile   (编辑系统文件)

linux---shell变量及函数

[[email protected] ~]# source /etc/profile  (刷新)

[[email protected] ~]# echo $a    (输出a的值)

2

[[email protected] ~]# su - student

Last login: Fri Jun 29 18:35:54 EDT 2018 on pts/0

[[email protected] ~]$ echo $a  (切换到student用户,输出a的值)

2

linux---shell变量及函数

5.字符的转义变量的声明

\                 转译单个字符

""                弱引用,批量转译“”中出现的字符

''                  强引用,批量转译‘’中出现的字符

''  与""           两者的区别在于,“”中不能转译“\”,“`”,“$”

${}                变量声明

例如:

        A=1

        echo $Ab


6.变量值传递

$1                  ##脚本后的第一串字符串

$2                 ##脚本后的第二串字符串

$3                  ##脚本后的第三串字符串

$#                  ##脚本后所跟字符串的个数

$*                  ##脚本后所根的所有字符串,模式为“1 2 3”

[email protected]                  ##脚本后所跟的所有字符串,模式为“1” “2” “3”

 

[[email protected] mnt]# vim user_create.sh

linux---shell变量及函数

[[email protected] mnt]# ls

user_create.sh

[[email protected] mnt]# vim xixi.sh

[[email protected] mnt]# sh xixi.sh  ##所跟字符为0时

$0 is xixi.sh

$1 is

$2 is

$3 is

$# is 0

$* is

[email protected] is

linux---shell变量及函数

[[email protected] mnt]# sh xixi.sh westos   ##所跟字符为1

$0 is xixi.sh

$1 is westos

$2 is

$3 is

$# is 1

$* is westos

[email protected] is westos

linux---shell变量及函数

[[email protected] mnt]# sh xixi.sh westos redhat  ##所跟字符为2时

$0 is xixi.sh

$1 is westos

$2 is redhat

$3 is

$# is 2

$* is westos redhat

[email protected] is westos redhat

linux---shell变量及函数

[[email protected] mnt]# sh xixi.sh westos redhat linux    ##所跟字符为3时

$0 is xixi.sh

$1 is westos

$2 is redhat

$3 is linux

$# is 3

$* is westos redhat linux

[email protected] is westos redhat linux

linux---shell变量及函数


这样并不能体现出 "[email protected]"   "$*"的区别,用实验来证明:

证明“$*”模式为一串字符,“[email protected]”模式为所跟字符个数字符串

 

[[email protected] ~]# cd /mnt

[[email protected] mnt]# vim test1.sh

 linux---shell变量及函数

[[email protected] mnt]# sh -x test1.sh  hehe haha xixi   (只执行了一次)

+ for name in '"$*"'

+ echo hehe haha xixi

hehe haha xixi

linux---shell变量及函数

[[email protected] mnt]# vim test1.sh

linux---shell变量及函数

[[email protected] mnt]# sh -x test1.sh  hehe haha xixi  (执行了三次)

+ for name in '"[email protected]"'

+ echo hehe

hehe

+ for name in '"[email protected]"'

+ echo haha

haha

+ for name in '"[email protected]"'

+ echo xixi

xixi

linux---shell变量及函数

7.read交互式变量传递

read -s westos    ##隐藏输入字符

read -p westos   ##显示提示,-p打印

利用一个脚本来说明:

#!/bin/bash

read -p "please input a number: " IP           ##在运行脚本时候,需要输入一个数字 时IP

ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down    ##执行ping命令输出一次,如果ping成功显示IP is up ,否则输出 IP is down

[[email protected] mnt]# vim test.sh

linux---shell变量及函数

[[email protected] mnt]# sh test.sh

please input a number: 172.25.30.20

172.25.30.20 is down

linux---shell变量及函数

[[email protected] mnt]# sh test.sh

please input a number: 172.25.30.250

172.25.30.250 is down

[[email protected] mnt]# vim test.sh

[[email protected] mnt]# sh test.sh

please input a number: 172.25.30.250

172.25.30.250 is up

linux---shell变量及函数

8.系统命令别名的设定--alias

8.1临时设定命令别名  alias xie=‘vim’

linux---shell变量及函数

linux---shell变量及函数

8.2永久设定用户级别的命令别名   vim .bashrc

linux---shell变量及函数

source .bashrc  (刷新)

8.3永久设定系统级别命令别名    vim /etc/bashrc

linux---shell变量及函数

source  /etc/bashrc (刷新)


函数

脚本中的函数是把一个复杂的语句块定义成一个字符串的方法,可以确保命令的循环执行,简化骄傲本长度

实验一:编写脚本利用函数虚幻,不断使用ping命令:

#!/bin/bash

PING()       ##定义函数

{

        read -p "please input a number: " IP

        ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down

        PING   ##循环

}

PING    ##循环

linux---shell变量及函数

[[email protected] mnt]# vim test.sh    

[[email protected] mnt]# sh test.sh

please input a number: 172.25.30.1

172.25.30.1 is down

please input a number: 172.25.30.2

172.25.30.2 is down

please input a number: 172.25.30.3

172.25.30.3 is down

please input a number:     ##会一直循环

 linux---shell变量及函数

实验二:

编写脚本利用函数循环,不断使用ping命令,当输入exit后退出

#!/bin/bash

PING()

{

        read -p "please input a number: " IP

        [ "$IP" -eq  "exit" ]&&{

                echo bye

                exit 0

        }

        ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down

        PING

}

PING

~   

 linux---shell变量及函数

[[email protected] mnt]# sh test.sh

please input a number: 172.25.30.1

172.25.30.1 is down

please input a number: 172.25.30.250

172.25.30.250 is up

please input a number: exit

bye

linux---shell变量及函数