Hi,
Is there a way to get the current cursor position in a bash script? I know that the positon can be saved and restored, but did not find a way to red the row and column values of where the cursor is.
Hi,
Is there a way to get the current cursor position in a bash script? I know that the positon can be saved and restored, but did not find a way to red the row and column values of where the cursor is.
Don’t know if this helps you, it does not ‘get’ it, but ‘sets’ it:
tput cup 10 20
would move the cursor to row 10, column 20
‘man tput’ will give you more options.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Scripts do not have cursors. They have lines and positions, but
usually those are clear when an error happens.
What is the point of this exercise? Wanting to know where something
breaks? You can get a lot of debugging data by loading the script
(assuming you mean bash since you don’t say otherwise) by running it as
follows:
bash -x /path/to/your/script.sh
If you mess up something completely, even without the ‘-x’ option, you
still get a line number for your failure:
…/test.sh: line 4: fakeCommand: command not found
If you are using a text editor in a file then the text editor may tell you
where you are, but that’s not what you asked and it would vary per editor
so more data are needed.
Good luck.
On 03/02/2010 05:56 AM, inp3dance wrote:
>
> Hi,
>
> Is there a way to get the current cursor position in a bash script? I
> know that the positon can be saved and restored, but did not find a way
> to red the row and column values of where the cursor is.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJLjQ15AAoJEF+XTK08PnB5REoQAKuwqMYihlipN2vMDhp8dgfr
qY4hLonrgz1svudTlHxpoLDzzMCNZfmV74B630IOHffSl1hQW1xTgZiSmQu6A2PB
jIdvVgOzxIxt4f2fQAPb3PL+HaDWjz0p7IiA66Tf8V4MxSvs4JrfkZiPaN267Rk2
guAvjGa+9opXvF954sISYNKzXTDXu1W9pdpIBbj2SNi9eb1fl/e1xl3LnvRpg2et
rBwTfYgnGnwqL4Z00PmgUPhnhghTeZu/FENhWJ2GHpaM8VKFPTYYfFoXKT8rPjwO
nie5LYOSO8S40LvFDwg1S9uiMtmCWM8y9NlSZV6cztfwsKND/iFRbKkKSvNvG5N7
G3OkGJv858cgwD1qQxBlMj/0gw+qfk0drzazEHIMRYQflFHAPh1Olb0q+IxZvAr4
LeCt92/5s7xIHEZ2YnC4WJotAnUiqo1vYe3oNkRNO4gi73ZHVj2WineAVMKzC1Nc
XVGKTm4dYsS1RqT10DBaKNY0DmfwBR/t8N55jTmrsXCRGyeAIUJny3exX30fWSAd
wd2VnwxE5DYCTlDU64LZgTuhCuMuMFOlf4EmmUCp5piwXW9XN7950Ie3bjCFiJv5
w13oAZLEKBZ/Fh459LsHgxvR26wC7hr9p8aO9fHMxPjPlMYDXfhmKHIYb5GtFRG3
o2WeJuoITBIxSzC+0EZJ
=CqDJ
-----END PGP SIGNATURE-----
Must disagree, see example:
tput cup 100 100; read TXT; tput cup 100 101; echo $TXT
This will put the cursor at row 100, column 100, read input, and echo input one line beneath.
The command was explained to me in the late eighties as meaning ‘terminal put cursor position rownumber columnnumber’. I’ve used these kind of scripts for ages.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Yes, true… I misunderstood the question, or understood it based only on
text and not on what was desired. This does not put the cursor anywhere
in the script, but rather puts the cursor somewhere within the terminal
based on code within the script. Anyway, I imagine your interpretation of
the question was correct and mine was not.
Good luck.
On 03/02/2010 06:46 AM, Knurpht wrote:
>
> ab@novell.com;2129345 Wrote:
>> Scripts do not have cursors. They have lines and positions, but
>> usually those are clear when an error happens.
>>
>>
>
> Must disagree, see example:
>
>
> Code:
> --------------------
>
> tput cup 100 100; read TXT; tput cup 100 101; echo $TXT
>
> --------------------
>
>
>
> This will put the cursor at row 100, column 100, read input, and echo
> input one line beneath.
>
> The command was explained to me in the late eighties as meaning
> ‘terminal put cursor position rownumber columnnumber’. I’ve used these
> kind of scripts for ages.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJLjSPrAAoJEF+XTK08PnB5JpIQAMUZsRoN1Lx8y06yAVgR4i30
E2E70qDcuTQQHwllaFeiDHtJz0kO0zuLIjR9ayqQTjRmnnD9Wn479aiIMrSRUNN9
GwDSv7REa1lRceybWaLLJG15m7OZ0v6F/QG2wbhXPVvG2Pag4MjxJy2gXEbZhnE2
MnqhYh3ZetCydknDbCVhwHr8ZXcvrGjhrGmMQTIIjerzHi6GxCBjpq3THbeO0Upp
6B5YdCYjHAtD1lyQSdLDCl+JekS4SjeBDYEZM9JSQpO2uK800QqHs1bGBRn15rjD
9n0uCBG2TLTnL8qnz5qf3thIlrjdolKHjYDxsVvY630ncctY5a1SM3U1+nXf771m
b5NaK7EgN2N6KrRDclza5lJr9TPXkGwYDOzsuudbg0ny7gIdD89U+GoJbL6NCLqz
d93gEmiwsc1Zb7Ti7NXHu5c/pmDj1mvjrHV/avFjRuzWtmQPmU6AsvUoYBlQCVJ8
sSi8ULRJvReUFf6wgJ4c6HaJ146tiAQqBBC48SfzFxbgG3PzQT1IxcDZy8cqiykd
/rbvq+HE6+lxo+Hirb54yLwcLW1gBmDj/rSHVGYMs/qvXUTtEWQw9pqCtldSHPN9
vNWEVsikPIKGd/YFK3oKNuu00TjQ3aRGk3o4ttz5ULYZ/7ln/Wg5i5sLWOnwakqD
mPredMMUurF9rZ2/QFmH
=Ar7C
-----END PGP SIGNATURE-----
I needed to get the current row and column value because I was working on some kind of progress bar in bash. But resolved other way, if somebody need to inspire, here it is ( critique is welcome too ):
#!/bin/bash
function progress_bar_init
{
# get the number of columnss
cols=$(tput cols)
#number of columns in the progress bar_cols
# will be like this: ======== ] xxx%
bar_cols=$$cols-8]
max_bar_cols=100
if $bar_cols -gt $max_bar_cols ]; then
bar_cols=$max_bar_cols
fi
#draw the empty progress bar
echo -n ""
tput cuf $bar_cols
echo -n "] 0%"
current_position=0
characters=1
}
function progress_bar
{
p=$1
#calculate the position to go in the progress_bar
position=$$bar_cols*$p/100]
if "$position" -gt "$bar_cols" ]; then
position=$bar_cols
fi
#go to the current position
tput cub $$bar_cols+3+$characters-$current_position]
#full up from current position to new position
for ((f=0;f<$$position-$current_position];f+=1)); do
echo -n "="
done
#update the current position
current_position=$position
#write the precentage value at the end
tput cuf $$bar_cols-$position+2]
echo -n $p"%"
if "$p" -gt "9" ]; then
characters=2
fi
if "$p" -gt "99" ]; then
characters=3
fi
}
progress_bar_init
for ((i=0;i<=120;i+=3)); do
progress_bar $i
sleep 0.5
done
And yes, the question was not defined right. What I was asking is to get the cursor position on the terminal, not in the editor, my mistake, sorry.
Pixel graphics or character cell graphics? If the latter, have a look at ncurses.
ANSI sequences: when you send **<ESC>[6n[/b] to the screen it returns an escape sequence with the cursor position: <ESC>{ROW};{COLUMN}R
See: ANSI Standard (X3.64) Control Sequences for Video Terminals and Peripherals**
Thank you vodoo, just another one question: if I do echo -e “\0336n”, how can I read the response for that?