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

Linux 如何从脚本运行另一个脚本并自动回答提示,linux,sh,Linux,Sh,我正在尝试从script2.sh运行script1.sh并回答script1的提示 在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

我正在尝试从script2.sh运行script1.sh并回答script1的提示

在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
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中:

sh "/home/username/Desktop/script1.sh"
source "/home/username/Desktop/script1.sh"
我希望这样,每当我尝试运行script2.sh时,它都会自动为script1.sh的两个提示输入yes或no。

我有一个建议:

在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
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中:

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

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