linux sh : read user input

前言

看了一段书,将脚本中读取用户输入的实现封装了几个函数。
重定向的写法

#   redirect output to file use : 1> log_file 
#   redirect error to file use : 2> log_file 
#   cmd param 1> /home/dev/run_result.txt 2> /home/dev/run_result.txt 

实验

# !/bin/bash
# @file demo_003.sh
# @brief Knowledge point below
#   * read user input, can process input timeout, e.g. read_user_input()
#   * read input form yes or no, e.g. read_user_input_yes_or_no()
#   * read password input, e.g. read_user_input_password()

# --------------------------------------------------------------------------------
# define macro
# --------------------------------------------------------------------------------

PROG_NAME="rec user input"
PROG_VER="1.0.0.3"
PROG_TIME="2018-02-12 12:54"

IND_CHAR=-
LINE10=${IND_CHAR}${IND_CHAR}${IND_CHAR}${IND_CHAR}${IND_CHAR}${IND_CHAR}${IND_CHAR}${IND_CHAR}${IND_CHAR}${IND_CHAR}
LINE80=${LINE10}${LINE10}${LINE10}${LINE10}${LINE10}${LINE10}${LINE10}${LINE10}

WALL_CHAR='|'

# --------------------------------------------------------------------------------
# global value
# --------------------------------------------------------------------------------

#
# err code define
#
g_err_code_base=0
g_err_code_ok=$(echo "scale=4; $g_err_code_base + 0" | bc)
g_err_code_err=$(echo "scale=4; $g_err_code_base + 1" | bc)
g_err_code_timeout=$(echo "scale=4; $g_err_code_base + 2" | bc)
g_err_code_input_yes=$(echo "scale=4; $g_err_code_base + 3" | bc)
g_err_code_input_no=$(echo "scale=4; $g_err_code_base + 4" | bc)

# --------------------------------------------------------------------------------
# init
# --------------------------------------------------------------------------------
function func_init() {
    local i_index=0

    clear

    # print 25 empty lines for run result
    for (( i_index=0; i_index < 25; i_index++ ))
    do
        echo
    done
}

# --------------------------------------------------------------------------------
# uinit
# --------------------------------------------------------------------------------
function func_uinit() {
    exit 0
}

# --------------------------------------------------------------------------------
# show title
# --------------------------------------------------------------------------------
function func_show_title() {
    echo -e "${LINE80}"

    echo -e -n "${WALL_CHAR}"
    echo -e "\t@brief\twrap some functin to recv user input"

    echo -e "${LINE80}"
}

# --------------------------------------------------------------------------------
# show tail
# --------------------------------------------------------------------------------
function func_show_tail() {
    echo -e "${LINE80}"

    echo -e -n "${WALL_CHAR}"
    echo -e "\tEND"

    echo -e "${LINE80}"
}

# --------------------------------------------------------------------------------
# show os info
# --------------------------------------------------------------------------------
function func_show_os_info() {
    local str_date=$(date +%Y_%m_%d_%H_%M_%S)

    # show time
    echo -e -n "${WALL_CHAR}"
    echo -e "\t${str_date}"

    # show shell
    echo -e -n "${WALL_CHAR}"
    echo -e "\t${SHELL}"

    # show language
    echo -e -n "${WALL_CHAR}"
    echo -e "\t${LANG}"

    # show user
    echo -e -n "${WALL_CHAR}"
    echo -e "\t${USER}"

    echo -e "${LINE80}"
}

# --------------------------------------------------------------------------------
# show prog info
# --------------------------------------------------------------------------------
function func_show_prog_info() {
    # show prog name
    echo -e -n "${WALL_CHAR}"
    echo -e "\t${PROG_NAME}"

    # show prog version
    echo -e -n "${WALL_CHAR}"
    echo -e "\t${PROG_VER}"

    # show code modify time
    echo -e -n "${WALL_CHAR}"
    echo -e "\t${PROG_TIME}"

    echo -e "${LINE80}"
}

# --------------------------------------------------------------------------------
# @fn read_user_input()
# @param IN sz_tip
# @param OUT g_str_user_input
# @return g_err_code_
# --------------------------------------------------------------------------------

g_str_user_input=""
function read_user_input() {
    local rc=0

    echo -e -n "$1 :"

    # -t x is set timeout
    read -t 5 g_str_user_input
    rc=$?
    echo

    if [ $rc -ne 0 ]
    then
        return $g_err_code_timeout
    fi

    return $g_err_code_ok
}

# --------------------------------------------------------------------------------
# @fn read_user_input_yes_or_no()
# @param IN sz_tip
# @return g_err_code_input_yes or g_err_code_input_no
# --------------------------------------------------------------------------------

function read_user_input_yes_or_no() {
    local rc=0
    str_user_input=""

    while $true
    do
        echo -e -n "$1 :"

        # -t x is set timeout
        read -n1 -t 5 str_user_input
        rc=$?
        echo

        # case input timeout
        if [ $rc -ne 0 ]
        then
            return $g_err_code_input_yes # if user not input, default is YES
        fi

        # case input is empty or press enter key
        if [ -z $str_user_input ]
        then
            # use input content is empty
            return $g_err_code_input_yes # if user not input, default is YES
        fi

        case $str_user_input in
        "Y" | "y")
            return $g_err_code_input_yes
            ;;
        "N" | "n")
            return $g_err_code_input_no
            ;;
        *)
            continue
            ;;
        esac
    done

    return $g_err_code_input_yes
}

# --------------------------------------------------------------------------------
# @fn read_user_input_password()
# @param IN sz_tip
# @param OUT g_str_user_input_password
# @return g_err_code_ok or g_err_code_err
# --------------------------------------------------------------------------------
g_str_user_input_password=""
function read_user_input_password() {
    local rc=0

    while $true
    do
        echo -e -n "$1 :"

        # -s is hide display
        read -s g_str_user_input_password
        echo

        # case input is empty or press enter key
        if [ -z $g_str_user_input_password ]
        then
            # use input content is empty
            echo "password not allow to empty :)"
            continue
        fi

        if ! [ -n $g_str_user_input_password ]
        then
            # use input content is empty
            echo "password not allow to empty :)"
            continue
        fi

        # is password not empty, is right, can return password
        break
    done

    return $g_err_code_ok
}

# --------------------------------------------------------------------------------
# do task
# --------------------------------------------------------------------------------
function func_do_task() {
    local rc=0

    read_user_input_password "please input password"
    rc=$?
    case $rc in
    $g_err_code_ok)
        echo "password is: [$g_str_user_input_password]"
        ;;
    *)
        echo "not input password"
        ;;
    esac
    return

    read_user_input_yes_or_no "will be quit prog, are you sure? (Y/N)"
    rc=$?
    case $rc in
    $g_err_code_input_yes)
        echo "user input is YES"
        ;;
    $g_err_code_input_no)
        echo "user input is NO"
        ;;
    *)
        echo "error : not YES or NO"
        ;;
    esac

    read_user_input "please input"
    rc=$?
    if [ $rc -eq $g_err_code_ok ]
    then
        echo "user input is : $g_str_user_input"
    elif [ $rc -eq $g_err_code_timeout ]
    then
        echo "read time out, please use default input value"
    else
        echo "unknow error when read user input"
    fi
}

# --------------------------------------------------------------------------------
# main shell script
# --------------------------------------------------------------------------------
func_init
func_show_title
func_show_prog_info
func_show_os_info

func_do_task

func_show_tail
func_uinit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值