Bash 脚本问题

发布于 2024-09-10 20:38:31 字数 370 浏览 7 评论 0原文

我可以很好地运行这个命令,并得到我想要的输出:

ifconfig eth0 | grep HWaddr | awk '{print $5}'

但是,当我将命令设置为变量并打印该变量时,我收到错误:

CASS_INTERNAL=`ifconfig eth0 | grep HWaddr | awk '{print \$5}'`
$CASS_INTERNAL

not find

my inside xxx ip: command 奇怪的事情 - 我的内部 IP 实际上出现了。我该如何处理而不出现错误?没关系,但我使用的是最新版本的 Ubuntu。

I can run this command fine, with the output I want:

ifconfig eth0 | grep HWaddr | awk '{print $5}'

However, when I set the command to a variable, and print the variable, I get an error:

CASS_INTERNAL=`ifconfig eth0 | grep HWaddr | awk '{print \$5}'`
$CASS_INTERNAL

my internal xxx ip: command not found

The weird thing - my internal IP actually shows up. How do I go about this without getting an error? It shouldn't matter, but I'm using the latest version of Ubuntu.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

葮薆情 2024-09-17 20:38:31

您没有打印变量,而是将其作为命令名称运行。您正在寻找

echo "$CASS_INTERNAL"

(养成总是在变量替换周围加上双引号的习惯。)

更高级的 shell 注意:在这种情况下,这并不重要,但一般来说 echo 可能会遇到一些问题特殊字符(-\\),因此最好使用以下更复杂但完全可靠的命令:

printf "%s\n" "$CASS_INTERNAL"

You're not printing the variable, you're running it as a command name. You're looking for

echo "$CASS_INTERNAL"

(Get into the habit of always putting double quotes around variable substitutions.)

More advanced shell note: in this case it doesn't matter, but in general echo can have trouble with some special characters (- and \\), so it's better to use the following more complicated but fully reliable command:

printf "%s\n" "$CASS_INTERNAL"
以为你会在 2024-09-17 20:38:31

不必使用grep

ifconfig eth0 | awk '/HWaddr/{print $5}'

don't have to use grep

ifconfig eth0 | awk '/HWaddr/{print $5}'
第七度阳光i 2024-09-17 20:38:31
CASS_INTERNAL=`ifconfig eth0 | grep HWaddr | awk '{print \$5}'`
echo $CASS_INTERNAL

您的:

$CASS_INTERNAL

会尝试将其作为命令运行。

CASS_INTERNAL=`ifconfig eth0 | grep HWaddr | awk '{print \$5}'`
echo $CASS_INTERNAL

Your:

$CASS_INTERNAL

Would try to run it as a command.

等待圉鍢 2024-09-17 20:38:31

你也许想要吗

echo $CASS_INTERNAL

do you maybe want

echo $CASS_INTERNAL
澉约 2024-09-17 20:38:31

好吧,您首先 grep 查找 HWaddr,因此这一行的第五个字段是相关网络适配器的 MAC 地址,而不是您的本地 IP 地址。

其他人建议解决方案是简单地回显结果,这意味着如果本示例中的 eth0 在执行该行的时间点不可用,则它将无法工作。

据我了解,您希望将所需的命令行放入变量中,然后稍后对其进行评估。这种模式通常称为延迟评估,并且可以在 bash 中实现 通过使用 eval 内置函数:

#put the desired command in variable
CASS_INTERNAL='ifconfig eth0 | grep HWaddr | awk "{print \$5}"'

# ....

#at later on - evaluate its contents!
eval $CASS_INTERNAL
11:22:33:aa:bb:cc

Well, you grep for HWaddr first, so the fifth field on this this line is the MAC address of the network adapter in question - not your local IP address.

Others have suggested the solution is to simply echo the result, meaning if eth0 in this example is not available at that point in time which the line gets executed, it will not work.

From what I understand you wish instead to put the desired command line in a variable, then evaluate it later on. This pattern is commonly called lazy evaluation, and is made possible in bash by using the eval builtin:

#put the desired command in variable
CASS_INTERNAL='ifconfig eth0 | grep HWaddr | awk "{print \$5}"'

# ....

#at later on - evaluate its contents!
eval $CASS_INTERNAL
11:22:33:aa:bb:cc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文