1
#!/bin/bash

if [ -z "$1" ]
  then
    echo "No argument supplied"
    exit
fi

if [ "$1"="abc" ] ; then
abc
exit
fi

if [ "$1" = "def" ]; then
def
exit 1
fi

function abc()
{
    echo "hello"
}

function def()
{
    echo "hi"
}

Here abc is a function which has local definition. But Bash is giving error "./xyz.sh: line 10: abc: command not found". Please give me any Solution?

2
  • Show the whole script, or at least the function definition relative to your if statement. Commented Sep 28, 2012 at 4:06
  • 3
    The function needs to be declared before the code you are executing. Commented Sep 28, 2012 at 4:45

2 Answers 2

2

All functions must be declared before they can be used, so move your declarations to the top.

Also, you need to have a space on either side of the = in your string comparison test.

The following script should work:

#!/bin/bash

function abc()
{
    echo "hello"
}

function def()
{
    echo "hi"
}

if [ -z "$1" ]
  then
    echo "No argument supplied"
    exit
fi

if [ "$1" = "abc" ] ; then
   abc
   exit
fi

if [ "$1" = "def" ]; then
   def
   exit 1
fi
Sign up to request clarification or add additional context in comments.

2 Comments

It would be somewhat more succinct and idiomatic to do the conditional as a case statement, by the way. case $1 in abc) abc; exit;; def) def; exit 1;; esac
And your error message should look like echo "$0: No argument supplied" >&2 with the script name in the message and the redirect to standard error.
0

I suspect there is a problem with your declaration of abc. If you provide the code for your script, we'll be more able to provide specific help, but here is an example of what I think you are trying to achive.

#!/bin/bash -x

abc(){
  echo "ABC. bam!"
}

foo="bar"
if [ "$foo"="bar" ]; then
  abc
else
 echo "No bar for you"
fi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.