1、键盘输入
#!/bin/bash
echo "input a number"
#echo -n "Please input a number " 注:echo -n 为本行输入,没有参数则为换行输入
read ans
echo "Your input number is $ans"
exit 0 ;
2、键盘输入
#!/bin/bash
read -p "Enter a user :"
echo $REPLY
cd /var/log/
id=`cat maillog |grep $REPLY |awk '{print $6}' |uniq`
echo $id
for i in $id
do
cat /var/log/maillog |grep $i
done
exit 0 ;
3、时间限制 参数 -t
#!/bin/bash
if read -t 5 -p "please enter your name:" name
then
echo "hello $name, welcome to my script"
else
echo "sorry,too slow"
fi
exit 0
4、read 查询日志
#!/bin/bash
if read -t 10 -p "please enter the user in 10 seconds :" user
then
echo "The user is:$user , -------- "
{
id=`cat /var/log/maillog |grep $user |awk '{print $6}' |uniq`
if [ "$id" != "" ]
then
echo "We found the log ID: $id ,the detailed is as follows "
for i in $id
do
cat /var/log/maillog |grep $i
done
else
echo "The $user did not out send email,bye "
fi
}
else
echo "sorry,you are too slow ,bye !!!"
fi
exit 0
5、read 当输入的字符数目达到预定数目时,自动退出,并将输入的数据赋值给变量
#!/bin/bash
read -n1 -p "Do you want to continue [Y/N]? " answer
case $answer in
Y|y)
echo "good ,!!!!!";;
N|n)
echo "ok,go on";;
*)
echo "error choice";;
esac
exit 0
6、不显示 参数 -s ,read命令将文本颜色设置成与背景相同的颜色
#!/bin/bash
read -s -p "Enter your password: " pass
echo "your password is $pass"
exit 0
7、每行前添加序号 cat 命令的输出作为read命令的输入,读到的值放在line中
#!/bin/bash
count=1
cat dat| while read line
do
echo "$count: $line"
count=$(($count + 1))
done
exit 0