将 bash 表达式的输出传递给通过对话框进度框管道传输的变量会导致变量为空值

问题描述

我将 dialog Version: 1.3-20190211GNU bash,version 5.0.3(1)-release 一起使用,并具有以下脚本片段,用于扫描 wlan0 上的网络。

#!/bin/bash

# duplicate file descriptor 1 on file descriptor 3
exec 3>&1

# Scan for networks and inform the user of scan in progress
scan=$(iwlist wlan0 scan) | dialog --backtitle "Secure WiFi Bridge - $(hostname)" \
                                   --title "Scan in Progress" \
                                   --clear \
                                   --progressBox "Scanning for networks in range..." 0 0 2>&1 1>&3

# Close file descriptor 3
exec 3>&-

clear
echo "$scan"

这确实会运行扫描,但是会弹出对话框; $scan 变量在稍后尝试在脚本中使用时为空。上面的 echo "$scan" 仅用于演示目的,以表明输出为空。如何在显示 $scan 时设置 progressBox 变量?

更新

我意识到我可以用 scan=$(iwlist wlan0 scan) | dialog 更改 iwilst wlan0 scan > /tmp/scan_result | dialog,然后我可以用 scan="$(cat /tmp/scan_result)"文件读入变量,但我更愿意避免使用写入/读取如果可能的话,到/从一个文件

解决方法

我稍微改变了策略就设法获得了预期的结果。由于通过 progressbox 的管道似乎导致 $scan 被填充到子 shell 中,我选择在一个简单的 printf 语句中简单地管道并从 {{ 1}} 声明。这使得 --cleardialog 运行时显示。

progressbox