setting file count to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting setting file count to a variable
# 1  
Old 07-19-2005
setting file count to a variable

Hey guys. My goal here is to count the number of .dat files in in a directory(28 files). If 28 files exist I am ok. Having trouble doing this. Any help would b e greatly appreciated.

#!/usr/bin/ksh
#=============================================================================
### Define local variables
#=============================================================================
typeset -i Z_FILE_COUNT=0

Z_FILE_COUNT= echo ls *.dat | wc -l
echo "count is $Z_FILE_COUNT"
# 2  
Old 07-19-2005
Code:
#!/usr/bin/ksh
#=======================================
### Define local variables
#=======================================
typeset -i Z_FILE_COUNT=0

Z_FILE_COUNT=$(ls *.dat | wc -l)
echo "count is $Z_FILE_COUNT"

# 3  
Old 07-19-2005
Quote:
Originally Posted by vino
Code:
Z_FILE_COUNT=$(ls *.dat | wc -l)

This line looks dubious, because "ls <pattern>" uses a tabbed output with several columns, hence "wc -l" (count the lines) will presumably give the wrong result.

Either use "ls -1 *.dat" (which will output one file each line) or use "wc" (without the "-l" switch it will count "words" instead of "lines") in the line above. Preferably use "Z_FILE_COUNT=$(ls -1 *.dat | wc -l)" because this would cover for filenames containing blanks, which would otherwise confuse wc.

bakunin
# 4  
Old 07-19-2005
Thanks for the post. Got me running
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading properties from file and setting variable values

I want to read properties from a file and print evaluated values of each key. I am using AIX6.1. myfile.props protocol=http siteA.host=siteAhostname pageA=pageNameA siteAURL1=${protocol}:/${siteA.host}/pagea/blabla?v1=32 siteAURL2=${protocol}:/${siteA.host}/${pageA}/blabla?v1=32... (5 Replies)
Discussion started by: kchinnam
5 Replies

2. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

3. Shell Programming and Scripting

Getting file count in a variable

Hi All I am checking for the presence of certain no of files in a directory. Only if the required no of files are present should I continue with my processing. For e.g. in the temp directory below, there are 4 files of the format sample_aa(bb)(cc)(dd)_test. I need to check the count of these... (17 Replies)
Discussion started by: swasid
17 Replies

4. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

5. Shell Programming and Scripting

count lines in file to variable

I have a text file in which you need to identify the number of lines that looks like this: awk '{x + +} END {print x}' filename The problem is that I do not know how this data to any variable in which then need to continue to work in a cycle for .. do not know someone help? Sorry for my... (4 Replies)
Discussion started by: gizmo16
4 Replies

6. Shell Programming and Scripting

Setting Environment variable from value in file

I've searched Google and now this forum. Best guess is my search fu is not good (and it probably isn't). The Google search did bring me here. Background I have a number of Korn Shell scripts who all use one of 3 values for an environment variable used in the backup system. On occasion one or... (8 Replies)
Discussion started by: WolfBrother
8 Replies

7. Shell Programming and Scripting

Help setting variable from file contents with spaces

I suppose the easiest way to explain my problem is to show you some code and then show you what the code outputs: -------------------------------------------------- #!/bin/bash echo "This line has spaces" > "/tmp/This Filename Has Spaces.txt" export Foo=$(cat "/tmp/This Filename Has... (4 Replies)
Discussion started by: nrogers64
4 Replies

8. Shell Programming and Scripting

setting a variable by searching within a file

Hi, I am trying to set a variable to be used in later scripting, and am having some difficulty. I want to look in a file called scan.info and find the line that says "variable ok". Then I want to cut the number at the beginning of that line and assign that number as a variable so that later... (4 Replies)
Discussion started by: garth6@hotmail.
4 Replies

9. Shell Programming and Scripting

Setting Variable to oldest file in a directory

I am using a bash script to perform some automated maintenance on files in a directory. When I run the script using $sh -x script.sh <directory> the script works fine. It sets the variable to the oldest file, and continues on. However when I run the script like this $./script.sh <directory>, it... (5 Replies)
Discussion started by: spaceherpe61
5 Replies

10. Shell Programming and Scripting

Piping to a file and setting filename using a variable

Hi all, I would like to send the output of a line in a ksh script to a file, but I need to name the file using a predefined variable: ls -l > $MYVAR.arc But what is the correct syntax for achieving this? I can't seem to find the correct syntax for giving the file an extension. Any... (8 Replies)
Discussion started by: mandriver
8 Replies
Login or Register to Ask a Question