#!/bin/bash stty -echo while

本文介绍了一个简单的Shell脚本示例,演示了如何通过脚本来实现用户密码输入验证的功能,并展示了如何使用基本的Shell命令来控制程序流程。
sql 代码
 
  1. #!/bin/bash  
  2.   
  3. # none 1,2,3,15,20 signal  
  4. trap '' 1 2 3 15 20  
  5.   
  6. #clear screen ,close redisplay  
  7. clear  
  8. stty -echo  
  9.   
  10. #set password  
  11. echo -n "Enter your codeword: "  
  12. read secretcode  
  13. echo  
  14.   
  15. #confirm password  
  16. echo -n "Enter your codeword again: "  
  17. read same  
  18. echo  
  19. if [ $secretcode != $same ]  
  20.     then  
  21.         echo "Work on your short-term memory before using this code! "  
  22.         stty echo  
  23.         exit 1  
  24. fi  
  25.   
  26. #get user input  
  27. clear  
  28. echo -n "Enter the code word: "  
  29. read yourguess  
  30. echo  
  31.   
  32.   
  33. #only input is right codeword ,or loop forever  
  34. while [ "$secretcode" != "$yourguess" ]  
  35. do  
  36.     clear  
  37.     echo "secretcode: $secretcode \n yourguess: $yourguess"  
  38.     echo -n "Enter the code word: "  
  39.     read yourguess  
  40. done  
  41.   
  42. #input real password to continue  
  43. clear  
  44. echo "Back gain! "  
  45. stty echo  
  46. exit 0  
sql 代码
  1. #shell   
  2. #! /bin/bash  
  3. files=(`ls`) numfiles=`ls | wc -w`  
  4.   
  5. echo ${files[*]}  
  6. echo ${files[1]}  
