bash脚本问题 [英] Bash script issue

查看:130
本文介绍了bash脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以运行此命令细末,用输出我想:

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

我的内部IP XXX:找不到命令

怪异的事情 - 我的内部IP居然出现了。我怎么去这个没有得到一个错误?它不应该的问题,但我使用了最新版本的Ubuntu。

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.

推荐答案

那么,使用grep命令查找的HWaddr第一,所以在这此行的第五个领域是有问题的网卡的MAC地址 - 而不是你的本地IP地址。

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.

其他建议的解决方案是简单呼应的结果,这意味着如果为eth0在本例中是不是可以在该行被执行的时间点,它不会工作。

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.

据我了解,你想,而不是把所需的命令行中的变量,然后评估的后面。这种模式通常被称为懒惰的评价,然后在bash是由可能通过使用的评估内置的:

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

这篇关于bash脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