Checking the existance of multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking the existance of multiple files
# 15  
Old 10-14-2010
Quote:
Originally Posted by methyl
And a one-liner.

Code:
[ -f /path/filename* ] && echo "File Exists" || echo "Fail"



In any other shell this will produce an error if there is more than one file present. If this works in a Korn shell it is probably because it is secretly using a shell builtin.
Quote:
It works in Posix shells but gives a syntax error in normal ksh88.
ksh: test: argument expected
What shell are you using (pdksh?) on what platform? If not pdksh, what does man test say?
# 16  
Old 10-14-2010
My example was from ksh88 on HP-UX. Neither "man ksh" nor "man test" mention "test -e". Though I don't expect to find "test -e" in "ksh", many recent posts show that there are some variants out there.
The "test" command is both a "ksh" builtin and an external command.
# 17  
Old 10-14-2010
Interesting, since this means that the implementation of ksh88 on HP-UX is not fully posix compliant.
They must be using a very early implementation where -a was used instead of -e, which was later deprecated.
Interestingly man sh-posix does mention -e . So one would have to use /bin/sh to get that functionality (or use /usr/dt/bin/dtksh of course)

Code:
test [expr]

    *

      Evaluate conditional expression expr. See test(1) for usage and description. The arithmetic comparison operators are not restricted to integers. 
      They allow any arithmetic expression. The following additional primitive expressions are allowed:

      -L file

          True if file is a symbolic link.
      -e file

          True if file exists.
      file1 -nt file2

          True if file1 is newer than file2.
      file1 -ot file2

          True if file1 is older than file2.
      file1 -ef file2

          True if file1 has the same device and i-node number as file2.


Last edited by Scrutinizer; 10-14-2010 at 09:57 AM..
# 18  
Old 10-14-2010
Scrutinizer is quite correct. The filename expansion is taking place first and creating a long [ -e ] statement.

On my tests, a "test -f" is only actually testing the first name in the expanded list! If we seed the test with discrete filenames we get some unusual behavior:

Code:
touch testfile1
echo "List of files"
ls testfil*
echo ""
#
echo "Testing testfile2 then testfile1"
if [ -f testfile2 testfile1 ]
then
        echo "ok"
else
        echo "not ok"
fi
echo ""
echo "Testing testfile1 then testfile2"
if [ -f testfile1 testfile2 ]
then
        echo "ok"
else
        echo "not ok"
fi

List of files
testfile1

Testing testfile2 then testfile1
not ok

Testing testfile1 then testfile2
ok

Therefore those methods which either count the number of files or process each file in a loop are preferable. If nothing else because some implementations of "ksh" don't seem to cope with the expanded list.


Footnote: ksh88 is definitely not Posix compliant. Many standards have come and gone (including past failed Posix standards) since ksh88 was released. I find ksh88 to be pretty standard across multiple platforms of multiple vintages and continue to wait for a viable replacement cross-platform standard.

Last edited by methyl; 10-14-2010 at 10:16 AM..
# 19  
Old 10-14-2010
This says more:
Quote:
An early proposal used the KornShell -a primary (with the same meaning), but this was changed to -e because there were concerns about the high probability of humans confusing the -a primary with the -a binary operator.
The Open Group Base Specifications Issue 7

Additional information on the differences:
sh-posix(1)

posix shell was largely derived from ksh88, so while ksh88 is not fully compliant, but I think there are not many differences and ksh88 is largely a superset, but obviously not completely and less than I thought.
Quote:
Originally Posted by methyl
Footnote: ksh88 is definitely not Posix compliant. Many standards have come and gone (including past failed Posix standards) since ksh88 was released. I find ksh88 to be pretty standard across multiple platforms of multiple vintages and continue to wait for a viable replacement cross-platform standard.
So I found out Smilie. This is even wrong in O'Reilly's learning the Korn Shell
About the cross-platformness: If you are willing to give up goodies like arrays, then you could use posix standard and use the posix implementations on the various platforms (/bin/sh on hpux, /usr/xpg4/bin/sh on Solaris).. At least then your scripts will work on modern implementations too). You could also standardize on ksh93 (which is available as /bin/ksh93 or /usr/dt/bin/dtksh), which is also fully POSIX compliant (and is free downloadable software anyway nowadays).

