从Emacs运行多字符串bash脚本

Mir*_*lov 5 emacs shell executable elisp

我想运行以下bash脚本,它存储在Elisp字符串中,而不是存储在.sh文件中,然后将shell输出存储在变量中.

#!/bin/bash
IFS=: read -ra _dirs_in_path <<< "$PATH"

for _dir in "${_dirs_in_path[@]}"; do
    for _file in "${_dir}"/*; do
        [[ -x ${_file} && -f ${_file} ]] && printf '%s\n' "${_file##*/}"
    done
done
Run Code Online (Sandbox Code Playgroud)

我无法运行shell-command由多个字符串组成的bash脚本.Emacs和Long Shell命令也没有帮助我,因为compile并且comint-run还需要命令,而不是bash语法.

如何从Elisp运行复杂的bash脚本?

Sea*_*ean 6

多行命令可以作为参数提供,bash -c如果你引用它们就像你可能包含shell元字符的任何其他shell参数一样,例如:

(setq my-command
      (concat "IFS=: read -ra dirs <<<\"$PATH\"\n"
              "for dir in ${dirs[@]}; do\n"
              " echo got dir \"$dir\"\n"
              "done\n"))

(shell-command (format "bash -c %s" (shell-quote-argument my-command)))
Run Code Online (Sandbox Code Playgroud)