Linux shell:dialog命令的使用

本文详细解析了一段bash脚本,该脚本利用dialog和tput命令创建了一个简单的交互式对话框,用于获取用户输入并做出响应。通过脚本的运行和分析,读者可以深入了解如何在bash环境中实现基本的用户交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面这段代码来自http://unix.stackexchange.com/questions/67877/screen-buffer-and-dialog

#!/usr/bin/env bash
tput smcup
clear
dialog --yesno "Do you want to continue?" 0 0
rc=$?
clear
tput rmcup
if [ "${rc}" == "0" ]; then
  echo Yes
else
  echo No
fi

运行:

分析: tput smcup用来保存当前的display,tput rmcup用来恢复之前保存的display。

dialog命令用来显示对话框。--yesyno参数的解释如下:

       --yesno text height width
              A yes/no dialog box of size height rows by width columns will be
              displayed.  The string specified by text is displayed inside the
              dialog  box.   If this string is too long to fit in one line, it
              will be automatically divided into multiple lines at appropriate
              places.  The text string can also contain the sub-string "\n" or
              newline characters `\n' to  control  line  breaking  explicitly.
              This  dialog box is useful for asking questions that require the
              user to answer either yes or no.  The dialog box has a Yes  but‐
              ton  and  a  No  button, in which the user can switch between by
              pressing the TAB key.

              On exit, no text is written to dialog's output.  In addition  to
              the "Yes" and "No" exit codes (see DIAGNOSTICS) an ESC exit sta‐
              tus may be returned.

              The codes used for "Yes" and "No" match those used for "OK"  and
              "Cancel", internally no distinction is made.

若选择yes,退出码为0;选择no,则为非0。

$?是上一条命令的退出码。

转载于:https://my.oschina.net/letiantian/blog/527085

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值