Help with typeset in bash


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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
# 2  
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  
Randall R Schulz - Re: KSH to Bash conversion

Are there not open source versions of ksh?
# 4  
Quote:
Originally Posted by porter

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  
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  
Quote:
Originally Posted by Ian_H
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


Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #531
Difficulty: Easy
Unicode is not backward compatible, so it does not work with plain ASCII characters.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why use typeset?

Hi, All the scripts we have here use typeset instead of normal variables. They don't have any parameters, they just get declared at the beginning of the scripts like this: typeset var1 var2 var3Could anyone tell me why this is done? I don't see the advantage in this over using normal variables. (1 Reply)
Discussion started by: Subbeh
1 Replies

2. Shell Programming and Scripting

converting ksh to bash - typeset commands

Hi all, Am trying to convert a script written in ksh to a bash shell. At the moment, am stumped with the typeset -u command and I can't find an equivalent of it in bash. integer function is also not working as is the following if statement if ] && ]; then continue fi Is... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

Typeset conversion problem from ksh to bash

Hi, typeset -l sgf # all lowercase letters typeset -u SGF # all uppercase letters sgf=$1 SGF=$sgf these lines used in my scripts . It ran fine in ksh but when we convert this to bash it erroring out. I like to know what the use of typeset ?? Thanks & Regards kanagaraj (3 Replies)
Discussion started by: kanagaraj
3 Replies

4. UNIX for Dummies Questions & Answers

Typeset

Hi, Can any one please explain me the use of 'typeset' in shell scripting? I donot under stand the use and advantages of using typeset. In one of our script, it is written like typeset VERBOSE NO_UPDATE typeset LOAD_SYBASE_TABLES I donot understand what actually these lines do. As per my... (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

5. Shell Programming and Scripting

bash typeset padding with zeros

Hi everybody, I have a question about typesetting. I originally wrote a script for use with ksh and now I am on a system that I cannot modify, and it only has bash. In the original script I just did typeset -RZ4 variable and it would add the leading zeros. In bash, it doesn't work. I've... (2 Replies)
Discussion started by: jwheeler
2 Replies

6. UNIX and Linux Applications

typeset-r

Hi All , Can any one help me the meaning of typeset -r Thanks, venkat (1 Reply)
Discussion started by: venkatakotiy
1 Replies

7. Shell Programming and Scripting

Typeset

Hi, Could any one please explain about typeset or share any link from where i can study about typeset i.e how to use it, how to define it.. etc? Thanks- Yogi (3 Replies)
Discussion started by: bisla.yogender
3 Replies

8. Shell Programming and Scripting

typeset

Can anyone show me a simple practical usage of typeset. (1 Reply)
Discussion started by: balaji_prk
1 Replies

9. Shell Programming and Scripting

typeset -f ???

I have found this command *typeset* and the option * -f *, which should provide me the list of all the currently defined functions. Is there any possibility of specifying the file in which this command to search ? (1 Reply)
Discussion started by: xinfinity
1 Replies

10. UNIX for Dummies Questions & Answers

TYPESET use

Hi all, I have problem understanding shell script.Written that $bindir/put_load.ksh ; typeset RV= $? I dont have any other document about script. How can i find that $bindir is exist or not what is the content of that, i am working on new box . how can i search that in old box what... (4 Replies)
Discussion started by: sam71
4 Replies

Featured Tech Videos