akohan
asked on
5th argument doesn't work!!
Hello,
I have a script that was working just fine but now I found there is an error due to a passed argument which is empty.
I traced it and it has a value but when it reaches to the body of a function is emapty. Why? is there any limitation in number of arguments we pass in shell?
func1 $a $b $c $d $e
function func1
{
all have values except $e
}
Would you please give me some advice on this?
Thanks,
A
ASKER
Hi q_shakur,
Yes, exactly I go with $1 and ... $5 always and what I had posted was just an example. Anyway they are all valid except $5! it used to work just fine. I check the caller function and the fifth argument has a valid value but for some reasons when it gets to the function (here func1() ) the value gets lost!
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
did you maybe check against a value:
if [ $5 = "" ] ?
cause you should check with ==
you should put the script for our better understanding
if [ $5 = "" ] ?
cause you should check with ==
you should put the script for our better understanding
ASKER
Hello,
In caller function I call:
ChkMediaSize $DES_DIR $SRC_DIR $1 $REMOTE_MACHINE $2
This is where error happens: (Called function)
in following function the $2 used to had a valid value but not it is empty so when it gets to [ $5 -eq 1 ] causes error.
function ChkMediaSize
{
#passing source path, dest path and conn type (local/remote)
if [ $5 -eq 1 ];
then
#restore case
echo ""
ChkMediaSize $DES_DIR $SRC_DIR $1 $REMOTE_MACHINE $2
else
#backup case
#ChkMediaSize $SRC_DIR $DES_DIR $1 $REMOTE_MACHINE $2
ChkMediaSize $SRC_DIR $DES_DIR $1 $2
fi
etc .....
}
That's a very bad way to handle parameter in a function.
When you call ChkMediaSize with your five parameters, the following is se
$1 = $DES_DIR
$2 = $SRC_DIR
$3 = global $1
$4 = $REMOTE_MACHINE
$5 = global $2
That makes it confusing and conflicting.
Additionally you have a recursive function with no way of exiting (unless that's in the code you are not showing).
I think the whole code needs a big re-think and re-write to make it much clearer and more robust.
When you call ChkMediaSize with your five parameters, the following is se
$1 = $DES_DIR
$2 = $SRC_DIR
$3 = global $1
$4 = $REMOTE_MACHINE
$5 = global $2
That makes it confusing and conflicting.
Additionally you have a recursive function with no way of exiting (unless that's in the code you are not showing).
I think the whole code needs a big re-think and re-write to make it much clearer and more robust.
ASKER
Tintin,
thanks for your advice so what do you suggest? as far as global arguments?
ASKER
Tintin,
Now, I see what you mean. No I had made a mistake when I have been doing copy/paste from my code. That is not recursive at all. for some reasons I have type the same name for the function as ChkMediaSize() by mistake!
Sorry!
this is my code:
caller is here:
if [ $5 -eq 1 ];
then
#restore case
echo ""
ChkMediaSize $DES_DIR $SRC_DIR $1 $REMOTE_MACHINE $2
else
#backup case
#ChkMediaSize $SRC_DIR $DES_DIR $1 $REMOTE_MACHINE $2
ChkMediaSize $SRC_DIR $DES_DIR $1 $2
fi
..........
function ChkMediaSize()
{
# here I use those arguments to check the available room (using \df , -k ) and checking what size is going to be backedup or restored using (du)
}
ASKER
The problem is sovled.
I found out what the problem was. When the forth argument is empty (is read from a config file) the fifith one is assumed (shifted to left) as the forth one then the fifth one will be empty.
That's exactly what I suggested in my original post :-)
That's why you should always check for a valid number of args, eg:
function ChkMediaSize
{
if [ $# -ne 5 ]
then
echo "Incorrect number of args" >&2
exit 1
fi
...
}
That's why you should always check for a valid number of args, eg:
function ChkMediaSize
{
if [ $# -ne 5 ]
then
echo "Incorrect number of args" >&2
exit 1
fi
...
}
there is no limitation, you can write arg1 arg2 arg3 arg4 arg5 argN
so i dont think the problem is in the number of arguments, i think its in the last variable you did, i mean $e .
in the function body you can call the argument as $1 for arg1 , $2 for arg2 $3 for arg3 and so on, try to not use $e and use $5 and see if the argument is empty or not.
##---------
this is a fast example i did for 5 arguments try it, its work
function doit() {
echo $1 $2 $3 $4 $5
}
doit "My" "Name" "is" "q" "shakur"
##---------
>> I have a script that was working just fine but now I found there is an error due to a pass..... <<
in your function example the caller is before the body of the function i think this will give you a command not found
but i think you write it here just for showing us an example and in your script the caller is after the function body
good luck,