0

This works to create a name-picker radiolist dialog box:

dialog --stdout --no-tags --clear --backtitle "Name Picker" --title " Name Selection " --radiolist " Select the name " 12 95 0 "Adam Zero" "Adam Zero" off "John Smith" "John Smith" off > file

What I need, however, is for the names to come out of a Mariadb or MySQL database. So:

create table names (fullname varchar(128));
insert into names values ('John Smith');
insert into names values ('Adam Zero');

IFS=$'\n'; unset NAMELIST
results=( $(/usr/bin/mariadb -N -B -u myuser -h 10.0.0.25 -pmypass -P 3306 -D mydb --skip-ssl -e "select distinct fullname from names order by fullname" ) )
for i in ${results[*]}; do
  NAMELIST=$(echo $NAMELIST "\"$i\" \"$i\" off")
done  
echo "$NAMELIST"

...which returns this:

"Adam Zero" "Adam Zero" off "John Smith" "John Smith" off

...which appears to be exactly the same string as appeared at the end of the earlier, working 'dialog' command, so I just substitute it in:

dialog --stdout --no-tags --clear --backtitle "Name Picker" --title " Name Selection " --radiolist " Select the name " 12 95 0 $NAMELIST > file

...and get this result:

Expected at least 20 tokens for --radi, have 5.
Use --help to list options.

The same error returns whether I use $NAMELIST or "$NAMELIST" (i.e., with or without quotation marks)

First question: Why can't I just substitute into the dialog command a variable whose textual contents are exactly the same as text that, specified as static text, works?

More important question: How can I re-write this bash code to allow the results of a single-column query from MariaDB or MySQL to be successfully the source of a dialog radiolist dialog?

I've seen questions here about reading in data from a file, but not from a database... and I wasn't able to adapt my code accordingly to get a usable result. Any insights appreciated.

0

2 Answers 2

2

Try this:

#!/bin/bash

# Connection to DB
DB_USER="root"
DB_PASS="root"
DB_HOST="localhost"
DB_PORT="3306"
DB_NAME="mydb"

# Read names from the DB
IFS=$'\n' read -r -d '' -a results < <(mariadb -N -B -u $DB_USER -h $DB_HOST -p$DB_PASS -P $DB_PORT -D $DB_NAME --skip-ssl -e "SELECT DISTINCT fullname FROM names ORDER BY fullname" && printf '\0')

# Prepare the options array
options=()
for name in "${results[@]}"; do
    options+=("$name" "$name" "off")
done

# Check if the options array is empty
if [ ${#options[@]} -eq 0 ]; then
  echo "No names found in the database!"
  exit 1
fi

# Use dialog with the correctly formatted options
dialog --stdout --no-tags --clear --backtitle "Name Picker" --title "Name Selection" --radiolist "Select the name" 12 95 0 "${options[@]}" > file

dialog expects each element of the array to be passed as a separate argument, and as a result it fails to create the list.

Sign up to request clarification or add additional context in comments.

1 Comment

First off, that works, so very helpful. Can you explain what the 'printf '\0' bit is doing at the end of the query, please? Additionally, I don't quite get the point of the 'each element needs to be passed as a separate argument' bit. If the command requires an input of 'A B C' and my variable has a value of 'A B C', why is the variable not seen as passing three values to the command? I guess you're saying dialog sees $NAMELIST as one thing, even though its value is multiple separate things... is that just a dialog quirk? Or a bashism? But: thank you for a code example that works!
1

I'm glad I could help you. Sorry if I'm replying after a day. I'll reply with another answer here because it's too long in the comments.

So printf \0 is used to tell bash that the command is finished, a bit like saying ; in programming languages, to mean that the instruction is finished. This happens if you're in bash, while in the MariaDB terminal it returns lines with \n.

While for $NAMELIST it's a Bash problem, not a dialog problem.

For this you need to use options[@], because it allows you to pass each separate element since it's an array.

If you pass it as a string, Bash interprets it as a single value, even if there are spaces.

To give you a practical example to help you understand the concept, I once came across a database with phone numbers.

The problem was that some numbers started with 0, and the computer if you assigned that number as an integer, the 0 cut it, and consequently the number was very wrong.

Example:

Int number = 055333333 —> 55333333

String number = 055333333 —> 055333333

In this case I had to use a string to solve the problem, rightly the 0 in front of a number without a comma, it's as if it wasn't there.

This is an example in reverse, but it's to make you understand how sometimes you have to interpret things differently on the PC.

Sorry for my English. If you have other questions don't hesitate to ask, for now bye bye and good continuation with the script

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.