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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2007
Registered User
 

Join Date: Sep 2007
Posts: 2
Help with typeset in bash

Hi everybody, hoping you can help.

I'm trying to get some scripts working using bash which were written in ksh and I'm struggling with typeset. Specifically typeset -R and typeset -L. We need fixed length variables with left and right justification and bash does not seem to do it. Spent ages on Google and just seem to be getting nowhere

Thansk in advance
Sponsored Links
  #2 (permalink)  
Old 09-18-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 770
Hi.

For some simple characteristics, but requiring more work on the part of the author of the script:

Code:

#!/usr/bin/env ksh

# @(#) s1       Demonstrate similarities, ksh: typeset, bash: printf.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "ksh version = $KSH_VERSION"
bash --version | head -1
printf --version | head -1

echo

typeset -L10 L10="12345"
typeset -R10 R10="abcde"

echo " ksh with typeset:"
echo "L10 is $L10"
echo "R10 is $R10"

echo
echo " bash with printf:"
bash <<'EOF'
left="12345"
righ="abcde"

printf "left is %-10s\n" $left
printf "righ is %10s\n" $righ
EOF

exit 0

Producing:

Code:

% ./s1

ksh version = @(#)PD KSH v5.2.14 99/07/13.2
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
printf (GNU coreutils) 5.2.1

 ksh with typeset:
L10 is 12345
R10 is      abcde

 bash with printf:
left is 12345
righ is      abcde

cheers, drl
  #3 (permalink)  
Old 09-18-2007
porter porter is offline Forum Advisor  
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Randall R Schulz - Re: KSH to Bash conversion

Are there not open source versions of ksh?
  #4 (permalink)  
Old 09-18-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,148
Quote:
Originally Posted by porter View Post

Are there not open source versions of ksh?
A few vendor specific versions of ksh88 are still closed source. Other than that, open source versions are all there are. ksh93 became open source on March 1, 2000.
  #5 (permalink)  
Old 09-19-2007
Registered User
 

Join Date: Sep 2007
Posts: 2
If I could use ksh there would be little point me trying to convert them to bash unfortunately sysadmins being what they are bash is all I've got. I've managed to use "tr" to get round typeset -l and -u but I'm still stuck with -L and -R to left and right justify variables and give them a fixed length, printf is no use because I'm, not printing these variables, they are needed later on in the script.

Thanks again
  #6 (permalink)  
Old 09-19-2007
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,533
Quote:
Originally Posted by Ian_H View Post
I'm trying to get some scripts working using bash which were written in ksh and I'm struggling with typeset. Specifically typeset -R and typeset -L. We need fixed length variables with left and right justification and bash does not seem to do it. Spent ages on Google and just seem to be getting nowhere

Generally, you shouldn't use fixed width variables; rather you should specify the widths when you print them (using printf).

However, if you really do need them, you can still use printf:


Code:

var1=$( printf "%5s" "$value" )  ## Flush right
var2=$( printf "%-5s" "$value" ) ## Flush left

If you have bash3, you can use the much faster:


Code:

printf -v var1 "%5s" "$value"  ## Flush right
printf -v var2 "%-5s" "$value" ## Flush left


Sponsored Links
Closed Thread

Bookmarks

Tags
linux, linux commands

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Typeset bisla.yogender Shell Programming and Scripting 3 04-30-2008 07:45 AM
typeset in Ksh nervous UNIX for Dummies Questions & Answers 5 10-19-2006 03:48 PM
typeset -f ??? xinfinity Shell Programming and Scripting 1 04-04-2006 05:43 PM
TYPESET use sam71 UNIX for Dummies Questions & Answers 4 05-02-2005 01:16 PM



All times are GMT -4. The time now is 10:19 PM.


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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0