小编San*_*nex的帖子

Bash脚本在"if"之后丢失变量(简单的#bash代码)

为什么代码末尾的变量$ c不回显"微笑"而只回显空值?

在"if运算符"结束后我不会得到变量$ c但是当运算符完成作业时我的值$ c不返回"smile"但只返回""(空值)

#!/bin/bash
###
## sh example.sh

a="aaa"
b="bbb"

if [ "$a" != "$b" ]; then ( 
c="smile"
echo "echo inside if:"
echo $c # in this echo "smile" 
) else ( 
c="yes" 
) fi

echo "echo after fi:"
echo $c  # echo ""  # why this not echo "smile"
Run Code Online (Sandbox Code Playgroud)

结果:

[root@my-fttb ~]# sh /folder/example.sh
echo inside if:
smile
echo after fi:

[root@my-fttb ~]#
Run Code Online (Sandbox Code Playgroud)

linux bash shell sh

2
推荐指数
1
解决办法
145
查看次数

运行 cron 后 Bash 空变量,但手动运行

我有一个简单的 bash 脚本,该脚本在终端上工作手册,但是在 cron 之后给出了一个空变量。

#!/bin/bash 
gwip=`/usr/bin/nmcli dev list iface eth0 | grep IP4-SETTINGS.GATEWAY: | awk '{ print $2}'`
printf '%s\n' "$(date) =- $gwip -= " >> /var/log/looog.log
Run Code Online (Sandbox Code Playgroud)

跑: /bin/bash /test.bash

文件 /var/log/looog.log 中的输出:

1 monday 2016 14:17:36 +0300 =- 23.18.117.254 -= 
Run Code Online (Sandbox Code Playgroud)

当我运行 cron 时,变量为空。

*/1 * * * * root /bin/bash /test.bash
Run Code Online (Sandbox Code Playgroud)

文件 /var/log/looog.log 中的输出:

1 monday 2016 14:19:13 +0300 =-  -=
Run Code Online (Sandbox Code Playgroud)

为什么变量$gwip是空的?如何解决?

linux variables bash cron

2
推荐指数
1
解决办法
1344
查看次数

标签 统计

bash ×2

linux ×2

cron ×1

sh ×1

shell ×1

variables ×1