コンテンツ
1.読み取りコマンド
コマンド形式:読み取り[オプション][変数名]
サブオプション:-p「プロンプト情報」:#読み取り入力を待機しているときに、プロンプト情報を出力します。
-t秒:#readコマンドは常にユーザー入力を待機します。このオプションを使用して待機時間を指定します。
-n文字数:#readコマンドは、指定された文字数のみを受け入れて実行します。
-s#機密情報の入力に適した入力コンテンツを非表示にします。
変数名:変数名はカスタマイズ可能です。変数名が指定されていない場合、入力はデフォルトの変数REPLYに保存されます。変数名が1つだけ指定されている場合は、入力行全体が変数に割り当てられます。複数の変数名が指定されている場合、入力行は単語に分割され、各変数が次々に割り当てられ、コマンドラインの最後の変数が残りのすべての単語を取得します。
2.読み取りコマンドの基本的な使用法
#!/bin/bash
echo -n "Enter your name : "
read name
echo "hello $name ! welcome to BeiJing"
3.readは変数を指定しません
#!/bin/bash
read -p "Enter your name : "
echo
echo "hello $REPLY ! welcome to BeiJing"
4.読み取り入力タイムアウト書き込み方法
#!/bin/bash
if read -t 5 -p "Enter your name : "
then
echo "hello $REPLY,welcome to BeiJing"
else
echo "sorry, Output timeout, please execute the command again !"
fi
5.隠された入力を読む
#!/bin/bash
if read -t 5 -p "Enter your name : "
then
echo "hello $REPLY,welcome to BeiJing"
else
echo "sorry, Output timeout, please execute the command again !"
fi
echo "---------"
if read -s -t 5 -p "please enter your password : "
then
echo -n "status : $? , Ok"
else
echo "sorry, Output timeout, please execute the command again !"
fi
6.読み取りコマンドのデモンストレーションの例
[root @ localhost〜]#cat xx.sh#xx.shという名前のスクリプトを作成します
#!/bin/bash
read -t 30 -p "请输入姓名:" name #等待30秒,提示信息为“姓名..”,赋予变量”name“
read -t 30 -s -p "请输入年龄:" age #等待30秒,隐藏输入内容提示信息为“年龄..”变量age
echo -e "" #输入一个空行,没有的话在当前格式默认不换行。
read -t 30 -n 1 -p "请输入性别:" sex #等待30秒,提示信息为性别..,字符数1,变量sex
echo -e "" #输入一个空行,没有的话在当前格式默认不换行。
echo "$name" #分别输出$name $age $sex
echo "$age"
echo "$sex