#!/system/bin/sh # 双缓冲倒计时脚本 - 修复显示和输入错误问题 # 初始化变量 target_year=0 target_month=0 target_day=0 target_hour=0 target_minute=0 target_second=0 remaining_seconds=0 is_paused=true is_running=false last_update=0 refresh_rate=1 # 刷新频率(秒) # 双缓冲系统初始化 init_double_buffer() { # 创建两个临时文件作为缓冲区 buffer1=$(mktemp) buffer2=$(mktemp) # 设置当前显示缓冲区和后台缓冲区 current_buffer=$buffer1 back_buffer=$buffer2 # 隐藏光标 echo -ne "\033[?25l" # 保存初始光标位置 echo -ne "\033[s" } # 切换缓冲区 swap_buffers() { # 恢复光标到初始位置 echo -ne "\033[u" # 清除从光标到屏幕末尾的内容 echo -ne "\033[J" # 显示后台缓冲区内容 cat $back_buffer # 交换缓冲区 temp=$current_buffer current_buffer=$back_buffer back_buffer=$temp # 清空新的后台缓冲区 > $back_buffer } # 绘制内容到后台缓冲区 draw_to_back_buffer() { # 将内容追加到后台缓冲区 echo -e "$@" >> $back_buffer } # 清理资源 cleanup() { # 显示光标 echo -ne "\033[?25h" # 删除临时文件 rm -f $buffer1 $buffer2 # 清屏 echo -ne "\033[2J\033[H" } # 计算剩余秒数 calculate_remaining() { current_epoch=$(date +%s) # 计算目标时间的Unix时间戳 target_date="${target_year}-${target_month}-${target_day} ${target_hour}:${target_minute}:${target_second}" target_epoch=$(date -d "$target_date" +%s 2>/dev/null) if [ -z "$target_epoch" ]; then draw_to_back_buffer "错误:无效的日期格式" return 1 fi remaining_seconds=$((target_epoch - current_epoch)) return 0 } # 格式化时间显示 format_time() { local seconds=$1 # 计算总天数 local total_days=$((seconds / 86400)) local years=$((seconds / 31536000)) seconds=$((seconds % 31536000)) local months=$((seconds / 2592000)) seconds=$((seconds % 2592000)) local days=$((seconds / 86400)) seconds=$((seconds % 86400)) local hours=$((seconds / 3600)) seconds=$((seconds % 3600)) local minutes=$((seconds / 60)) local secs=$((seconds % 60)) # 优化显示格式 printf "总天数:%d | %d年%02d月%02d日 %02d:%02d:%02d" $total_days $years $months $days $hours $minutes $secs } # 更新剩余时间 update_remaining() { if [ $is_running = true ] && [ $is_paused = false ]; then current_time=$(date +%s) # 确保每秒只更新一次 if [ $current_time -gt $last_update ]; then if [ $remaining_seconds -gt 0 ]; then remaining_seconds=$((remaining_seconds - 1)) last_update=$current_time return 0 # 需要刷新 else # 时间到处理 is_running=false draw_to_back_buffer "时间到!" echo -e "\a" # 发出提示音 return 1 # 需要刷新 fi fi fi return 2 # 不需要刷新 } # 构建菜单内容 build_menu() { # 清空后台缓冲区 > $back_buffer # 绘制菜单框架 draw_to_back_buffer "======== 双缓冲倒计时菜单 ========" draw_to_back_buffer "1. 设置目标时间" draw_to_back_buffer "2. 开始倒计时" draw_to_back_buffer "3. 暂停/继续" draw_to_back_buffer "4. 重置倒计时" draw_to_back_buffer "5. 退出程序" draw_to_back_buffer "===============================" # 显示目标时间 draw_to_back_buffer "当前目标: " if [ $target_year -ne 0 ]; then draw_to_back_buffer " $(printf "%d-%02d-%02d %02d:%02d:%02d" $target_year $target_month $target_day $target_hour $target_minute $target_second)" else draw_to_back_buffer " 未设置" fi # 显示剩余时间 draw_to_back_buffer "剩余时间: " if [ $target_year -ne 0 ]; then if [ $is_running = true ] && [ $is_paused = false ]; then # 动态更新模式 draw_to_back_buffer " $(format_time $remaining_seconds) [运行中]" else # 静态显示模式 if calculate_remaining; then draw_to_back_buffer " $(format_time $remaining_seconds) [暂停]" else draw_to_back_buffer " 计算错误" fi fi else draw_to_back_buffer " 未设置" fi draw_to_back_buffer "===============================" draw_to_back_buffer "请选择操作 [1-5]: " } # 设置目标时间 set_target() { # 临时保存当前缓冲区状态 temp_buffer=$(mktemp) cat $back_buffer > $temp_buffer # 显示输入提示 draw_to_back_buffer "请输入年份 (YYYY): " swap_buffers # 修复输入错误 stty -echo # 关闭回显,避免输入显示在菜单上 read target_year stty echo # 恢复回显 draw_to_back_buffer "请输入月份 (1-12): " swap_buffers stty -echo read target_month stty echo draw_to_back_buffer "请输入日期 (1-31): " swap_buffers stty -echo read target_day stty echo draw_to_back_buffer "请输入小时 (0-23): " swap_buffers stty -echo read target_hour stty echo draw_to_back_buffer "请输入分钟 (0-59): " swap_buffers stty -echo read target_minute stty echo draw_to_back_buffer "请输入秒数 (0-59): " swap_buffers stty -echo read target_second stty echo # 验证输入 valid=true case $target_year in [0-9][0-9][0-9][0-9]) ;; *) draw_to_back_buffer "错误: 年份必须是4位数字"; valid=false ;; esac case $target_month in 0[1-9]|1[0-2]|[1-9]) ;; *) draw_to_back_buffer "错误: 月份必须是1-12"; valid=false ;; esac case $target_day in 0[1-9]|[12][0-9]|3[01]|[1-9]) ;; *) draw_to_back_buffer "错误: 日期必须是1-31"; valid=false ;; esac case $target_hour in [01][0-9]|2[0-3]|[0-9]) ;; *) draw_to_back_buffer "错误: 小时必须是0-23"; valid=false ;; esac case $target_minute in [0-5][0-9]|[0-9]) ;; *) draw_to_back_buffer "错误: 分钟必须是0-59"; valid=false ;; esac case $target_second in [0-5][0-9]|[0-9]) ;; *) draw_to_back_buffer "错误: 秒数必须是0-59"; valid=false ;; esac if [ $valid = true ]; then # 计算剩余时间 if calculate_remaining; then if [ $remaining_seconds -le 0 ]; then draw_to_back_buffer "错误: 目标时间已过去!" else draw_to_back_buffer "目标时间已设置!" last_update=$(date +%s) fi fi fi swap_buffers sleep 1 # 恢复菜单状态 cat $temp_buffer > $back_buffer rm -f $temp_buffer } # 主程序入口 main() { # 初始化双缓冲系统 init_double_buffer # 初始构建菜单 build_menu swap_buffers # 主循环 while true; do # 更新剩余时间 update_remaining refresh_needed=$? # 检查是否需要刷新显示 if [ $refresh_needed -lt 2 ]; then build_menu swap_buffers fi # 修复输入错误:使用替代方法避免系统调用中断 # 尝试读取输入,忽略可能的错误 if read -t 0.1 -n 1 choice 2>/dev/null; then # 处理用户选择 case $choice in 1) set_target # 重建菜单 build_menu swap_buffers ;; 2) if [ $target_year -eq 0 ]; then draw_to_back_buffer "请先设置目标时间!" swap_buffers sleep 1 elif [ $is_running = true ]; then draw_to_back_buffer "倒计时已在运行中!" swap_buffers sleep 1 else is_running=true is_paused=false last_update=$(date +%s) draw_to_back_buffer "倒计时已开始!" swap_buffers sleep 0.5 fi # 重建菜单 build_menu swap_buffers ;; 3) if [ $is_running = true ]; then is_paused=$([ "$is_paused" = true ] && echo false || echo true) draw_to_back_buffer "已${is_paused:+"暂停":"继续"}" swap_buffers sleep 0.5 else draw_to_back_buffer "倒计时未运行!" swap_buffers sleep 0.5 fi # 重建菜单 build_menu swap_buffers ;; 4) target_year=0 target_month=0 target_day=0 target_hour=0 target_minute=0 target_second=0 remaining_seconds=0 is_running=false is_paused=true draw_to_back_buffer "已重置倒计时" swap_buffers sleep 0.5 # 重建菜单 build_menu swap_buffers ;; 5) cleanup echo "退出程序" exit 0 ;; *) draw_to_back_buffer "无效选择,请重新输入" swap_buffers sleep 0.5 # 重建菜单 build_menu swap_buffers ;; esac fi # 添加短暂延迟,减少CPU使用率 sleep 0.1 done } # 启动主程序 main 在测试过程中又出现新的问题了,输入所有菜单数字选项包括年月日都不显示数字
最新发布
08-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值