The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem with perl ali560045 Shell Programming and Scripting 6 05-29-2008 08:26 AM
Problem with "printf" Lokesha UNIX for Dummies Questions & Answers 1 02-07-2008 10:20 AM
A question of indentation Michael Iatrou High Level Programming 1 12-23-2006 05:29 PM
awk printf problem krishna UNIX for Advanced & Expert Users 6 11-18-2003 11:59 PM
disturbing problem with PRINTF() !! brain_processin High Level Programming 2 03-10-2002 09:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-27-2008
wolwy_pete wolwy_pete is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
Unhappy perl: printf indentation problem

hi all,
im having a problem with using perl printf. my requirement is to print a string (like [OK]) at the right most end of the screen.
i tried this perl script, but it fails with an error;

Code:
#!/usr/bin/perl 

use strict;
use warnings;

my $scrW = 0;

my $str = `stty size`;   # get the screen character width 
$str =~ m/(\d+)\s*$/;    # extract the char width
$scrW = eval($1);           # put the char width in a varable $scrW

$scrW -=10;                     # reduce the char width by 10
printf "%($scrW)s\n", "abcde";

tried to use printf with a variable as indentation length, but fails.
this type of method works in bash.
does anyone have an idea ?

thankx.
  #2 (permalink)  
Old 03-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You don't need the eval, just assign $1 to $scrW. And take out the parentheses in the printf. You then need braces around the variable name to disambiguate the variable name from the traliing s, like ${scrW}s

If you subtract ten, you obviously don't use the entire screen width, but I guess you know that.
  #3 (permalink)  
Old 03-27-2008
wolwy_pete wolwy_pete is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
Quote:
Originally Posted by era View Post
You don't need the eval, just assign $1 to $scrW. And take out the parentheses in the printf. You then need braces around the variable name to disambiguate the variable name from the traliing s, like ${scrW}s

If you subtract ten, you obviously don't use the entire screen width, but I guess you know that.
thankx era for the quick reply,
i subtracted ten thinking that the length of the string im gonna print is 10.
what u said worked , but i still had to use the eval.
anyway, thankx for the solution.
  #4 (permalink)  
Old 03-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
printf already does the subtraction for you; a positive number in %72s says pad with spaces as much as necessary to fill to the right up to, here, 72 characters width.
  #5 (permalink)  
Old 03-27-2008
buffoonix buffoonix is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 145
Believe us, the eval really is redundant

Code:
$ perl -e '($c)=qx(stty size)=~/\d+\s+(\d+)/;printf"%${c}s\n",$ARGV[0]' Hello\ World
                                                                          Hello World

  #6 (permalink)  
Old 03-27-2008
wolwy_pete wolwy_pete is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
sorry about my lack of understanding on how these functions work.
buffoonix`s method also works fine. thankx.
Closed Thread

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0