美文网首页
day64-shell编程之-变量基础

day64-shell编程之-变量基础

作者: 文娟_狼剩 | 来源:发表于2019-07-20 17:46 被阅读0次

1、定义变量

值可变的量,称为变量
变量名=变量值,常说的变量,一般是变量名
变量名命名规则:字母数字下划线,不能是数字开头

2、环境变量和普通变量

2.1 环境变量(全局变量)

  • 可在创建他们的shell以及派生出来的任意子shell中使用
  • 环境变量包括内置的环境变量和自定义的环境变量,且通常为大写。
  • 环境变量的定义方式:

declare -x 变量名=value
export 变量名=value(常用)

  • 环境变量有四个文件,他们的执行顺序如下图所示:


  • 登陆shell会加载所有的环境变量
  • 非登陆shell可能会加载~/.bashrc或者/etc/bashrc(上图非绿色部分),然而有些定时任务以上两个根本不会加载,所以需要手动指定,建议在定义变量时定义到/etc/bashrc
  • 可以在环境变量文件中定义普通变量

2.2 普通变量

  • 普通变量只有在当前shell下才能使用
  • 定义方式
# 适用于一般场景,不适用于带有空格等字符
变量名=value
 
# 所见即所得的定义方式
变量名=’value’

# 解析双引号之内的变量
变量名=”value”
  • 注意点:(举例说明)
    1>变量如果后面有内容,一定要把变量括{}起来
    2>希望变量内容原样输出则加单引号
    3>希望获取变量中的命令执行结果用``或者$()
练习:通过非登录命令(ssh)执行java -version

3、临时变量和永久变量(了解)

如果按照变量的生存周期来划分的话,Linux变量可以分为两类:

永久变量:需要修改变量配置文件,使得变量永久生效
临时变量,使用export命令或者直接在当前shell中赋值的变量

作业:需要完全掌握

1>用source执行脚本和用bash执行的区别是什么
用source执行的脚本,变量会在父shell下生效,用. 也是
用bash执行的脚本,变量不会在父shell下生效

2>如何自定义环境变量
export aaa="dfs"

3>定义变量内容,不加引号、单引号、双引号、反引号结果有什么不同?该怎么用
不加引号:变量的连续传参
单引号:所见即所得
双引号:可以解析引号下面的内容
反引号:强制命令解析

4>在shell编程中,如果要访问变量值,可以变量前加一个$符号

5>下列对shell变量FRUTT操作,正确的是C
A)为变量赋值:$FRUTT=apple B)显示变量的值:fruit=apple C)显示变量的值:echo $FRUTT

4、shell特殊变量

4.1 参数特殊变量

[root@shelledu /server/scripts]# cat copy.sh 
#!/bin/bash
echo $0
echo $1
echo $#
[root@shelledu /server/scripts]# sh copy.sh a b c d
copy.sh
a
4

4.2 进程特殊变量

[root@shelledu /server/scripts]# vim test2-4-2.sh 
  8 echo "hello wwj"
  9 echo $?
 10 cat /root/2.txt
 11 echo $?
 12 exit 50                                              
                                                  
"test2-4-2.sh" 12L, 293C written
[root@shelledu /server/scripts]# sh test2-4-2.sh 
hello wwj
0
cat: /root/2.txt: No such file or directory
1
[root@shelledu /server/scripts]# echo $?
50

5、shell变量子串

