If loop inside function not working.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If loop inside function not working.
# 1  
Old 07-05-2013
If loop inside function not working.

Code:
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

Last edited by Scott; 07-05-2013 at 05:00 AM.. Reason: Please use code tags
# 2  
Old 07-05-2013
Try ,

Code:
function check_deplver 
{
 
here your code
 
}


Last edited by Scott; 07-05-2013 at 05:01 AM.. Reason: Code tags
# 3  
Old 07-05-2013
You didn't call the function - at least not from the code you posted.
# 4  
Old 07-05-2013
I did called the function let me paste the complete things.
Code:
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
Code:
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

Moderator's Comments:
Mod Comment Please use code tags for your code and data

Last edited by vbe; 07-05-2013 at 06:05 AM.. Reason: code tags please
# 5  
Old 07-05-2013
Try running the script with trace.

Add this to your script:

Code:
set -xv

# 6  
Old 07-05-2013
sorry i am a novice , where do i need to add it?
# 7  
Old 07-05-2013
I see the problem.

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

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

Code:
my_function $1

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Getopts inside a function is not working

Hi All, I am using geopts inside a function in shell script. But it is doesnt seem to read the input args and I always gt empty value in o/p. my code is http://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/base/pgrs-sm.gif This message has not been sent. #!/bin/ksh IFS=' '... (1 Reply)
Discussion started by: prasperl
1 Replies

3. Shell Programming and Scripting

ash busybox read command not working inside function....

I have a script that has to execute a read command in a function, this is in an ash busybox. The code is... trapcatch () { echo "Ctl-c Detected, what do you want to do?" echo "Please choose the number of one of the following options" echo "1. Jump past this Set" echo "2. Exit... (8 Replies)
Discussion started by: tesser
8 Replies

4. Shell Programming and Scripting

set -options not working inside for loop?

I'm a beginner in shell scripting (I'm using ksh). I'm manipulating some files and I'm using set -A to transform each read line into a numeric array. However, inside the 'for' loop the options of set (ie '-A') are not recognized (the vi editor doesn't highlight it and it doesn't work). Where... (4 Replies)
Discussion started by: kasumlolla
4 Replies

5. Shell Programming and Scripting

string comparison not working inside while loop

The string comparison highlighted below is not working fine. Please help: while read line do # Get File name by deleting everything that preceedes and follows Filename as printed in cvs status' output f_name=`echo $line | sed -e 's/^File://' -e 's/ *Status:.*//' | awk '{print $NF}'` ... (4 Replies)
Discussion started by: sudvishw
4 Replies

6. Shell Programming and Scripting

String comparison not working inside while loop

Hi, In the code included below, the string comparision is not working fine. Please help while (( find_out >= i )) do file=`head -$i f.out|tail -1` dir=`dirname $file` cd $dir Status="" if ; then Status=`cvs -Q status... (3 Replies)
Discussion started by: sudvishw
3 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

String handling is not working inside if loop

Hi All, I am comparing two strings inside an if condition if the strings are same then it should go inside the loop else it should execute code given in else part. But there is a but inside my script Even if the if condition is true it is not going inside the loop also it is executing... (4 Replies)
Discussion started by: usha rao
4 Replies

9. Shell Programming and Scripting

Limiting the hanging of a function inside a loop

#! /bin/ksh ..(some lines are here..) for t in (LIST) do a=function($t) #here I want to wait till the function hangs for 5 secs else want to continue the loop for other values of t. done if ;then ............. fi ...... ...................... Suppose in this script;... (7 Replies)
Discussion started by: Niroj
7 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question