Visit The New, Modern Unix Linux Community


How to capture ^x,^y via bash script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to capture ^x,^y via bash script?
# 1  
How to capture ^x,^y via bash script?

Hi
I am new to this forum.
Any please help me to capture ctrl x and ctrl y via a bash script.

and please tell me how to clear the prompt via bash script

BR
Ramukumar M
# 2  
Not sure what you mean by "capture"...
Try to use the read builtin:
Code:
read -N1 VAR
 echo -n $VAR|hd
00000000  18                                                |.|

unless you need to get at those keys asynchronously...

Clear the prompt by unseting the PS1 environment variable.
This User Gave Thanks to RudiC For This Post:
# 3  
Quote:
Originally Posted by ramukumar
and please tell me how to clear the prompt via bash script
Easy: read the man page and search for "prompt".

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
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:
# 5  
Hi Perderabo
Thanks for the script.
Something similar only i need, I will go through the script and if i need any help to understand the script i will get back to you.
Thanks a lot for others Smilie

BR
Ramukumar M
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #49
Difficulty: Easy
A USB hard drive is considered to be a primary storage device for personal computers.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

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)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Capture run time of python script executed inside shell script

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)
Discussion started by: Adfire
2 Replies

3. Shell Programming and Scripting

Shell Script to Capture a Screenshot

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)
Discussion started by: ankur328
15 Replies

4. Shell Programming and Scripting

Capture a database val and use in script

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)
Discussion started by: mode09
1 Replies

5. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

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)
Discussion started by: Little
3 Replies

6. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

7. Shell Programming and Scripting

Script to capture errors

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)
Discussion started by: Katkota
2 Replies

8. UNIX for Advanced & Expert Users

How to capture STDOut of script in a CGI script?

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)
Discussion started by: kittu1979
1 Replies

9. Shell Programming and Scripting

script to capture certain output

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)
Discussion started by: fara_aris
18 Replies

10. Shell Programming and Scripting

script to capture

Hi In my production server some user runnig some scripts to get some data. I need a script to capture this user script code. best rds ab (5 Replies)
Discussion started by: aboorkuma
5 Replies

Featured Tech Videos