linux sh : read user input
发布日期:2021-06-30 22:17:02
浏览次数:2
分类:技术文章
本文共 7597 字,大约阅读时间需要 25 分钟。
前言
看了一段书,将脚本中读取用户输入的实现封装了几个函数。
重定向的写法# 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=0g_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_initfunc_show_titlefunc_show_prog_infofunc_show_os_infofunc_do_taskfunc_show_tailfunc_uinit
转载地址:https://lostspeed.blog.csdn.net/article/details/79316396 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月19日 22时47分54秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux png转jpg (convert命令)
2019-04-30
Ubuntu更新后终端中字体的颜色全是白色
2019-04-30
vscode git
2019-04-30
基于MATLAB的二进制数字调制与解调信号的仿真——2PSK
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——DSB
2019-04-30
HDU - 1166 敌兵布阵 (树状数组模板题/线段树模板题)
2019-04-30
CodeForces - 761C Dasha and Password (思维 暴力)
2019-04-30
CodeForces - 456C Boredom (dp)
2019-04-30
CodeForces - 675A Infinite Sequence(简单数论 细节)
2019-04-30
CodeForces - 1042B Vitamins (思维)
2019-04-30
ACM 2013 长沙区域赛 Collision (几何)
2019-04-30
ACM 2014 鞍山区域赛 E - Hatsune Miku (dp)
2019-04-30
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
CodeForces - 931B World Cup (思维 模拟)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30
java常用类 String面试题
2019-04-30
Windows10下的powershell美化教程
2019-04-30
利用ffmpeg合并音频和视频
2019-04-30