Some problem about file test


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Some problem about file test
# 1  
Old 02-01-2009
Some problem about file test

Hi,
I'm writing a part of script to test the files, here is what is looks like:
if [ -r /test/A*.TXT ]
then
do somthing
fi


This script runs well on HPUX. However, when I test it on Linux(redhat), it only works if there is only one file with name A*.TXT. If there's more than one files with this kind of name in that directory, the test will never pass.
Can anyone help me with this? Thanks a lot!
# 2  
Old 02-01-2009
You cant check the readability of all files at one time, you need to do it one by one.
# 3  
Old 02-01-2009
Quote:
Originally Posted by phantandy
Hi,
I'm writing a part of script to test the files, here is what is looks like:

Please put code inside code tags.
Quote:

Code:
if [ -r /test/A*.TXT ]
   then
     do somthing
   fi

This script runs well on HPUX.

With which shell? It wouldn't work in any Bourne-type shell.
Quote:
However, when I test it on Linux(redhat), it only works if there is only one file with name A*.TXT. If there's more than one files with this kind of name in that directory, the test will never pass.
Can anyone help me with this? Thanks a lot!
Code:
for file in /test/A*.TXT
do
  if [ -r "$file" ]
  then
    : do something
  fi
done

# 4  
Old 02-01-2009
This one works too, tested on gentoo, bash 3.1.16
Code:
for i in $( find /test -iname "A*.TXT" );
do
	if [ -r "${i}" ] ; then
		printf "Test OK\n"
	else
		printf "Test NG\n"
	fi
done

# 5  
Old 02-01-2009
Quote:
Originally Posted by wolwy_pete
This one works too, tested on gentoo, bash 3.1.16

I'll bet you didn't test it in a directory where there's a matching filename containing a space.
Quote:
Code:
for i in $( find /test -iname "A*.TXT" );
do
	if [ -r "${i}" ] ; then
		printf "Test OK\n"
	else
		printf "Test NG\n"
	fi
done

# 6  
Old 02-01-2009
oops!! ur correct cfajohnson,
never thought of spaces in a file name,
sorry abt that.
# 7  
Old 02-01-2009
Quote:
Originally Posted by cfajohnson

Please put code inside code tags.

With which shell? It wouldn't work in any Bourne-type shell.
Code:
for file in /test/A*.TXT
do
  if [ -r "$file" ]
  then
    : do something
  fi
done

Thanks, your code is ok!

BTW, my original code really works on my HPUX Smilie
The first line is #!/bin/sh, how can I check the exact version?
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test shell script (PROBLEM)

Dears , kindly I wanna do test for one KSH script to know how is it working , the problem that I'm facing is whenever put "sh -x ./my_script.sh" the output seems very long & although I tried to to redirect it to files as it shown , but it failed :eek: :- sh -x ./my_script.sh >... (2 Replies)
Discussion started by: arm
2 Replies

2. UNIX for Dummies Questions & Answers

Problem with test syntax in script

Hi guys here i'm again with more question The code below try to find an user and write everything about him if exist, so my problem appear on line of test, where the program test if the user has secondary groups related. The rest it's clear # usugrup.sh lista todas las caracteristicas de un... (3 Replies)
Discussion started by: Newer
3 Replies

3. Shell Programming and Scripting

Problem on Cygwin with HTK when trying to test .mfc files

I have a problem on working in Cygwin with HTK when running .mcf files. When I enter ./runDemo configs/01ic0201.mfc I have the following error: Cannot find proto config file /proto_s_m_c.pcf How do I fix this problem? (2 Replies)
Discussion started by: advise20023
2 Replies

4. Shell Programming and Scripting

Problem in test file operator on a ufsdump archive file mount nfs

Hi, I would like to ask if someone know how to test a files if exist the file is a nfs mount ufsdump archive file.. i used the test operator -f -a h almost all test operator but i failed file1=ufs_root_image.dump || echo "files doesn't exist && exit 1 the false file1 is working but... (0 Replies)
Discussion started by: jao_madn
0 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Shell Programming and Scripting

Problem when test to see if directory exists

Hi, I'm writing a shell script that will create a folder if it does not exist yet. Here's the script: (this if statement is inside a while loop) folderName="Pics" if ! test -d folderName then mkdir $folderName fi However, after the folder Pics has been created, every time the... (3 Replies)
Discussion started by: trivektor
3 Replies
Login or Register to Ask a Question