阅读背景:

How do I wrtite the current IP of tun0 into a txt file with a shell script?

来源:互联网 
#!/bin/sh
/sbin/ifconfig tun0 | grep 'inet addr:' | cut -d: -f2 | awk '{print 
#!/bin/sh
/sbin/ifconfig tun0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}' > ./tun0_ip.txt

This works very good when I put it into the command line. However it does not work with a sh script.

当我把它放入命令行时,这非常好用。但它不适用于sh脚本。

What am I doing wrong? Why do some commands not work with a sh script?

我究竟做错了什么?为什么有些命令不适用于sh脚本?

~$ bash -x ./reset.sh
+ echo /sbin/ifconfig tun0
+ grep 'inet addr:'
+ cut -d: -f2
+ awk '{print $1}'

1 个解决方案

#1


0  

It is strange that you are testing with bash, while giving #!/bin/sh as shebang line.

很奇怪,你正在用bash进行测试,同时将#!/ bin / sh作为shebang line。

However this is probably not the reason this does not work. Definitely, whether you run some commands from the command line or from a script with the exact same environment, they should not give you a different result.

然而,这可能不是这不起作用的原因。当然,无论是从命令行还是从具有完全相同环境的脚本运行某些命令,它们都不应该给出不同的结果。

Maybe the user that calls the script has a different locale, so the output of ifconfig is no more 'inet addr' but something else? Or some environment variable that changes the form of ifconfigs output is not exported?

也许调用脚本的用户具有不同的语言环境,因此ifconfig的输出不再是'inet addr'而是其他东西?或者某些更改ifconfigs输出形式的环境变量未导出?

You can find out only, by going step by step. Looking at the script it should be some problem with ifconfig not giving you, what you expect. So simply take a look at the output of that from the script, unchanged.

你可以一步一步地找到它。看看脚本,ifconfig没有给你,你期望的应该是一些问题。因此,只需看看脚本中输出的内容即可。


}' > ./tun0_ip.txt #!/bin/sh /sbin/ifconfig tun0 | grep 'inet addr



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: