Solved

5th argument doesn't work!!

Posted on 2006-10-30
14
222 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now