- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: typeset usage
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 11:22 PM
тАО02-01-2007 11:22 PM
typeset usage
I am working on code porting from tru64 to hp-ux.
I want to know what would i benefit if i use a typeset to variable before i use it.
or rather when should we use variables after doing typeset for it and when to use it without doing a typeset.
Also does it have different impact on different unix systems.
Thanks and Regards,
Sangeeta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 11:28 PM
тАО02-01-2007 11:28 PM
Re: typeset usage
I would always be careful useing typeset variables and have not found a definite need for them, as yet.
Example:
# typeset -i a
# a=0.4
# echo $a
0
# b=0.4
# echo $b
0.4
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 11:44 PM
тАО02-01-2007 11:44 PM
Re: typeset usage
Using 'typeset' for your variables allows you to do:
1. Faster arithmetic (if declared a s an integer).
2. To perform automatic upper/lower case translations when you store into the variable, eliminating testing for "Y" or "y" or "N" or "n", for example.
3. To right or left justify and fill will blanks or zeros.
4. Declare read-only variables so that you can logically treat them as constants.
5. Create local variables within subroutines.
These implementions are common among the POSIX, ksh, and bash shells. Obviously, there is great benefit to using 'typeset'.
If you code in a C or Perl, 'typeset' feels like a "natural" thing to do.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2007 01:47 AM
тАО02-02-2007 01:47 AM
Re: typeset usage
probably the biggest asset of typesetting shell variables is that it lets you scope variables to avoid inadvertent name clashes like in proper programming languages (even untyped ones like Perl, otherwise no CPAN was possible at all).
You may already have noticed these neat alii that HP compiled into their sh:
$ alias autoload functions integer local
autoload='typeset -fu'
functions='typeset -f'
integer='typeset -i'
local=typeset
So you could use typeset (or the aliased local) for local scoping in function definitions like this:
$ scope() { var=blah;echo $var; };scope;echo ${var:-unset};unset -f scope;unset var
blah
blah
$ scope() { typeset var=blah;echo $var; };scope;echo ${var:-unset};unset -f scope;unset var
blah
unset
You can also temporarily export variables
whithout much ado
$ var=blah printenv var;printenv var;printenv var;unset var
blah
I like the auto case conversion already mentioned by James.
$ typeset -u uc_pwd=$PWD; echo $uc_pwd
/ETC/RC.CONFIG.D
likewise in lower case
$ typeset -l lc_pwd=$uc_pwd;echo $lc_pwd
/etc/rc.config.d
Integer arithmetics are accomplished without fussy bracing etc.
$ typeset -i i=0;while ((i<=10));do a[i+=1]="$((10-i))\n";done;echo ${a[*]}
10
9
8
7
6
5
4
3
2
1
0
You can also declare read only variables which may act similar to constants.
$ typeset -r const="won't change";const="take this"
const: This variable is read only.
Enough reasons I would think to more often make use of typeset.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2007 02:31 AM
тАО02-02-2007 02:31 AM
Re: typeset usage
typeset -i8 OCTALVAR=1
so that a base/radix can be declared rather than the default base10.
typeset -i VAR=1 works everywhere (Korn, POSIX, bash, zsh) but typeset -i10 works in some implementations but not others. I would avoid integer declaration with a base specification although that can be an extremely useful construct in some cases.
In any event, typeset should be used for exactly the same reasons that strongly typed languages are favored. If you use them nowhere else, at least use them within functions so that you can use local variables without fear of side-effects.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2007 05:03 PM
тАО02-02-2007 05:03 PM
Re: typeset usage
typeset -i8 OCTALVAR=1
so that a base/radix can be declared rather than the default base10.
Yes, we allow that. Also if you want to convert from hex:
typeset -i10 fromhex=16#dead; echo $fromhex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 04:59 AM
тАО02-05-2007 04:59 AM
Re: typeset usage
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 01:02 PM
тАО02-05-2007 01:02 PM
Re: typeset usage
Yes, I understood that but wanted to mention what use you could do with bases.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-06-2007 09:37 PM
тАО02-06-2007 09:37 PM
Re: typeset usage
you have had quite a few answers here.
If this problem is resolved, could you please complete the thread by awarding points to helpful answers and summarising the solution for you.
This will help resolution of similar problems in the future.