代码之家  ›  专栏  ›  技术社区  ›  StevieD

Bash config文件在源于Perl脚本时抛出错误

  •  1
  • StevieD  · 技术社区  · 7 年前

    有一行perl代码,在它发出消息后调用bash包装器函数 .bash_profile

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

    /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 5.0.2版。

    1 回复  |  直到 7 年前
        1
  •  6
  •   glenn jackman    7 年前

    perl的 system 使用 /bin/sh 就像贝壳一样( https://perldoc.perl.org/functions/system.html

    您需要显式调用bash:

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

    使用 q{}


    bash注意:如果将其作为交互式shell调用,它会自动在bashrc中发出slurp,因此您应该能够执行以下操作:

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

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