Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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
0
akohan
Asked:
akohan
2 Solutions
 
q_shakurCommented:
>> is there any limitation in number of arguments we pass in shell? <<
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,
0
 
akohanAuthor Commented:


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
0
 
ygouthamCommented:
chk your func1 to see if you have used $5 as a variable to be declared as a separate value.  more often it is the problem.

check with an echo statement at the beginning of the func1 to see if all values are captured and move the echo line down the function to see where you are losing the value!

wud be better if the entire script is posted for debug.

goutham
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TintinCommented:
How are you checking that the caller function has a valid 5th argument?

Are you doing something like

func1()
{
  echo "func1 function received $# arguments"

  echo "arg1 $1"
  echo "arg2 $2"
  echo "arg3 $3"
  echo "arg4 $4"
  echo "arg5 $5"
}

echo "a=$a b=$b c=$c d=$d e=$e"
func1 $a $b $c $d $e

I suspect that you've just inspected $e without checking that all the other arguments have values.
0
 
_iskywalker_Commented:
did you maybe check against a  value:
if [ $5 = "" ] ?
cause you should check with ==
you should put the script for our better understanding
0
 
akohanAuthor Commented:


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 .....
}
0
 
TintinCommented:
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.
       
0
 
akohanAuthor Commented:

Tintin,

thanks for your advice so what do you suggest? as far as global arguments?

0
 
akohanAuthor Commented:

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)

}




0
 
akohanAuthor Commented:

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.
0
 
TintinCommented:
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

   ...
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now