首页 > 解决方案 > 内联 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 作为文件名参数)

标签: bashunix

解决方案


<(tail -n +2 test.sh)构造是 bash 功能,因此您需要在bashshell 中运行脚本,

替换你的顶线

#!/bin/sh

#!/bin/bash

(或者 bash 可执行文件的正确路径,如果它不在/bin/bash您的系统上)

请注意,即使是例如 bash 的符号链接,当您将其运行为 时,它也会以posix 兼容模式/bin/sh启动 bash ,并且许多 bash 特定功能将不可用)/bin/sh


推荐阅读