Count number of files and use result as variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count number of files and use result as variable
# 1  
Old 07-07-2011
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
# 2  
Old 07-07-2011
what is the output of print statement ?

Code:
 
print $numfiles

# 3  
Old 07-07-2011
Quote:
Originally Posted by itkamaraj
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
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

# 5  
Old 07-07-2011
Quote:
Originally Posted by itkamaraj
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.
# 6  
Old 07-07-2011
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."

# 7  
Old 07-07-2011
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help for Count number of files in certain time

Chaps, I need to count number of files in a remote directory from Linux (FreeBSD) as if 10 trace files (log files) been generated within 5min of time. So this is the script then I can setup a monitoring. I came across with ls -1 \ip\d:\Logs | wc -l but then what else requires to check time... (8 Replies)
Discussion started by: samwijekoon
8 Replies

2. Shell Programming and Scripting

How to count the number of files moved?

I'm writing a script for searching substring in file content and then moving found files. So far I've wrote script shown below grep -lir 'stringtofind' $1 | xargs mv -t $2 How can i count number of files moved? (4 Replies)
Discussion started by: Kadikis
4 Replies

3. Shell Programming and Scripting

Use GREP to count number of records and place it in a variable

I am trying to count the number of records from different files using grep, and then place the result in a separate variable for each file, so at the end of my shell script, I can sum all the variables and check if the number of records are equal to what I was expecting. It is weird butwc -ldoes... (2 Replies)
Discussion started by: dhruuv369
2 Replies

4. Shell Programming and Scripting

Count number of files

Hi All! I need to have a script that counts the number of files arriving in a landing directory, them some app pick these files to be processed and load to a DB. But this process is so fast that I am not able to count all the files arriving on a landing directory. Please can you help? My... (6 Replies)
Discussion started by: fretagi
6 Replies

5. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

6. Shell Programming and Scripting

count of files and number of bytes

1) I need a shell code to count the number of files ( without directories or sub-directories ) in a directory given as arguments I tried this code but it didn't work , maybe I tried the wrong one: numOfFiles=`find $1 -type f -maxdepth 1 | wc -l` I found it in another thread in this site.. ... (17 Replies)
Discussion started by: jack1985
17 Replies

7. UNIX for Dummies Questions & Answers

Count number of files in directory excluding existing files

Hi, Please let me know how to find out number of files in a directory excluding existing files..The existing file format will be unknown..each time.. Thanks (3 Replies)
Discussion started by: ammu
3 Replies

8. Shell Programming and Scripting

count number of files in a directory

what's the script to do that? i want to only count the number of files in that directory, not including any sub directories at all (5 Replies)
Discussion started by: finalight
5 Replies

9. Shell Programming and Scripting

Count the number of files in a directory

Hi All, How do i find out the number of files in a directory using unix command ? (14 Replies)
Discussion started by: Raynon
14 Replies

10. UNIX for Dummies Questions & Answers

Count number of files in subdirectories

Hello, I am new to unix and would like to have a count of all the files in the current directory as well as all the files in a subdirectory. The command I used was ls -R | wc -l but the number returned wasn't correct. Can someone please help? Thanks (2 Replies)
Discussion started by: cbeverly
2 Replies
Login or Register to Ask a Question