English 中文(简体)
我如何在 whi子中使用阵列?
原标题:How do I use an array in a whiptail menu?

试图在有系统安装的含水层机器上写一个简单的文字,以显示使用 whi尾阵列的时间区清单。 至今:

tzones=$(timedatectl list-timezones)
arr=( $tzones )
count=0
for i in "${arr[@]}"; do allzones[$count]=$(echo ""$i""); ((count=count+1)); done
for i in "${allzones[@]}"; do echo $i; done #So I know I have a list of items in quotes at this point
whiptail --title "Time zone" --menu "Select time zone" 25 78 16 "${allzones[@]}"

Bash keeps spitting out a help menu for whiptail. I ve only just started messing with whiptail and I ve been at this script a while so not sure what I m missing.

问题回答

你们似乎希望使用连续编号作为标签。 您可在各时区清单中使用<代码>cat -n,以便按一定数目预先排列。 由于时区没有空间,因此没有必要使用阵列或进一步照顾间隔。

list=$(timedatectl list-timezones | cat -n)
whiptail --title "Time zone" --menu "Select time zone" 25 78 16 $list

The menu will look something like this:

─────────┤ Time zone ├────────

Select time zone

    1   Africa/Abidjan
    2   Africa/Accra
    3   Africa/Addis_Ababa
    4   Africa/Algiers
    :   :

The last arguments to the --menu option of whiptail must be a sequence of pairs of tag and item, where the chosen tag is send to stderr. Then the array allzones should look like 0 Africa/Abidjan 1 Africa/Accra ...

Then would you try something like:

#!/bin/bash

tzones=( $(timedatectl list-timezones) )
for (( i = 0; i < ${#tzones[@]}; i++ )); do
    allzones+=("$i" "${tzones[$i]}")
done
whiptail --title "Time zone" --menu "Select time zone" 25 78 16 "${allzones[@]}"

If you want to assign a variable to the chosen tag, modify the last line as:

val=$(whiptail --title "Time zone" --menu "Select time zone" 25 78 16 "${allzones[@]}" 3>&1 1>&2 2>&3)

echo "$val"     # just to test

If you want to suppress the numbers, put --notags option to whiptail command.





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签