如何调用可能从另一个脚本请求用户输入的 bash 脚本

如何调用可能从另一个脚本请求用户输入的 bash 脚本

我有两个脚本,一个调用另一个脚本,在某些情况下,调用的脚本可能会请求用户输入。目前我只是执行这个脚本,但它甚至不等待输入。如何正确调用此脚本并将与用户的通信传递给它?

主要脚本:

#!/usr/bin/env bash

script_dir=$(dirname "${0}")
hook_name=$(basename "${0}")

hook_dir="${script_dir}/${hook_name}.d"

if [[ -d ${hook_dir} ]]; then
  for hook in "${hook_dir}"/*; do
    "${hook}"  "${@}"
    exit_code=${?}

    if [ ${exit_code} != 0 ]; then
      error=$(tput setaf 1)
      normal=$(tput sgr0)
      script_name=$(basename "${hook}")
      echo "${error}Hook ${hook_name} : ${script_name} failed!${normal}"
      exit ${exit_code}
    fi
  done
fi

exit 0

调用的文件:

#!/usr/bin/env bash

files=("hooks/install.sh" "hooks/install.bat")
changed=$(git diff --cached --name-only)

for file in "${files[@]}"; do
  if [[ ${changed} == *"${file}"* ]]; then
    error=$(tput setaf 1)
    normal=$(tput sgr0)
    message="${error}You have changed one of the files that supposed to work identically!${normal}: [ ${files[*]} ]"
    echo "${message}"
    read -r -p "Are you sure you want to continue? [y/N]: " response
    response=${response,,}
    if [[ $response =~ ^(no|n) ]] || [[ -z $response ]]; then
      exit 1
    fi
  fi
done

exit 0

相关内容