shell 获取用户输入

#!/bin/bash


#用系统变量REPLY保存输入的值

read_2reply()
{
    read -p "Enter a number:"
    for (( count=1; count<=$REPLY; count++ ))
    do
        count=$[ $count +1 ]
    done
    echo "The count of your number is $count"

}


#读取多个值

read_2p()
{
    read -p "Enter your name:" first last
    echo "Your English format name is:$last $first"
}


#设置读取时间和读取的字符数

read_tn()
{
    if read -t 5 -n 1 -p "Do you want to continue [Y/N]:"
    then
        case $REPLY in
            Y|y)
                echo -e "\ncontinue on ...\n\n\n"

                read -s -p "Please input your password:" pass
                if [ $pass == "123456" ]
                then
                    echo -e "\nYou are pass is ok!\n"
                else
                    echo -e "\nYou are bad boy with wrong pass!\n"
                fi

                ;;
            N|n)
                echo -e "\nOK,goodbye\n"
                exit
                ;;
        esac
    else
            echo -e "You are too slow!\n"
    fi
}


#读取文件

read_file()
{
    echo "hello"
    if read -t 10 -p "Please input file name you want to see:" file_name
    then
        if [ -n "$file_name" ]
        then
            count=1
            cat $file_name |while read line
            do
                echo "Line $count:$line"
                count=$[ $count +1  ]
            done
        fi
    else
        echo -e "\nSorry you are too slow, input time is over!"
    fi
}


#测试

#function test
#read_2reply
#read_2p yang
#read_tn
read_file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值