I am trying to make a bash inputmenu
dialog
handle different types, such as, files, dates, regular text. Clicking the edit button, will simply send the user to the correct dialog to retrieve the input. For regular text, I simply want to use the rename feature of inputmenu
. I cannot have the user manually select rename because I only want the rename action to be used for text inputs. Having an identical looking dialog load up with the rename action automatically selected, would allow me to solve this problem.
I've tried to achieve this by changing the file descriptor and passing in
, \n
and \r
characters as inputs but with no luck.
#!/bin/bash
list=(aaa bbb ccc)
selected=1
function menu1()
{
count=0
declare -a items
for item in "${list[@]}"; do
items+=($((++count)) "$item")
done
echo -n '2 ' > tmp.txt
exec 4<tmp.txt
cmd=(dialog
--input-fd 4
--print-size
--print-maxsize
--extra-button --extra-label "Edit"
--default-button extra
--default-item "$selected"
--inputmenu "Select action:" 22 76 20)
exec 3>&1
#choices=$("${cmd[@]}" "${items[@]}" <<< ' ' 2>&1 1>&3)
choices=$("${cmd[@]}" "${items[@]}" 2>&1 1>&3)
retVal=$?
exec 3>&-
readarray -t choices <<< "${choices}"
choices="${choices[2]}"
echo "choices=$choices"
echo "retVal=$retVal"
menuAction "$retVal" "${choices[0]}"
}
function menu()
{
count=0
declare -a items
for item in "${list[@]}"; do
items+=($((++count)) "$item")
done
cmd=(dialog
--print-size
--print-maxsize
--extra-button --extra-label "Edit"
--default-button extra
--default-item "$selected"
--inputmenu "Select action:" 22 76 20)
exec 3>&1
choices=$("${cmd[@]}" "${items[@]}" 2>&1 1>&3)
retVal=$?
exec 3>&-
readarray -t choices <<< "${choices}"
choices="${choices[2]}"
echo "choices=$choices"
echo "retVal=$retVal"
menuAction "$retVal" "${choices[0]}"
}
function menuAction()
{
retVal="$1"
choice="$2"
declare -a choice="${choice[0]}"
if [[ "$retVal" -eq 3 ]]; then
choice=(${choice[0]})
if [[ "${choice[0]}" == "RENAMED" ]]; then
let selected=choice[1]
let index=choice[1]-1
unset choice[0]
unset choice[1]
list[$index]="${choice[@]}"
fi
fi
[[ "$retVal" -ne 1 ]] && menu
}
menu1
Edit
I've nearly got it working using expect
. Unfortunately, after expect has sent input to the dialog, it returns back to the terminal:
#!/bin/bash
/usr/bin/expect <<EOD
set timeout 30
spawn ./dialog1 >/dev/tty
sleep 1
send " "
expect eof
EOD
NB
I am fully aware that I am trying to circumvent dialogs
limitations and this is usually a bad thing, as it results in poorer code that's harder to maintain and may have unnecessary dependencies. This question should be considered more of a learning exercise. Practically, it's only worthwhile in exceptional circumstances or as an optional extra. I am creating an api maker and I will be giving the client the choice of optional enhancements, such as this, that require a hackish solution.