読み取るには、コンテンツを bash に自動的に挿入します。

読み取るには、コンテンツを bash に自動的に挿入します。

こんにちは。質問に答えるように、何かを入力するために使用した小さなbashスクリプトがあります。しかし、私がしたいのはbashスクリプトを実行することだけです。これにより、質問に答えるのではなく、自動的に答えが入力されます。

    echo "
Please type in your name "

echo $name > /root/details/name.info
echo "
Your name is $name ! "

echo "
    What is your age "
    read age
    echo $age > /root/details/age.info

echo "
This server will carry out a complete maintenance routine daily
Enter the time at which this routine should happen :
Example : Eleven o'clock is 23 , midnight is 00 , three in the morning is 03 ...
Based on the example , enter the time of maintenance with a decimal number 00-23
Please note: ( 00/23 )"

echo $HorMan>

echo "
    Please enter  your username e.g. admin"
    read username
    echo $username > /root/details/username.info

上記のコードでは、名前と年齢はスクリプトの実行ごとに異なりますが、サーバーのメンテナンスとユーザー名はスクリプトの実行ごとに同じです。

ベストアンサー1

質問に対する答えを正しい順序で1行に1つずつ配置します。

次にスクリプトを実行します。

$ ./script.sh <answers.txt

編集する:質問を更新した後です。

スクリプトへの入力の一部が静的である場合は、スクリプトから入力をまったく読み取らないか(静的データに置き換える)、2番目のスクリプトを使用して値を入力します。

$ ./answerscript.sh <answers.txt | ./script.sh

以下を実行answers.txtしながら、名前と年齢の行を交互に含めます。answerscript.sh

#!/bin/sh

IFS=
while read name; do
  echo $name
  read age
  echo $age
  echo 23
  echo enoch
done

23そしてenoch時間とユーザー名は静的です。)

しかし、今作成中のスクリプトは名前や時間を読みません。

おすすめ記事