I am working on new kickstart file for RedHat and I need to prompt the user for stuff like ip addr, netmask, gateway, etc... I wanted to provide some editing capablility so the user can correct errors easily. I stayed with shell builtins because I am not sure how much OS is guarranteed to be present at kickstart time. This is really a work in progress, but it is at least close right now. I am capturing each keystroke so I can process cntl-p, cntp-b, and so forth. I think this is enough to show the OP how to do this sort of thing.
Code:
#! /bin/bash
function InputWithEdit
{
local cntla cntlb cntld cntle cntlf cntlh cntlj cntlk cntlm cntln cntlp
local line CH GO move col row datapoint data saveline
local backwardsD forewardsD piece1 piece2
cntla=$(echo -e '\001\c')
cntlb=$(echo -e '\002\c')
cntld=$(echo -e '\004\c')
cntle=$(echo -e '\005\c')
cntlf=$(echo -e '\006\c')
cntlh=$(echo -e '\010\c')
cntlj=$(echo -e '\012\c')
cntlk=$(echo -e '\013\c')
cntlm=$(echo -e '\015\c')
cntln=$(echo -e '\016\c')
cntlp=$(echo -e '\020\c')
esc=$(echo -e '\e\c')
#
# Display Field Prompts
echo -e '\e[H\e[2J\c'
echo $Heading
echo
for ((line=0; line < ${#prompts[*]} ; line++ )) ; do
((row=line+3))
echo -e "${prompts[line]}:${values[line]}"
done
echo -e '\e[5;10H\c'
#
# Display Help
echo
echo
echo
echo
echo
echo '*******************************************'
echo '* Editing Help *'
echo '*******************************************'
echo '* Control-a = beginning of line *'
echo '* Control-e = end of line *'
echo '* Control-n = next line *'
echo '* Return or Tab = next line *'
echo '* Control-b = move backwards in line *'
echo '* Control-f = move forewards in line *'
echo '* Control-p = previous line *'
echo '* Control-d = delete forward *'
echo '* Control-h = delete backwards *'
echo '* Control-k = kill line *'
echo '* < = top line *'
echo '* > = bottom line *'
echo '* Esc = done *'
echo '*******************************************'
#
#
# Loop to Read Data From User
GO=1
line=0
move=1
while ((GO)) ; do
#
# This routine handles the arrival on a new data field
if ((move)) ; then
move=0
((col=${#prompts[line]} + ${#values[line]}+ 2))
datapoint=${#values[line]}
data=${values[line]}
saveline=$line
((row=line+3))
fi
#
# Read a character. In some case the case statement can process the
# character directly
backwardsD=0
forewardsD=0
gotdata=0
echo -e '\e['${row}';'${col}'H\c'
read -s -n 1 CH
case $CH in
${cntla}) datapoint=0; ((col=${#prompts[line]} + 2)) ;;
${cntlb})
if ((datapoint>0)) ; then
((datapoint--)); ((col--))
fi
;;
${cntlf})
if ((datapoint < ${#data})) ; then
((datapoint++)); ((col++))
fi
;;
${cntld}) backwardsD=1 ;;
${cntle}) move=1;;
${cntlh}) forewardsD=1 ;;
${cntlk})
echo -e '\e[K\c'
data=${data:0:datapoint}
;;
${cntln}) ((line++)) ; move=1;;
${cntlj}) ((line++)) ; move=1;;
${cntlp}) ((line--)) ; move=1;;
'<') line=0 ; move=1;;
'>') ((line=${#prompts[*]}-1)) ; move=1;;
${esc}) move=1 ; GO=0 ;;
*) gotdata=1;;
esac
#
# Here is the Backwards Delete routine
if (( backwardsD && ${#data} )) ; then
if ((datapoint == 0)) ; then
data=${data:1:${#data}-1}
echo -e '\e[K\c'
echo -e "${data}"
else
piece1=${data:0:datapoint}
piece2=${data:datapoint+1:${#data}-datapoint}
data=${piece1}${piece2}
echo -e '\e[K\c'
echo -e "${piece2}"
fi
fi
#
# Here is the Forewards Delete routine
if (( forewardsD && ${#data} && datapoint)) ; then
if ((datapoint == ${#data})) ; then
data=${data:0:${#data}-2}
((datapoint--))
((col--))
echo -e '\e['${row}';'${col}'H\c'
echo -e '\e[K\c'
else
piece1=${data:0:datapoint-1}
piece2=${data:datapoint:${#data}-datapoint+1}
data=${piece1}${piece2}
((datapoint--))
((col--))
echo -e '\e['${row}';'${col}'H\c'
echo -e '\e[K\c'
echo -e "${piece2}"
fi
fi
#
# This is the insert data routine. It handles every data key-press
if ((gotdata)) ; then
if ((datapoint == ${#data})) ; then
data=${data}${CH}
echo -e "${CH}\c"
elif ((datapoint == 0)) ; then
data=${CH}${data}
echo -e '\e['${row}';'${col}'H\c'
echo -e "${data}"
else
piece1=${data:0:datapoint}
piece2=${data:datapoint:${#data}-datapoint}
data=${piece1}${CH}${piece2}
echo -e '\e['${row}';'${col}'H\c'
echo -e "${CH}${piece2}"
fi
((datapoint++))
((col++))
fi
#
# Prepare for move (of cursor to another data field))
if ((move)) ; then
values[saveline]="$data"
if ((line >= ${#prompts[*]})) ; then
((line=${#prompts[*]}-1))
echo
echo
echo "Press Esc when you are finished entering the data"
fi
((line < 0)) && ((line=0))
fi
done
#
# Clear Off The Screen
((row=${#prompts[*]}+4))
col=1
row=1
echo -e '\e['${row}';'${col}'H\e[J\c'
return 0
}
#
# Set up the Data
HOSTNAME="testbox"
Heading="Enter the configuration data for $HOSTNAME"
declare -a prompts=("IP Address" "Subnet Mask" "Gateway" "Realm")
declare -a values=("" "" "" "")
loopy=1
while ((loopy)) ; do
InputWithEdit
echo
echo Final Values:
for ((line=0; line < ${#prompts[*]} ; line++ )) ; do
echo "${prompts[line]} = ${values[line]}"
done
echo
echo -e "Is this all Correct? \c"
read ans
if [[ $ans == Y* || $ans == y* ]] ; then
loopy=0
fi
done
IPADDR=${values[0]}
NETMASK=${values[1]}
GATEWAY=${values[2]}
REALM=${values[3]}
echo IPADDR= $IPADDR
echo NETMASK=$NETMASK
echo GATEWAY = $GATEWAY
echo REALM = $REALM
exit 0
These 3 Users Gave Thanks to Perderabo For This Post:
Hi All,
I want to create a script that capture only Date & Time, Current CPU % usage, Disk % usage, Mem % usage and Top process based on this output;
Data Collected: 05/17/08 17:19:49
Refresh Interval: 600 seconds
GlancePlus Started/Reset: 05/17/08 08:19:45
B3692A GlancePlus... (18 Replies)
Hi Perl Experts,
I am invoking a shell script thru a perl script and the perl script is cgi script.I need to capture the STDOUT of the shell script in the html page where I am invoking the script .?The shell script takes couple of mintutes to complete its execution .Mean while my html page does... (1 Reply)
Hello;
I'm trying to write a script to capture any hardware error from logs/syslog on my SUSE 10 servers so i can be notified if we have any hardware issues such a bad fan or battery, etc..
Thanks in advance for any help (2 Replies)
hi,
i want to pop up an alert box using perl script. my requirement is.
i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Hello, sorry if this has been asked before, I couldn't find what I was looking for.
I know how to connect to Oracle and execute stored procedures from a shell script, but what I would like to do is return a value from a table and use it in my script.
For Example, If I had a table Called... (1 Reply)
Hi All,
Suppose I want to take a screenshot of a website say Google and save that image. How should I do it?
I tried wget with this but of no help. It just makes a particular file in jpeg format but on opening the same it says corrupted. Although I can edit the jpeg as an HTML file.
wget... (15 Replies)
I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
I'm new to utilities like socat and netcat and I'm not clear if they will do what I need.
I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes.
I'm using Cygwin bash on Windows 10.
My... (3 Replies)