【问题标题】:How to run another script from a script and automatically answer prompts [duplicate]如何从脚本运行另一个脚本并自动回答提示[重复]
【发布时间】:2019-05-04 18:15:30
【问题描述】:

我尝试从 script2.sh 运行 script1.sh 并回答脚本 1 的提示。

在script1.sh中:

echo "Do you wish to save these settings?"
read response
if [ $response = "yes" ]
then 
   echo "Settings saved"
elif [ $response = "no" ]
then 
   echo "Settings not saved"
fi
echo "Would like to end"
read response2
if [ $response2 = "yes" ]
then 
   echo "Bye"
elif [ $response2 = "no" ]
then 
   echo "OK"
fi

在script2.sh中:

sh "/home/username/Desktop/script1.sh"

我想要它,这样每当我尝试运行 script2.sh 时,它会自动为 script1.sh 的两个提示输入是或否。

【问题讨论】:

  • 一个更好的设计(这将使您的原始问题标题正确,尽管我现在看到有人对其进行了更新)是将选项作为命令行参数传递。

标签: linux sh


【解决方案1】:

我有一个建议:

在 script1.sh 中:

echo "Do you wish to save these settings?"
read response
while [ "$response" != "yes" ] && [ "$response" != "no" ] 
do
  echo "Please enter yes or no only"
  echo "Do you wish to save these settings?"
  read response 
done

在 script2.sh 中:

source "/home/username/Desktop/script1.sh"

echo yes | sh "/home/username/Desktop/script1.sh"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2017-03-30
    • 2023-03-15
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多