0

I want to learn shell-scripting. I want to create simple tool to show file content with edit option but I can't get value from dialog --editbox. Anybody can explain me how it works?

My code:

#!/bin/bash
BACKTITLE="Some backtitle"
FILENAME="filename.txt"

touch $FILENAME

INPUT=/tmp/menu.sh.$$

ret=0

while [ $ret -eq 0 ]
do
    dialog --title "Menu" \
        --backtitle "$BACKTITLE"  \
        --menu "Wybierz" 10 60 3 \
        1 "Pokaz menu" \
        2 "Edytuj" \
        2>"${INPUT}"

    ret=$?
    option=$(<"${INPUT}")

    if [ $ret -eq 0 ]
    then
        if [ $option -eq 1 ]
        then
            dialog --title "File content" \
                --backtitle "$BACKTITLE" \
                --textbox $FILENAME 10 60
        elif [ $option -eq 2 ]
        then
            dialog --title "Edit file content" \
                --backtitle "$BACKTITLE" \
                --editbox $FILENAME 10 60

            editboxret=$?
            echo $editboxret
            ret=0
        fi
    fi
done
ventaquil
  • 2,780
  • 3
  • 23
  • 48

2 Answers2

1

dialog writes the 'edited' content to STDERR you need to make sure it ends up in the original file again.

# Write the output of dialog to a temp-file
dialog --editbox $FILENAME 10 60 2> "${INPUT}"

# ADVISED: Show the user the temporary file-content
# and ask for confirmation before doing the next step:

# Overwrite the input-file
cp ${INPUT} $FILENAME
Stephan Arts
  • 155
  • 9
1

Per the manpage (man dialog), the output is written to stderr. Using the suggestion in https://stackoverflow.com/a/6317938/5528982, you can use

{ newcontents=$(dialog --title "Edit file content" -- backtitle "$BACKTITLE" --editbox $FILENAME 10 60 2>&1 1>&$out); } {out}>&1
Community
  • 1
  • 1
walter
  • 518
  • 3
  • 8