如何使用带有 heredoc 的安装命令

How to use the install command with a heredoc

我正在尝试将安装脚本转换为使用 install 命令,而不是创建文件并针对它创建 运行 chmod +x。这是现在的脚本:

#!/usr/bin/env bash

install_target=/usr/local/bin/my_prog
volatile_path=/this/path/could/change

cat << EOF > "$install_target"
#!/usr/bin/env bash

"$volatile_path/some_other_executable" "$@"
EOF
chmod +x "$install_target"

我更愿意做的是:

#!/usr/bin/env bash

install_target=/usr/local/bin/my_prog
volatile_path=/this/path/could/change

install "$install_target" << EOF 
#!/usr/bin/env bash

"$volatile_path/some_other_executable" "$@"
EOF

我缺少什么来完成这项工作?

根据评论,假设您使用的是 BSD 版本的安装(GNU install 具有 install --help 显示的完整帮助,而 BSD 仅显示基本用法)我认为这就是你想做的事:

#!/usr/bin/env bash

install_target=/usr/local/bin/my_prog
volatile_path=/this/path/could/change
temp_file=/tmp/[=10=].$$.$RANDOM

cat << EOF > "$temp_file"
#!/usr/bin/env bash

"$volatile_path/some_other_executable" "$@"
EOF

install -bd "$temp_file" "$install_target"
rm -f "$temp_file"

未经测试,但使用 process substitution 应该不需要临时文件:

#!/usr/bin/env bash

install_target=/usr/local/bin/my_prog
volatile_path=/this/path/could/change

install -bd <(cat << EOF
#!/usr/bin/env bash

"$volatile_path/some_other_executable" "$@"
EOF
) "$install_target"