shell变量子串实例:
[root@shelledu /server/scripts]# url=www.oldboy.com
[root@shelledu /server/scripts]# echo $url
www.oldboy.com
[root@shelledu /server/scripts]# echo ${url}.cn
www.oldboy.com.cn
[root@shelledu /server/scripts]# echo ${#url}
14
[root@shelledu /server/scripts]# echo ${url:4}
oldboy.com
[root@shelledu /server/scripts]# echo ${url:4:6}
oldboy
[root@shelledu /server/scripts]# txt=abcABCabcABC
[root@shelledu /server/scripts]# echo $txt
abcABCabcABC
[root@shelledu /server/scripts]# echo ${txt#*c}
ABCabcABC
[root@shelledu /server/scripts]# echo ${txt##*c}
ABC
[root@shelledu /server/scripts]# echo ${txt%c*}
abcABCab
[root@shelledu /server/scripts]# echo ${txt%%c*}
ab
[root@shelledu /server/scripts]# echo ${txt/abc/wwj}
wwjABCabcABC
[root@shelledu /server/scripts]# echo ${txt//abc/wwj}
wwjABCwwjABC

6、练习

6.1 请使用shell脚本打印下面语句中字符数不小于6的单词

I am teacher oldchang and I like eating and sleeping



6.2 写出shell脚本,通过传参的方式,传入以下内容,并打印下面语句中字符数不小于6的单词

7、变量数值计算

7.1 算数运算符

7.2 Shell的常见运算命令

[root@shelledu ~]# echo $((3+6))
9
[root@shelledu ~]# let a=4*5
[root@shelledu ~]# echo $a
20
[root@shelledu ~]# expr 12 + 3
15
[root@shelledu ~]# echo $[9+5]
14
[root@shelledu ~]# echo $[2**10]
1024
[root@shelledu ~]# declare -i b=2+5
[root@shelledu ~]# echo $b
7
[root@shelledu ~]# echo 2*4|bc
8
[root@shelledu ~]# echo "scale=3;10/3"|bc
3.333
[root@shelledu ~]# echo 100 4|awk '{print $1/$2}'
25
[root@shelledu ~]# echo 100 40|awk '{print $1/$2}'
2.5
[root@shelledu ~]# echo $a
20
[root@shelledu ~]# unset a   #取消变量
[root@shelledu ~]# echo $a

[root@shelledu ~]# 

8、变量传参

8.1 脚本变量传参的三种方式:

1>直接赋值

[oldchang@oldboy-node101 ~]$ cat 1.sh 
IP=127.0.0.1
echo $IP
[oldchang@oldboy-node101 ~]$ sh 1.sh 
127.0.0.1

2>传参方式

[oldchang@oldboy-node101 ~]$ cat 1.sh 
IP=$1
echo $IP
[oldchang@oldboy-node101 ~]$ sh 1.sh 127.0.0.1
127.0.0.1

3>read方式

[oldchang@oldboy-node101 ~]$ cat 1.sh 
read -p "请输入一个参数: " IP
echo $IP
[oldchang@oldboy-node101 ~]$ sh 1.sh 
请输入一个参数: 127.0.0.1
127.0.0.1

8.2 练习

1>使用脚本传参的方式实现整数的加、减、乘、除、取余、幂运算
2>使用脚本变量传参的三种方式,写一个脚本,通过传参方式修改主机名和ip地址
[root@shell-project /server/scripts]# cat  test-read.sh
#!/bin/bash
#使用脚本变量传参的三种方式,写一个脚本,通过传参方式修改主机名和ip地址
read -p "请输入要修改的主机名: " hostname
echo $hostname
hostnamectl set-hostname $hostname
read -p "请确认要修改那块网卡: " eths
echo $eths
if [ $eths = 'eth0' ];then
    read -p "请输入eth0的的新IP: " newip0
    sed -i "s#^IPADDR=.*\$#IPADDR=${newip0}#g" /etc/sysconfig/network-scripts/ifcfg-eth0
elif [ $eths = 'eth1' ];then
    read -p "请输入eth1的的新IP: " newip1
    sed -i "s#^IPADDR=.*\$#IPADDR=${newip1}#g" /etc/sysconfig/network-scripts/ifcfg-eth1
fi

相关文章

网友评论

      本文标题:day64-shell编程之-变量基础

      本文链接:https://www.haomeiwen.com/subject/ytfdlctx.html