bash - 内联 unix 命令作为文件名
问题描述
我有以下 bash 脚本 test.sh (具有执行权限):
#!/bin/sh
CAT_BIN="cat"
"$CAT_BIN" <(tail -n +2 test.sh)
当我运行它时,它给了我这个错误:
$ ./test.sh
./test.sh: line 4: syntax error near unexpected token `('
./test.sh: line 4: `"$CAT_BIN" <(tail -n +2 test.sh)'
但是,当我获取以下命令时,它执行得很好。
$ CAT_BIN="cat"
$ "$CAT_BIN" <(tail -n +2 test.sh)
这如何在脚本中工作?(使用<(tail -n +2 test.sh)
inline 作为文件名参数)
解决方案
该<(tail -n +2 test.sh)
构造是 bash 功能,因此您需要在bash
shell 中运行脚本,
替换你的顶线
#!/bin/sh
和
#!/bin/bash
(或者 bash 可执行文件的正确路径,如果它不在/bin/bash
您的系统上)
请注意,即使是例如 bash 的符号链接,当您将其运行为 时,它也会以posix 兼容模式/bin/sh
启动 bash ,并且许多 bash 特定功能将不可用)/bin/sh
推荐阅读
- c# - C# ReadAsync 程序挂起 - 死锁
- react-native - React Native Context调度删除当前屏幕(项目)导致渲染错误
- javascript - 猫鼬子文档数组不保存整个对象?
- maven - Gradle - 生成源代码并编译
- outlook - Outlook Web 加载项部署
- python - ValueError:检查输入时出错:预期 lstm_16_input 有 3 个维度,但得到的数组形状为 (836, 400, 3, 1)
- python - 为什么我的线程锁不能正常工作?
- javascript - 将信息从 Javascript 函数传递到表单
- python - Dash 允许下载 Excel Linux ubuntu
- azure-data-factory - 如何在arm模板中创建数据工厂的集成运行时