- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scripting question
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
Forums
Discussions
Discussions
Discussions
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
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
01-06-2004 09:15 PM
01-06-2004 09:15 PM
How can I put argument as variable in script? Such as:
If first argument to a script is "Arg1". If I do
x=1
echo $($x)
Can I get "Arg1" to print out? I know this is not the correct way to do. Anyone can point out the right way? Thanks.
regards,
tenghin
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:21 PM
01-06-2004 09:21 PM
Re: scripting question
x=$1
echo $($x)
Regards,
Bernhard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:25 PM
01-06-2004 09:25 PM
Re: scripting question
You can capture them as $1, $2 etc. You can use them as any other variable.
echo $1
HTH,
Umapathy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:32 PM
01-06-2004 09:32 PM
Re: scripting question
Thanks for the reply. The reason I want to make the argument as variable is because sometime the position of argument can change, may be first argument, may be second. So I don't think your suggestion can work. Do you have other suggestion ?
regards,
tenghin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:37 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:41 PM
01-06-2004 09:41 PM
Re: scripting question
At some point you have to identify what that incoming argument means. At that same point you can assign them to your variable of choice.
I dont find any problem here.
HTH,
Umapathy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:42 PM
01-06-2004 09:42 PM
Re: scripting question
Thanks a lot. That is just what I want.
Umapathy,
Thanks for you reply also.
Thanks guys!!
regards,
tenghin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:46 PM
01-06-2004 09:46 PM
Re: scripting question
so I guess you want something like
./script -1 arg1 -2 arg2
would work exactly like
./script -2 arg2 -1 arg1
???
then you would use a case statement and use shift for each $1 to read each script argument sequentially and set variables according to whether -1 -2 etc have been found until you have gone through the whole command line.
Regards,
Bernhard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 09:56 PM
01-06-2004 09:56 PM
Re: scripting question
Actually what I want is assign the second last argument to a variable, such as :
x=$#
y=`expr $x - 1`
h=`eval echo \${$y}`
echo $h
I can do "eval echo \${$y}" alone but cannot assign to variable h as above.Any idea anyone ?
regards,
tenghin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 10:09 PM
01-06-2004 10:09 PM
Re: scripting question
One way to do is to use the shift. Go on shifting till last but one.
HTH,
Umapathy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 10:20 PM
01-06-2004 10:20 PM
Re: scripting question
Thanks for the suggestion! Will try that.
regards,
tenghin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 10:21 PM
01-06-2004 10:21 PM
Re: scripting question
x=$#
(( y = x - 1 ))
while [ $y -gt 1 ]
do
shift
(( y = y - 1 ))
done
echo $1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 10:35 PM
01-06-2004 10:35 PM
Re: scripting question
actuall $# will give you the number of arguments, so that you can evalute $(($#-1))
Regards,
Bernhard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2004 11:02 PM
01-06-2004 11:02 PM
Re: scripting question
1st parameter : $1 or ${1}
12th parameter : ${12}
$12 is unknown (or, in fact, ${1} with a 2 at the end)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2004 01:11 PM
01-07-2004 01:11 PM
Re: scripting question
Thanks for the script. It works but I don't what are the lines "((y=x-1))" and "((y=y-1))" for ? and why we need double brackets for these lines? Sorry if I ask dumb question, I'm a newbie in scripting :).
Bernhard,
Thanks for your suggestion. But when I do
"eval $(($#-1))" in script, it gaves error. Would you mind show me the right command line? Thanks.
Marc,
Thanks for your tips!
regards,
tenghin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2004 01:47 PM
01-07-2004 01:47 PM
Re: scripting question
h=$(eval echo \${$(($#-1))})
but
if (( $# < 2 )) ;then
print "not enough args"
exit
fi
#create array of command line arguments
set -A parms $*
(( x=$#-1 )) #x=second to last argument
h=${parms[$x] #h=value of second to last arugment
is easier to understand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2004 01:54 PM
01-07-2004 01:54 PM
Re: scripting question
the syntax ((...)) does arthmetic evaluation
(( y=y-1)) subtracts 1 from y and assigns that value to the variable y.
actually it should be (( y=$y-1))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2004 02:07 PM
01-07-2004 02:07 PM
Re: scripting question
h=${parms[$x] #h=value of second to last arugment
being the array index starts at zero the above actually gives last argument and i forgot the ending brace. so this is what works
(( x=$#-2 )) #x=second to last argument
h=${parms[$x]} #h=value of second to last arugment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2004 02:48 PM
01-07-2004 02:48 PM
Re: scripting question
Thanks a lot. Your first suggestion is the easier way for my need. Also thanks for explanation on the double brackets.
Again thanks to everyone for providing me with such useful tips and explanations. I sure learn a lot of things from this thread :)
regards,
tenghin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2004 07:22 PM
01-07-2004 07:22 PM