LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-30-2007, 01:53 PM   #1
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Rep: Reputation: 0
BASH : find max character per line in a console


Another little question with bash programming. I hope you could help me.

Ok, I have a script that display some info like this :

Executing command : ........................[ok]

I do this by using echo -n

Of course, I want to put my [ok] at the end of the current line
What I want to know is is there a way to know the maximun line lenght in the current console? Because if I work on a windowed console, I also want my [ok] string be displayed at the end.

Thanks
 
Old 08-30-2007, 01:56 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The COLUMNS variable holds the current line length.

Try the 'set' command in a terminal/console, it should be amongst the output.

Hope this helps.
 
Old 08-30-2007, 02:01 PM   #3
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
You are right thanks, columns give me the answer, still I have a little one for you :
It seems that the column lenght take in consideration the string at the begenning of the bash prompt, like :
[romain@cxsr test]$

Is there a way to have this string in a variable so I can calculate the real column lenght starting the point?

Thanks
 
Old 08-30-2007, 02:09 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This: [romain@cxsr test]$ is set by PS1 (Your 'primary' prompt), it's also among the output of the set command.

Hope this helps.
 
Old 08-30-2007, 02:12 PM   #5
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Yes but how can I retreive it in a bash script?
I have tested
Code:
t=$PS1
echo $t
but it show nothing ....
 
Old 08-31-2007, 05:35 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

These variables (COLUMNS PS1 etc) are not exported from the shell and thus not accessible in a script.

The width of the terminal can also be found by the tput command (tput cols, man tput for details).

This also means that you do not need to subtract the width of the prompt (which is not used when printing from a script).

Hope this helps.
 
Old 08-31-2007, 10:09 AM   #7
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks,
In fact I have managed to find a easier solution to display my [ok] at the end of the line.
First I have realized that when the script is executed, it's starting displaying lines at the very beginning of the console and not just after the user's prompt.
Then, instead of trying to figure out how to calculate the lengh, i have realize that whatever the console is resized or full screen, still there are 80 columns available.
So, I only have to create a line which at the end, display my [ok].
I have achieve this with this code :
Note that here $1 is a parameter of a function which accept a command as first parameter, the $2 is also a paramter in case the command $1 is piped with a second command $2

Code:
disp (){

	str="Executing :"
	str2=$1" "
	if ! [ "$2" = "" ]; then
		str2=$str2"| "$2" "
	fi
	str3=$str$str2
	strl=${#str3}
	let ll=72-$strl
	o=""
	for i in `seq 0 $ll`;
	do
		o=$o"."
	done    
	echo -n $str2$o
	if ! [ "$2" = "" ]; then
		res="$($1 | $2 2>&1)"
	else
		res="$($1 2>&1)"
	fi
	err=$?
	echo "$res" >> $OUTFILE
	if ! [ $err = 1 ]; then
		echo "   [OK]"
	else
		echo "[ERROR]"
		echo "La commande a retournée l'erreur suivante: "
		echo $res
	fi
}
- Basically, I build a first string that concatenate the 'header' then the command name
- Then for [ok] and [error] I know that I will need 7 characters but to make thing simplier, the string [ok] will have the same size as [error] (7 characters] by adding spaces to the [ok] string)
- Then a little calculation to know how many charaters there are between the lenght of the header minus the lengh of the result string minus the console columns lenght. It gives 72 because : 72 + 7 (lenght of the result code (" [OK]" or "[ERROR]")) -1 to avoid the carriage return
- Then I build a string with the header, the command, and some dot
- Then execute the command
- Display the result at the end

I can put the result code after the initial string using echo -n which does not do a carriage return.

The command are executed and their stderr code is evaluted to display the result string

An important thing is when you execute a command using $z=$(<command>)
use
Code:
$z="$(<command>)"
with quotes
and
Code:
echo "$z"
also with quotes
By this way, the resultat will have proper carriage return. If not, then the command will output a single string with no carriage return

Maybe it is not the best code for that but it is simple to use and fit my needs.
If you have a better approach then feel free to post your code or suggestions.
 
Old 08-31-2007, 11:05 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If you ever have to run this from a terminal, which can be smaller then 80 columns, you don't have to change that much.

This: termWidth="`tput cols`" will fill termWidth with the actual width of the terminal.
And this part: let ll=72-$strl, should be changed to: let ll=$termWidth-$strl-8.
Or both rolled into one: let ll=`tput cols`-$strl-8.
 
Old 09-04-2007, 12:43 PM   #9
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks druuna,
Now the script manage correctly the display of the [ok] whatever is the column length.

Perfect!
Thanks again.
 
Old 09-06-2007, 11:07 AM   #10
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
I have another little question for you and maybe you already have the answer.
What I want to achieve now is have a a text displayed on a line and have the ability to overwrite this line (that is, write on the same line) with a new text.
For ex:
If I want to display - \ | / on the same line to show that a script is currently working, like an animated kind of hourglass (sort of..)

Do you have any idea on how I can position the cursor to display a line of text?

Thanks
 
Old 09-06-2007, 11:39 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

There is more then one way to tackle this, depending on what you want to do.

tput can do what you want, but it could be to elaborate. If a rotating cursor is all you want, something like this should work (tput is not used):

Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
# Syntax  : spinning.cursor.sh
# -------------------------------------------------------------------------- #
#set -xv

# start the process that needs the spinning cursor
sleep 10 &
pid=$!



printf "Processing |"
rotate='|/-\'

while kill -n 0 $pid 2>/dev/null; do
    rotate="${rotate#?}${rotate%???}"
    printf '\b%.1s' "$rotate"
    sleep 1
done
echo ''

wait $pid

echo "All done"

exit 0

# -------------------------------------------------------------------------- #
# End
--- not originally written by me. I got this one from a scripting site, a long time ago ---

Here are 2 urls that deal specifically with tput:

Colours and Cursor Movement With tput
The Floating Clock Prompt

Hope this helps.
 
Old 09-06-2007, 12:17 PM   #12
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks,
In fact, basically, I only need a way to overwrite a line of text at the same position that the previous line was previously.
I know that tput could do this but I don't really understand how to use it ...
 
Old 09-06-2007, 12:30 PM   #13
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Well, maybe this simple test could do the job... what do you think?

Code:
#!/bin/bash
tput sc
echo -n "start"
for i in `seq 1 5000`;
do
        echo $i > \dev\null
done
tput el1
tput rc
echo "end"
 
Old 09-06-2007, 12:43 PM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Maybe this will help:

Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
# Syntax  : tput.example.sh
# -------------------------------------------------------------------------- #
#set -xv

# clear screen using tput
tput clear

# put cursor on certain position and echo text. Upperleft corner is 0 0
tput cup 10 10
echo -n "Counting down :"

# create a loop for the countdown
for i in `seq 9 -1 0`
do
  # put cursor on wanted position
  tput cup 10 26
  # print current counter value
  echo -n "$i"
  # have a break
  sleep 1
done

# done
exit 0

# -------------------------------------------------------------------------- #
# End
Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find max memory usage for process in Linux by command line kashameni Linux - Newbie 9 06-01-2007 01:39 AM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
[Bash] New line character in variable michael_hk Linux - Newbie 4 06-15-2006 02:13 PM
Console screen being displayed at max resolution Tino27 Linux - Laptop and Netbook 0 11-15-2004 02:50 PM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration