Variable logic clarification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable logic clarification
# 1  
Old 05-25-2016
Variable logic clarification

I have one script which consists of variables number of parameter and stuckup with following logic

case:1
script:
parameteres are
username,password,P1=v1,P2=v2,P3=v3

case:2
script:
parameteres are
username,password,P1=v1,P2=v2,P3=v3,P4=v4

i will pass username,password for all the script and no of parameters after that is not fixed.It may be 3 or 4 or 5 etc

I want to store rest of parameters in a variable except username and password.

case:1 output

v=-param P1=v1 -param P2=v2 -param P3=v3


case:2 output

v=-param P1=v1 -param P2=v2 -param P3=v3 -param P4=v4
# 2  
Old 05-25-2016
Would this thread help?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-25-2016
New to shell script and any input will be appreciated. Not able to understand completly
# 4  
Old 05-25-2016
Well, what do you expect? Did you try the proposals given? Did you understand the results and how they are achieved? Consult your shell's man page for details of the constructs shown.

If you've got any difficulties, explain what it is you don't understand, and I'm sure you'll get help.
# 5  
Old 05-26-2016
1)Thanks for reply

This is my script:script.sh
Code:
Numbr_Parms=$#
a=`expr $Numbr_Parms - 2`
while [ $a -le $Numbr_Parms ]
do
if [ "$a" =  "3" ]
then
   PARAMSTRING="-param $3"
else
   PARAMSTRING="$PARAMSTRING -param $a"
   
fi
a=`expr $a + 1`
done
echo $PARAMSTRING

Running:
Code:
script.sh  username pass p1=v1 p2=v2 p3=v3

Expected output:

Code:
-param p1=v1 -param p2=v2 -param p3=v3

But i am getting:$PARAMSTRING as
Code:
-param p1=v1 -param 4 -param 5

Not sure what is the Issue with $4 and $5



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 05-26-2016 at 06:04 AM.. Reason: Added code tags.
# 6  
Old 05-26-2016
You obviously started by copying the original script that notoriously was erroneous. And you reproduced and confirmed the error. How about looking and testing out the solutions provided?
# 7  
Old 05-26-2016
Not working tried below thing.

Code:
Numbr_Parms=$#
a=`expr $Numbr_Parms - 2`
while [ $a -le $Numbr_Parms ]
do
if [ "$a" =  "3" ]
then
   PARAMSTRING="-param $3"
else
   temp=\$$a
   PARAMSTRING="$PARAMSTRING -param \$$temp"
   
fi
a=`expr $a + 1`
done
echo $PARAMSTRING


Last edited by Don Cragun; 05-26-2016 at 04:04 PM.. Reason: Add CODE tags again.
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable clarification

what is the significance of %%.ksh in processname and %.ksh in processname_1 variable? Why is it returning same value?How is it working? processname=Testabc export processname=${processname%%.ksh} echo $processname #It is returning Testabc export processname_1=${processname%.ksh} echo... (2 Replies)
Discussion started by: vamsi.valiveti
2 Replies

2. Shell Programming and Scripting

Missing Assigned Variable within logic operator

Hey , I'm trying to perform the following command, however it cannot read the variable assigned earlier. I'm not sure why this happen. Please help thanks while : do echo "what's ur name? (if none just press )" read name changeName = echo $name | sed "s/on/ey/" echo $changeName #this... (8 Replies)
Discussion started by: sexyTrojan
8 Replies
Login or Register to Ask a Question