Search code examples
bashshelldmenu

using dmenu to define variables


i'm trying to write a simple script to cut a segment of an audio file, set it to an image and render it to a video. i want to use dmenu as a clean way of displaying my prompts for inputting variables. all works well except it seems dmenu expects something to be piped into it, otherwise the script doesn't work.

here is how i have the relevant part of the code currently working

INPUT=$(ls | dmenu -i -p "Input file:")
START=$(echo " " | dmenu -i -p "Start time:")
DURATION=$(echo " " | dmenu -i -p "Duration:")
IMAGE=$(ls | dmenu -i -p "Image file:")

i would like to remove the echo command for $START and $DURATION, both because it is ugly (it leaves an unneeded coloured box of space in dmenu) and because it seems redundant. however, without it the script fails on these lines.

does dmenu always expect something to be piped into it? perhaps i am doing something else completely wrong, this is only my 3rd ever shell script.


Solution

  • To get rid of colored spaces you can do this:

    DURATION=$(:| dmenu -i -p "Duration:")
    

    or

    DURATION=$(dmenu -i -p "Duration:"< <(:))
    

    : means true. So it's the same as writing:

    DURATION=$(true| dmenu -i -p "Duration:")
    DURATION=$(dmenu -i -p "Duration:"< <(true))
    

    But you can also use the -n flag to echo for the same resultat:

    DURATION=$(echo -n ''|dmenu -i -p "Duration:")
    DURATION=$(dmenu -i -p "Duration:"< <(echo -n ''))
    

    From the man page:

    ... which reads a list of newline-separated items from stdin

    So yes, dmenu need stuff to be piped to it.