Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-07-2011
Registered User
 

Join Date: May 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Count number of files and use result as variable

Hi there

I have a .ksh script that I am using on an AIX ( Actual Level 5.3.10.0, Maintenance Level 5.3.0.0) where I am logging into a windows box, doing a file count on that server and returning the output to the UNIX session.

I would like to exit the script at this point in time if the value is greater than 1. I have attempted to write this function into the script, but the variable that I am setting is not being echoed out as required.

When I run the script below, the result is

Quote:
1

There are in the <PATH> directory. Transfer stopped.
The value of $numfiles is returned, but not incorporated into the output.


Code:
#!/usr/bin/ksh

numfiles=$(ssh <SERVER> cmd /c 'dir/b/a-d <PATH> | find /c ".CSV"')

print $numfiles

if [ $numfiles -gt 0 ]
  then
     echo "There are "$numfiles" in the <PATH> directory. Transfer has been stopped."
fi

Can anyone assist with a solution to this issue?

Any help would be greatly appreciated
Sponsored Links
    #2  
Old 07-07-2011
itkamaraj's Avatar
Unix is God
 

Join Date: Apr 2010
Posts: 2,354
Thanks: 21
Thanked 470 Times in 459 Posts
what is the output of print statement ?


Code:
 
print $numfiles

Sponsored Links
    #3  
Old 07-07-2011
Registered User
 

Join Date: May 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by itkamaraj View Post
what is the output of print statement ?


Code:
 
print $numfiles

It prints out the correct value, in this case 1 (I have verified that there is only 1 file in the directory!)
    #4  
Old 07-07-2011
itkamaraj's Avatar
Unix is God
 

Join Date: Apr 2010
Posts: 2,354
Thanks: 21
Thanked 470 Times in 459 Posts
In your statement, you mentioned "I would like to exit the script at this point in time if the value is greater than 1"

But you are checking for > 0 in condition

replace your if condition as below and let me know the output


Code:
 
if [ $numfiles -gt 0 ]
then
echo "$numfiles"
     echo "There are "$numfiles" in the <PATH> directory. Transfer has been stopped."
else
echo "$numfiles"
fi

Sponsored Links
    #5  
Old 07-07-2011
Registered User
 

Join Date: May 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by itkamaraj View Post
In your statement, you mentioned "I would like to exit the script at this point in time if the value is greater than 1"

But you are checking for > 0 in condition

replace your if condition as below and let me know the output


Code:
 
if [ $numfiles -gt 0 ]
then
echo "$numfiles"
     echo "There are "$numfiles" in the <PATH> directory. Transfer has been stopped."
else
echo "$numfiles"
fi

Thanks for your help. I have replaced the if statement an re ranit. It has returned the following:
Quote:

1
in the <PATH> directory. Transfer has been stopped.
Sponsored Links
    #6  
Old 07-07-2011
itkamaraj's Avatar
Unix is God
 

Join Date: Apr 2010
Posts: 2,354
Thanks: 21
Thanked 470 Times in 459 Posts
replace the echo statement. seems, your $numfiles is holding the value of 1


Code:
echo "There are $numfiles in the <PATH> directory. Transfer has been stopped."

Sponsored Links
    #7  
Old 07-07-2011
Registered User
 

Join Date: May 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
I had tried that before itkamaral, but it returns the same results:

Quote:
1

in the <PATH> directory. Transfer has been stopped.
Would you have any other suggestions?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count number of files in directory excluding existing files ammu UNIX for Dummies Questions & Answers 3 08-25-2010 01:54 PM
Count the number of files without ls spkandy Shell Programming and Scripting 37 07-01-2010 09:41 AM
count number of files in a directory finalight Shell Programming and Scripting 5 05-14-2009 07:57 AM
Count the number of files in a directory Raynon Shell Programming and Scripting 14 08-16-2007 10:07 PM
Count number of files in subdirectories cbeverly UNIX for Dummies Questions & Answers 2 06-02-2005 03:06 PM



All times are GMT -4. The time now is 04:23 AM.