如何在 linux 对话框输入框中保留命令结果的空白?
How can I maintain the whitespace of a command's results in a linux dialog inputbox?
我有一个我一直在处理的 bash 脚本文件,我试图能够列出当前可用的蓝牙设备,然后允许用户选择一个并连接到它。我已经获得了所有可用的代码,但是当您直接从 CLI 命令 运行 时,蓝牙设备列表将不会保留您通常获得的空白。
在理想情况下,我希望能够实际列出蓝牙设备并让用户只 select 一个,但我在这方面遇到了太多麻烦,所以我选择了一个更简单的解决方案,只列出所有这些并要求用户键入他们想要连接的设备的 mac 地址。
我遇到问题的代码是:
echo "Scanning..."
bluetoothDeviceList=$(hcitool scan | sed -e 1d)
if [ "$bluetoothDeviceList" == "" ] ; then
result="No devices were found. Ensure device is on and try again."
display_result "Connect Bluetooth Device"
else
bluetoothMacAddress=$(dialog --title "Connect Bluetooth Device" --backtitle "Pi Assist" --inputbox "$bluetoothDeviceList \n\nEnter the mac address of the device you would like to conect to:" 0 0 2>&1 1>&3);
if [ $bluetoothMacAddress != "" ] ; then
bluez-simple-agent hci0 $bluetoothMacAddress
bluez-test-device trusted $bluetoothMacAddress yes
bluez-test-input connect $bluetoothMacAddress
fi
fi
在代码中我有一个 display_result 函数,我在下面显示了它,只是为了确保您更全面地了解我在做什么。
display_result()
{
dialog --title "" \
--no-collapse \
--msgbox "$result" 0 0
}
您可能会发现类似这样的内容是一个有用的起点。您将需要添加错误处理等
# Need a file to capture output of dialog command
result_file=$(mktemp)
trap "rm $result_file" EXIT
readarray devs < <(hcitool scan | tail -n +2 | awk '{print NR; print [=10=]}')
dialog --menu "Select device" 20 80 15 "${devs[@]}" 2> $result_file
result=$(<$result_file)
answer={devs[$((result+1))]}
dialog has an option to help with this (see manual):
--column-separator string
Tell dialog to split data for radio/checkboxes and menus on the
occurrences of the given string, and to align the split data into columns.
例如,可以使用 sed/awk 将列之间的 space 转换为列分隔符。这样做有助于告诉对话框哪些部分 是 列,以及解决由于使用多字节字符导致的格式问题。
我有一个我一直在处理的 bash 脚本文件,我试图能够列出当前可用的蓝牙设备,然后允许用户选择一个并连接到它。我已经获得了所有可用的代码,但是当您直接从 CLI 命令 运行 时,蓝牙设备列表将不会保留您通常获得的空白。
在理想情况下,我希望能够实际列出蓝牙设备并让用户只 select 一个,但我在这方面遇到了太多麻烦,所以我选择了一个更简单的解决方案,只列出所有这些并要求用户键入他们想要连接的设备的 mac 地址。
我遇到问题的代码是:
echo "Scanning..."
bluetoothDeviceList=$(hcitool scan | sed -e 1d)
if [ "$bluetoothDeviceList" == "" ] ; then
result="No devices were found. Ensure device is on and try again."
display_result "Connect Bluetooth Device"
else
bluetoothMacAddress=$(dialog --title "Connect Bluetooth Device" --backtitle "Pi Assist" --inputbox "$bluetoothDeviceList \n\nEnter the mac address of the device you would like to conect to:" 0 0 2>&1 1>&3);
if [ $bluetoothMacAddress != "" ] ; then
bluez-simple-agent hci0 $bluetoothMacAddress
bluez-test-device trusted $bluetoothMacAddress yes
bluez-test-input connect $bluetoothMacAddress
fi
fi
在代码中我有一个 display_result 函数,我在下面显示了它,只是为了确保您更全面地了解我在做什么。
display_result()
{
dialog --title "" \
--no-collapse \
--msgbox "$result" 0 0
}
您可能会发现类似这样的内容是一个有用的起点。您将需要添加错误处理等
# Need a file to capture output of dialog command
result_file=$(mktemp)
trap "rm $result_file" EXIT
readarray devs < <(hcitool scan | tail -n +2 | awk '{print NR; print [=10=]}')
dialog --menu "Select device" 20 80 15 "${devs[@]}" 2> $result_file
result=$(<$result_file)
answer={devs[$((result+1))]}
dialog has an option to help with this (see manual):
--column-separator string
Tell dialog to split data for radio/checkboxes and menus on the occurrences of the given string, and to align the split data into columns.
例如,可以使用 sed/awk 将列之间的 space 转换为列分隔符。这样做有助于告诉对话框哪些部分 是 列,以及解决由于使用多字节字符导致的格式问题。