I want to write a script that read the input to variable.
I want the input screen to have 2 lines, where the values already input will appear above the input line
for example if I want to input the words below:
like
love
live
life
The screen will display like this:
Before any input
Input words:
after entering
like
:
like
Input words:
after entering
life
:
like love live life
Input words:
I have tried the following but it keeps giving multiple lines, any ideas?
#!/usr/bin/ksh
n=2
p=""
while [[ $n != "x" ]]
do
echo "Input words: \c"
read n
p=$p" "$n
echo $p
done
You'll need man console_codes to position the cursor arbitrarily on the screen, but be aware that not all systems and terminal emulators accept these. Alternatively, try man termcap / terminfo .
You should prefer printf over echo , btw.
Assuming TERM is set in your environment to correctly indicate your terminal or terminal emulator type, the following should work:
#!/usr/bin/ksh
addword() {
tput clear
if [ "$input" != "" ]
then if [ "$output" != "" ]
then output="$output $input"
else output="$input"
fi
fi
printf '%s\nInput words (ctl-d when done): ' "$output"
read -r input
}
input=
output=
while addword
do :
done
echo
Although written and tested using ksh , this should work with any POSIX-conforming shell (e.g., ash , bash , dash , ksh , zsh , etc.).