If loop inside function not working.

check_deplver ()
{
  dir=/abc/def/ghi
  if ssh -o StrictHostKeychecking=no $1 "[ -d $dir ]" 2> /dev/null
  then
    echo " output is "
    ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc"  2> /dev/null
  else
    echo " directory not presnt"
  fi
}

This is not working.

But when i run this commands without function it is working. I need this to work inside a function and later call this function

Try ,

function check_deplver 
{
 
here your code
 
}

You didn't call the function - at least not from the code you posted.

I did called the function let me paste the complete things.

check_deplver ()
{
  dir=/abc/def/ghi
  if ssh -o StrictHostKeychecking=no $1 "[ -d $dir ]" 2> /dev/null
  then
    echo " output is "
    ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc"  2> /dev/null
  else
    echo " directory not presnt"
  fi
}
 
usage ()
{
echo " Please mention the arguments"
echo " example: $0 servername "
}

if [ $# -lt 1 ]
then
usage
else
check_deplver
fi

What i am suspecting is syntax error ,because i am able to get the desired output when i write the code without function. If i simply run

dir=/abc/def/ghi
  if ssh -o StrictHostKeychecking=no $1 "[ -d $dir ]" 2> /dev/null
  then
    echo " output is "
    ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc"  2> /dev/null
  else
    echo " directory not presnt"
  fi

Try running the script with trace.

Add this to your script:

set -xv

sorry i am a novice , where do i need to add it?

I see the problem.

Outside a function, $1 $2 ... mean commandline parameters.

Inside a function, $1 $2 ... mean function parameters.

my_function $1
1 Like

Thanks Corona688!! it worked!!