小编d0x*_*xin的帖子

在bash -c中设置变量

我尝试设置从接口IP地址获取的变量,ifconfig然后再读取它.但是当我执行一个echo命令时,变量仍然是空的.请看我的代码:

/usr/bin/bash -c "HOST_IPS=$(/usr/bin/ifconfig | /usr/bin/awk 'BEGIN {cnt=0} {if($0 ~ /inet / && cnt==1) {print $2} cnt++}'); echo $HOST_IPS"
Run Code Online (Sandbox Code Playgroud)

但/ bin/echo使用相同的命令工作正常:

/usr/bin/bash -c "echo $(/usr/bin/ifconfig | /usr/bin/awk 'BEGIN {cnt=0} {if($0 ~ /inet / && cnt==1) {print $2} cnt++}')"
Run Code Online (Sandbox Code Playgroud)

linux bash awk ifconfig

8
推荐指数
1
解决办法
2820
查看次数

标签 统计

awk ×1

bash ×1

ifconfig ×1

linux ×1