Anonymous Anonymous - 1 year ago 208
Bash Question

Save file content from editbox in BASH

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

Answer Source

Per the manpage (man dialog), the output is written to stderr. Using the suggestion in http://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