- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: scrit xyz -a cat -b dog -c mouse, what is $1, ...
Operating System - HP-UX
1822827
Members
4564
Online
109645
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО12-02-2004 08:22 AM
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2004 08:33 AM
тАО12-02-2004 08:33 AM
Re: scrit xyz -a cat -b dog -c mouse, what is $1, $2, $3, and $#?
# cat xyz
#!/usr/bin/sh
echo $1 $2 $3 $#
# ./xyz -a cat -b dog -c mouse
-a cat -b 6
#
#!/usr/bin/sh
echo $1 $2 $3 $#
# ./xyz -a cat -b dog -c mouse
-a cat -b 6
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2004 09:11 AM
тАО12-02-2004 09:11 AM
Solution
In scripting, very similar to coding arguments have specific identifiers.
In ksh, sh, bash, bash2, pdksh, etc...
$? - Return code of previous program
?# - Number of arguments passed to the script
$0 - name of script executing
$1 - First argument passed to the program
$2 - Second argument passed to the program
$22 - Twenty second argument passed to the program
If you are writing a program, use perl, etc.. you may want to utilize getopt where it is available. Quite handy.
Regards,
Shannon
In ksh, sh, bash, bash2, pdksh, etc...
$? - Return code of previous program
?# - Number of arguments passed to the script
$0 - name of script executing
$1 - First argument passed to the program
$2 - Second argument passed to the program
$22 - Twenty second argument passed to the program
If you are writing a program, use perl, etc.. you may want to utilize getopt where it is available. Quite handy.
Regards,
Shannon
Microsoft. When do you want a virus today?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2004 11:06 AM
тАО12-02-2004 11:06 AM
Re: scrit xyz -a cat -b dog -c mouse, what is $1, $2, $3, and $#?
getopt is also available for shell. A bit more complex than $1 but it manages "-a", "-b", etc for you.
regards,
Fred
regards,
Fred
--
"Reality is just a point of view." (P. K. D.)
"Reality is just a point of view." (P. K. D.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2004 11:32 AM
тАО12-02-2004 11:32 AM
Re: scrit xyz -a cat -b dog -c mouse, what is $1, $2, $3, and $#?
To answer your specific quest:
${1} = xyz
${2} = -a
${3} = -b
${#} = 7
You seem to want to know how to parse this. Man getopts (and their is also a getopt commnd) for details. Typically, you use getopts to extract all the options and then
use do a shift $((${OPTIND} - 1)) so that any remaining command line arguments without -a, -b, ... are now $1, $2, $3 ...
The getopts command sets ${OPTIND} for you; getopts is a better and more powerful version of getopt.
${1} = xyz
${2} = -a
${3} = -b
${#} = 7
You seem to want to know how to parse this. Man getopts (and their is also a getopt commnd) for details. Typically, you use getopts to extract all the options and then
use do a shift $((${OPTIND} - 1)) so that any remaining command line arguments without -a, -b, ... are now $1, $2, $3 ...
The getopts command sets ${OPTIND} for you; getopts is a better and more powerful version of getopt.
If it ain't broke, I can fix that.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP