来自Perl脚本的Bash配置文件抛出错误

时间:2019-03-28 02:13:57

标签: bash perl

在源.bash_profile后获得一行调用bash包装函数的perl

system ('source ~/.bash_profile; osascript -e \'quit app "Chromium"\'');

尽管包装函数被调用并执行得很好,但我从不相关的bash函数中抛出了一个错误:

/Users/me/.bashrc: line 9: syntax error near unexpected token `<'
/Users/me/.bashrc: line 9: `  done < <(find -L "$1" -type f -not -name *.swp -print0 | LC_COLLATE=C sort -dz)'

这是.bashrc文件中的问题功能:

source_dir() {
  while IFS= read -r -d $'\0' file; do
    source_file "$file"
  done < <(find -L "$1" -type f -not -name *.swp -print0 | LC_COLLATE=C sort -dz)
}

此bash函数直接采购时不会引发错误,仅当通过Perl脚本加载时。我很好奇为什么。

我正在运行bash版本5.0.2。

1 个答案:

答案 0 :(得分:6)

perl的system使用/bin/sh作为外壳(https://perldoc.perl.org/functions/system.html)。它不会理解bash特定的语法,例如进程替换。

您将要显式调用bash:

system 'bash', '-c', q{source ~/.bash_profile; osascript -e 'quit app "Chromium"'};

使用q{}单引号机制来避免反斜杠。


bash注释:如果您将其作为交互式shell调用,它将自动在bashrc中运行,因此您应该能够:

system 'bash', '-ic', q{osascript -e 'quit app "Chromium"'};

ref:https://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files