Solved

5th argument doesn't work!!

Posted on 2006-10-30
14
259 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
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 75 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
Technology Partners: 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 75 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
open source backup solution 1 43
number in printf 13 42
Redhat upgrade 1 48
simple bash script needed to unpack tar.gz 4 27
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

749 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