Hi,
I Want a shell script, when i run it should print the current row & column number of the terminal in which is invoked , so how can i get the current row & column number in the linux .

Thanks
Srikanth M.

Recommended Answers

All 6 Replies

[EDIT]
Sorry, forgot this was not the C forum. See below if you'd also like the C implementation.

Bash script for lines and columns:

#!/bin/bash

echo "cols: `tput cols`"
echo "lines: `tput lines`"

[/EDIT]


The following will print out the number of rows/columns but I'm not sure of how to get the exact cursor position...

#include <stdio.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

int main () {
    struct winsize ws;
    ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws);
    printf ("Rows: %d, Cols: %d\n", ws.ws_row, ws.ws_col);
    return 0;
}

Hi L7Sqr,
Thanks for your reply , but i want current cursor position not number of rows and columns of the terminal

Thanks
Srikanth M.

[EDIT]
Sorry, forgot this was not the C forum. See below if you'd also like the C implementation.

Bash script for lines and columns:

#!/bin/bash

echo "cols: `tput cols`"
echo "lines: `tput lines`"

[/EDIT]


The following will print out the number of rows/columns but I'm not sure of how to get the exact cursor position...

#include <stdio.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

int main () {
    struct winsize ws;
    ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws);
    printf ("Rows: %d, Cols: %d\n", ws.ws_row, ws.ws_col);
    return 0;
}

Here are two suggestions I found on stackoverflow:
Method one (zero-based):

#!/bin/bash

oldstty=$(stty -g)
stty raw -echo min 0
echo -en "\033[6n" > /dev/tty
IFS=';' read -r -d R -a pos
stty $oldstty
# change from one-based to zero based so they work with: tput cup $row $col
row=$((${pos[0]:2} - 1))    # strip off the esc-[
col=$((${pos[1]} - 1))
echo "Row: ${row}"
echo "Col: ${col}"

Method two (one-based):

echo -en "\E[6n"
read -sdR CURPOS
CURPOS=${CURPOS#*[}
echo
echo "${CURPOS}"

Hi L7Sqr,
Thanks for the reply, can u please provide me the same in C-Shell not in bash.

Thanks
Srikanth M.

Here are two suggestions I found on stackoverflow:
Method one (zero-based):

#!/bin/bash

oldstty=$(stty -g)
stty raw -echo min 0
echo -en "\033[6n" > /dev/tty
IFS=';' read -r -d R -a pos
stty $oldstty
# change from one-based to zero based so they work with: tput cup $row $col
row=$((${pos[0]:2} - 1))    # strip off the esc-[
col=$((${pos[1]} - 1))
echo "Row: ${row}"
echo "Col: ${col}"

Method two (one-based):

echo -en "\E[6n"
read -sdR CURPOS
CURPOS=${CURPOS#*[}
echo
echo "${CURPOS}"

Sorry, I am not familiar with C-shell. There is enough there that you should be able to do this on your own. Or, at a minimum just call bash from within you C-shell.

Hi L7Sqr,
Thanks for the reply, i will try to call bash in my script

Thanks
Srikanth M.

Sorry, I am not familiar with C-shell. There is enough there that you should be able to do this on your own. Or, at a minimum just call bash from within you C-shell.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.