变量赋值方式

变量赋值方式

1.显示赋值(变量名=变量值)
2.read从键盘读入变量值
3.注意事项:定义或引用变量时注意事项: " " 弱引用 ’ ’ 强引用
所见及所得,输入什么就输出什么,使用单引号
变量,字符串解析使用双引号
4.` `命令替换等价于 $()反引号中的shell命令会先被执行
命令解析执行需要用$()或` `


显示赋值

[root@xiudaochengxian test]# vim fz.sh
  1 ip1=192.168.1.105
  2 school="Wuhan JiaoTong"
  3 today1=`date +%F`
  4 today2=$(date +%F)
  5 
  6 echo $ip1
  7 echo $school
  8 echo $today1
  9 echo $today2
[root@xiudaochengxian test]# sh fz.sh 
192.168.1.105
Wuhan JiaoTong
2021-08-07
2021-08-07

read从键盘读入变量值

[root@xiudaochengxian test]# vim ping.sh
  1 #!/bin/bash
  2 read -p "Input IP:" ip
  3 ping -c2 $ip &> /dev/null
  4 if [ $? -eq 0 ]
  5 then
  6 echo "host $ip is ok"
  7 else
  8 echo "host $ip is fail"
  9 fi
[root@xiudaochengxian test]# sh ping.sh 
Input IP:192.168.1.105
host 192.168.1.105 is ok

定义或引用变量时注意事项: " " 弱引用 ’ ’ 强引用

[root@xiudaochengxian test]# school=100phone
[root@xiudaochengxian test]# echo "${school} is good"
100phone is good
[root@xiudaochengxian test]# echo '${school} is good'
${school} is good

` `命令替换等价于 $()反引号中的shell命令会先被执行

[root@xiudaochengxian test]# touch `date +%F`_file1.txt
[root@xiudaochengxian test]# ls
2021-08-07_file1.txt  fz.sh  ping.sh
[root@xiudaochengxian test]# touch $(date +%F)_file2.txt
[root@xiudaochengxian test]# ls
2021-08-07_file1.txt  2021-08-07_file2.txt  fz.sh  ping.sh

[root@xiudaochengxian test]# disk_free3="df -h | grep '/$' |awk '{print $4}'"
[root@xiudaochengxian test]# echo $disk_free3 
df -h | grep '/$' |awk '{print }'
[root@xiudaochengxian test]# disk_free4=$(df -h | grep '/$' |awk '{print $4}')
[root@xiudaochengxian test]# echo $disk_free4
14G
[root@xiudaochengxian test]# disk_free5=`df -h | grep '/$' |awk '{print $4}'`
[root@xiudaochengxian test]# echo $disk_free5
14G
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值