如何从一个脚本中运行另一个脚本并自动回答提示

时间:2019-05-04 09:03:36

标签: linux sh

我试图从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时,它都会自动为两个提示输入yes或no。

1 个答案:

答案 0 :(得分:0)

我有个建议:

在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"