[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

5th argument doesn't work!!

Posted on 2006-10-30
14
Medium Priority
?
277 Views
Last Modified: 2010-04-20

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
Comment
Question by:akohan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 1

Expert Comment

by:q_shakur
ID: 17839559
>> 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
 

Author Comment

by:akohan
ID: 17839588


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
 
LVL 14

Accepted Solution

by:
ygoutham earned 300 total points
ID: 17839816
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 300 total points
ID: 17840220
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
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17842849
did you maybe check against a  value:
if [ $5 = "" ] ?
cause you should check with ==
you should put the script for our better understanding
0
 

Author Comment

by:akohan
ID: 17844706


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
 
LVL 48

Expert Comment

by:Tintin
ID: 17845385
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
 

Author Comment

by:akohan
ID: 17845870

Tintin,

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

0
 

Author Comment

by:akohan
ID: 17845932

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
 

Author Comment

by:akohan
ID: 17845943

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
 
LVL 48

Expert Comment

by:Tintin
ID: 17846137
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question