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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Appending to a variable? paqman Shell Programming and Scripting 0 08-14-2007 09:16 AM
appending spaces to a variable rallapalli Shell Programming and Scripting 2 08-12-2007 10:23 PM
Sed - Appending a line with a variable on it eltinator Shell Programming and Scripting 4 07-30-2007 06:15 AM
Appending data into a variable michaeltravisuk Shell Programming and Scripting 4 03-08-2007 08:45 PM
appending space in binary data in c arunkumar_mca High Level Programming 2 02-12-2007 06:30 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 14
appending space to variable

Hi
I need to write a script where there the user enters 3 input parameter
variable
number
the program should ask the user left or right
if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is right that many number of spaces should be added in the right side

for ex
variable = vivek
number =4
and when user echo $variable output should be
if user opts for right then output = (4 spaces in front)vivek
if user opts for left then output =vivek(4 spaces in back)
Reply With Quote
Forum Sponsor
  #2  
Old 02-04-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
sample snippet in perl

Code:
$indendSpace += 2;
$spaceVar = " " x $indendSpace;

$var =~ s/^/$spaceVar #at the start
$var =~ s/$/$spaceVar #at the end
Reply With Quote
  #3  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 14
I need it in korn shell

need it in korn shell
Reply With Quote
  #4  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 14
Thanks but need it in korn shell
Reply With Quote
  #5  
Old 02-04-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
Code snippets in ksh-93

Code:
$pad="    "
$var="vivek"
$ newvar=$pad$var
$ echo $newvar
vivek
$ echo ${#newvar}
9
$ echo "$newvar"
    vivek
$ L=9
$ new=$(printf "%${L}s" "$var" )
$ echo "$new"
    vivek
$
Reply With Quote
  #6  
Old 02-04-2008
Registered User
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Code:
#!/bin/ksh

echo "Input variable: \c" ; read var
echo "Input number: \c" ; read pad
echo "Input Left or Right padding (L or R): \c" ; read lr

lr=$(echo $lr | tr '[a-z]' '[A-Z]')

if [ "$lr" != "R" ] && [ "$lr" != "L" ]
then
        echo Invalid padding $lr !
        exit
fi

while [ $pad != 0 ]
do
        if [ "$lr" = "R" ]
        then
                var=$var" "
                let pad=$pad-1
        else
                var=" "$var
                let pad=$pad-1
        fi
done

echo "[$var]"
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0