S.

---------- Post updated at 16:23 ---------- Previous update was at 15:54 ----------

So in the case of a POSIX-compliant shell you can use:

Code:
exists()
{ 
  [ -e "$1" ]
}

in case of original version of ksh88:
Code:
exists()
{ 
  [ -a "$1" ]
}

If you want to only test for a regular file (and not a directory for example) you could do something like this:
Code:
file_exists(){
  until [ -f "$1" ] ; do
    [ $# -le 1 ] && return 1
    shift
  done
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge multiple tab delimited files with index checking

Hello, I have 40 data files where the first three columns are the same (in theory) and the 4th column is different. Here is an example of three files, file 2: A_f0_r179_pred.txt Id Group Name E0 1 V N(,)'1 0.2904 2 V N(,)'2 0.3180 3 V N(,)'3 0.3277 4 V N(,)'4 0.3675 5 V N(,)'5 0.3456 ... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

2. Shell Programming and Scripting

CSV joining and checking multiple files

Hello, For our work we use several scripts to gather/combine data for use in our webshop. Untill now we did not had any problems but since a couple days we noticed some mismatches between imports. It happened that several barcodes where matched even though it was a complete other product. Of... (19 Replies)
Discussion started by: SDohmen
19 Replies

3. Shell Programming and Scripting

Checking File record equal to multiple of 70 or nearest number to multiple of 70

Hello, I have a file with below content - Example 3 6 69 139 210 345 395 418 490 492 I would like the result as - Multiple of 70 or nearest number in the file less than the multiple of 70 69 139 (5 Replies)
Discussion started by: Mannu2525
5 Replies

4. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

5. Shell Programming and Scripting

Record count checking for multiple files through for-loop

Hi Friends, I wrote one shell script to check the record count in two files and that will send us the notification activity if found zero record count. What i did is I created for loop and checking the count for both of the files but what is happening is for first file has data then it's... (13 Replies)
Discussion started by: victory
13 Replies

6. Shell Programming and Scripting

Error while checking file existance

Kindly help on below script: << i='find ...' if then echo 'File Exists' else echo 'File Does Not Exist' >> If i has some file name then it runs properly but if i has nothing ( blank value) then it throws an error. I dont exactly remember right now but error seems like:... (2 Replies)
Discussion started by: ravigupta2u
2 Replies

7. Shell Programming and Scripting

how to check existance of multiple directories

Hi, I would like to check whether all the directories exists or not. I tried the below but it gives some error. below is the excerpt from my original script 24 #Check if the required directories are exists 25 dirExists() { 26 27 if 28 then 29 echo "required... (1 Reply)
Discussion started by: lookinginfo
1 Replies

8. Shell Programming and Scripting

The checking of folder existance is not working...

Hi all, I have the following code: if ; then echo 'folder not exist'; else echo 'folder exist'; fi The "testing" folder is not exist in /home/batch , but thhe result is 'folder exist'. It seems that the code cannot detect that the folder "testing" not exist. ANybody know the... (1 Reply)
Discussion started by: suigion
1 Replies

9. Shell Programming and Scripting

checking existance of files and directories

I have a list of files and their directories, starting from a particular location. e.g. temp/new/a.c temp/new/b.c temp/old/a.c temp/old/older/b.c etc. Now I want to check the existence of all these files in the respective directories . I am trying something like cat "fileList" |... (4 Replies)
Discussion started by: herojeett
4 Replies

10. UNIX for Dummies Questions & Answers

Checking file existance by extension

I'm sure this can't be too tough, but. I want to check the existance of file(s) by extension. There may be 0,1,N files. If any files exist, then remove them. Thanks for any insight. (3 Replies)
Discussion started by: niswonp
3 Replies
Login or Register to Ask a Question