LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-25-2020, 11:13 AM   #1
billlove
LQ Newbie
 
Registered: Jul 2020
Location: DC area
Posts: 2

Rep: Reputation: Disabled
Wanting to center text


This code will justify the text "Start Program" in the left center of the terminal window. I'd like it in the center. If anyone has any suggestions, much appreciated.

cols=$( tput cols )
rows=$( tput lines )

message=Start Program

input_length=${#message}

half_input_length=$(( $input_length / 2 ))

middle_row=$(( $rows / 2 ))
middle_col=$(( ($col /2 ) - half_input_length ))

tput clear

tput cup $middle_row $middle_col
tput bold

echo Start Program

tput sgr0
tput cup $( tput lines ) 0

Last edited by billlove; 07-25-2020 at 11:26 AM.
 
Old 07-25-2020, 11:38 AM   #2
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by billlove View Post
This code will left justify the text "Start Program" on the left side of the terminal window. I'd like it centered. If anyone has any suggestions, much appreciated.

cols=$( tput cols )
rows=$( tput lines )

message=Start Program

input_length=${#message}

half_input_length=$(( $input_length / 2 ))

middle_row=$(( $rows / 2 ))
middle_col=$(( ($col /2 ) - half_input_length ))

tput clear

tput cup $middle_row $middle_col
tput bold

echo Start Program

tput sgr0
tput cup $( tput lines ) 0
First thing, check for typos: (cols != col) and it's best to enclose strings in double quotes.

Second, You defined "$message" but didn't use it when displaying the message.

Third, I've always found it better to keep cursor positioning and the text to be displayed in the same print/echo statement. Personal preference, though:
Code:
echo "$( tput cup $middle_row $middle_col )$( tpu bold )$message"
HTH...

Last edited by rnturn; 07-25-2020 at 12:01 PM. Reason: Added note about typos
 
1 members found this post helpful.
Old 07-25-2020, 12:51 PM   #3
dmchess
Member
 
Registered: Jan 2005
Posts: 244

Rep: Reputation: 43
I think there is a standard unix program that will center text. I'm not home right now, but I have seen something like that in Unix Power Tools.
 
Old 07-26-2020, 06:12 AM   #4
billlove
LQ Newbie
 
Registered: Jul 2020
Location: DC area
Posts: 2

Original Poster
Rep: Reputation: Disabled
No help what so ever - not what I expected but not surprised - so long LinuxQuestions.org
 
Old 07-26-2020, 06:25 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by billlove View Post
No help what so ever - not what I expected but not surprised - so long LinuxQuestions.org
You weren't able to wait even 24h?
Well, then LQ is indeed the wrong place for you.

(My guess is OP doubleposted and received a handout elsewhere)
 
Old 07-26-2020, 09:49 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
@OP. With changes suggested by rnturn applied, your code works as intended:
Code:
#!/bin/sh
: ${LINES:=$(tput lines)} ${COLUMNS:=$(tput cols)}
msg='Start Program'
row=$((LINES/2)) col=$(( (COLUMNS-${#msg})/2 ))
tput -S <<!
clear
cup $row $col
bold
!
  echo "$msg"
tput sgr0
tput cup $LINES 0
@dmchess. No, the example in Unix Power Tools shows how to center each line in a file. It uses printf for this. Well, it is actually an awk script there that boils down to
Code:
awk -v c=$COLUMNS '{printf"%"int((c+length())/2)"s\n",$0}'
[() after length could be omitted in some versions of awk (gawk, mawk, bwk, Plan9 awk), but required in others (busybox awk)].

But the same could be done with shell arithmetic expansion as well:
Code:
printf '%*s\n' $(((COLUMNS+${#msg})/2)) "$msg"
 
1 members found this post helpful.
Old 07-26-2020, 12:09 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
^^Very cool!
 
Old 07-27-2020, 10:36 AM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by billlove View Post
No help what so ever - not what I expected but not surprised - so long LinuxQuestions.org
After fixing your glaring typo, your script worked on my system. What sort of help were you looking for?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wanting to trust "Paranoia Text Encryption", need to check it out more! steelheat Linux - Security 8 06-30-2016 12:54 AM
LXer: Ubuntu Command Center: Gnome Control Center LXer Syndicated Linux News 0 02-20-2012 08:31 PM
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
LXer: Linux Media Center Better Then Windows Media Center? LXer Syndicated Linux News 0 08-26-2007 09:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:38 AM.

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