Alex
Alex

Reputation: 11

bash dynamic dialog

I have a task to write simple bash script that adds deletes and views entries from file.

The requirement is to use "dialog"

data structure in file:

Name Surname [email protected]
Another New [email protected]

basically i have accomplished everything except delete, i know how to do the delete itself(with "sed" i think?)

But i need to use dialog --menu to display the search results.

The menu item should be whole line of text i think as after selection of an item i will use "grep" again to filter out the unique entry.

Maybe anyone can put me on the right direction?

Thanks.

Upvotes: 1

Views: 3758

Answers (1)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74800

I used dialog never before, but maybe I still can help. Try this:

declare -a args=()


while read
do
    args+=("$REPLY" "")
done < <( grep '@' example.txt )
dialog --menu "Please select the line you want to edit" 40 60 34 "${args[@]}"

How does this work?

dialog --menu takes the following arguments:

  • question text
  • height and width of the window
  • height of the menu (which should be 7 less then the window height to use it fully, in my experience)
  • pairs of tag string and description.

    The selected tag string is then output (on stderr) at the end.

How to create such a list strings from our grep output? A failed try is described below, here the working one.

The read command reads one line a time from standard input (to which we redirected the grep output), and puts it (if we don't give other options or arguments) in the REPLY variable. We then add this value (quoted to be one element) to the array args , and additionally a single "" to add an empty string to the array, too.

We have to use the < <( ... ) syntax for redirection, since the normal | creates a subshell for the second command, which has the effect that changes to the variables are not propagated back to the original shell. (< means read input from file, and <( ... ) creates a pipe to read the output of the command and results in its filename.)

Then we use the "${args[@]}" parameter expansion - @ has the effect that each element is individually quoted as the result. So for your example, the command line now looks like

dialog --menu "Please select the line you want to edit" 40 60 34 "Name Surname [email protected]" "" "Another New [email protected]" ""

This creates a two line menu, with the complete lines as the "tag", and an empty string as the clarification.

You will need some way to capture it's standard error output, as it puts the result there.


Another idea which does not work:

The question is how to to get the output of grep in the command line of dialog, so that it forms two arguments for each line.

What helps here are the following syntactic constructs:

  • Command substitution: $( cmd ) executes the command and converts the result to a string, which is then used at the point in the command line.

So, we need some command which produces two "words" for each line of grep output (since your file would give three words). As you are already using sed, why not use it here too? The sed command s/^.*$/"&" ""/ replaces each line with the line enclosed in "", followed by another two quotes.

"Name Surname [email protected]" ""
"Another New [email protected]" ""

The idea would now be to use

dialog --menu "Please select the line you want to edit" 40 60 34 $( sed -e 's/^.*$/"&" ""/' < input )

but unfortunately the word-splitting of bash does not respect "" after command-substitution, so bash gives the six arguments "Name, Surname, [email protected]", "", "Another, New, [email protected]" and "" to the dialog program. (In fact, using "" to inhibit splitting seems to work only for quotes given literal in the source or in eval - but eval does not work here since we have line breaks, too.)

Upvotes: 3

Related Questions