从 shell 脚本运行 awk 文件而不指定 awk 的确切位置
我正在尝试调试调用 awk 文件的 shell 脚本。 这是一场噩梦,因为我以前从未使用过,也不太熟悉 Linux,但无论如何
一个开发人员制作了一个 awk 文件并尝试在 shell 脚本中运行它。
为了尝试从单独的位置运行它,而不需要指定确切的位置,他们将 awk 脚本放在 PATH 变量中的文件夹中。那么 awk 文件应该随处可用,对吧?
当你像这样运行时...
awk -f 脚本.awk arg1
...awk 可以找到该脚本吗? 当 shell 脚本尝试运行该 awk 命令时,它会输出错误:
awk:致命:无法打开源文件“script.awk”进行读取(没有此类文件或目录)
I'm trying to debug a shell script that calls an awk file. Which is a nightmare, cause I've never used either before, nor am I very fluent with linux, but anyway
A dev made an awk file and is trying to run it in a shell script.
To try and run it from a separate location, without needing to specify the exact location, they put the awk script in a folder that's in the PATH variable. So that awk file should be available everywhere, right?
When you run it like this...
awk -f script.awk arg1
...can awk find that script? It spits out an error, when the shell script tries to run that awk command:
awk: fatal: can't open source file `script.awk' for reading (No such file or directory)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如您所知,
awk
无法找到脚本本身。如果脚本被标记为可执行文件,并且您有一个
which
命令,那么您应该能够执行以下操作:或者,也许更好,将脚本制作为可执行文件:
shebang 需要 '< code>-f' 选项在我测试过的 MacOS X 10.7.1 上工作:
这为您提供了一个独立的单文件解决方案。
As you know,
awk
can't find the script itself.If the script is marked as executable, and if you have a
which
command, then you should be able to do:Alternatively, and probably better, make the script into an executable:
The shebang needs the '
-f
' option to work on MacOS X 10.7.1 where I tested it:That gives you a self-contained single-file solution.
不,那是行不通的。 awk 需要脚本的路径才能运行,它不会使用 PATH 变量来查找它。 PATH 仅用于查找可执行文件。
No, that's not going to work. awk needs the path to the script to run, it won't use the PATH variable to find it. PATH is only used to find executables.