1. 变量:
[root@yeyeck ~]# myname=yeyeck [root@yeyeck ~]# echo $myname yeyeck [root@yeyeck ~]# echo ${myname} yeyeck [root@yeyeck ~]# line='my name is ${myname}' <- 单引号不保留特定功能 [root@yeyeck ~]# echo $line my name is ${myname} [root@yeyeck ~]# line2="my name is ${myname}" <- 双引号保留特定功能 [root@yeyeck ~]# echo $line2 my name is yeyeck [root@yeyeck ~]#
2. 环境变量与子进程
设置环境变量: export 变量名
进入子进程: bash
离开子进程:exit
[root@yeyeck ~]# myname=yeyeck <- 定义一个普通变量 [root@yeyeck ~]# bash <- 进入子进程 [root@yeyeck ~]# echo $myname <- 在子进程中视图输出该变量, 无效 [root@yeyeck ~]# exit <- 离开子进程 exit [root@yeyeck ~]# echo $myname <- 回到父进程,输出该变量,有效 yeyeck [root@yeyeck ~]# export myname <- 将该变量设置为环境变量 [root@yeyeck ~]# bash <- 进入子进程 [root@yeyeck ~]# echo $myname <- 在子进程中视图输出该变量, 生效 yeyeck [root@yeyeck ~]# exit <- 离开子进程 exit [root@yeyeck ~]# unset myname <- 清楚该变量 [root@yeyeck ~]# bash [root@yeyeck ~]# echo $myname <- 子进程中无效 [root@yeyeck ~]# exit exit [root@yeyeck ~]# $myname <- 父进程中也无效
3. 将命令运行结果 保存到变量中
两种方法,用反单引号 `命令` 或者 $(命令)
[root@yeyeck ~]# ifconfig docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:11:c4:23:5a txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.122.194 netmask 255.255.240.0 broadcast 172.18.127.255 ether 00:16:3e:02:23:2c txqueuelen 1000 (Ethernet) RX packets 6370781 bytes 1439478547 (1.3 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8320275 bytes 1025890184 (978.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 36 bytes 4896 (4.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 36 bytes 4896 (4.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@yeyeck ~]# [root@yeyeck ~]# [root@yeyeck ~]# [root@yeyeck ~]# [root@yeyeck ~]# [root@yeyeck ~]# ipInfo=$(ifconfig) [root@yeyeck ~]# echo $ipInfo docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:11:c4:23:5a txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.122.194 netmask 255.255.240.0 broadcast 172.18.127.255 ether 00:16:3e:02:23:2c txqueuelen 1000 (Ethernet) RX packets 6370825 bytes 1439482437 (1.3 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8320306 bytes 1025895604 (978.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 36 bytes 4896 (4.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 36 bytes 4896 (4.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@yeyeck ~]# [root@yeyeck ~]# [root@yeyeck ~]# ipInfo2=`ifconfig` [root@yeyeck ~]# echo $ipInfo2 docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:11:c4:23:5a txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.122.194 netmask 255.255.240.0 broadcast 172.18.127.255 ether 00:16:3e:02:23:2c txqueuelen 1000 (Ethernet) RX packets 6370907 bytes 1439489387 (1.3 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8320354 bytes 1025902656 (978.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 36 bytes 4896 (4.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 36 bytes 4896 (4.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4. 读取用户输入
read [-pt] variable
-p 后面可以接提示语句提示用户输入什么
-t 后面可以接等待的时间
[root@yeyeck ~]# read words I am yeyeck [root@yeyeck ~]# echo $words I am yeyeck [root@yeyeck ~]# read -p'who are you?' who are you?yeyeck [root@yeyeck ~]# echo $words I am yeyeck [root@yeyeck ~]# read -p'Please input your name: ' yourName Please input your name: yeyeck [root@yeyeck ~]# echo $yourName yeyeck [root@yeyeck ~]# read -p'Please input your age: ' -t 5 yourAge Please input your age: 12 [root@yeyeck ~]# read -p'Please input your address: ' -t 5 yourAddr Please input your address: [root@yeyeck ~]# <- 5s 内没有输入,等待结束 [root@yeyeck ~]# echo $yourAddr [root@yeyeck ~]#