Linux test command


 
Thread Tools Search this Thread
Operating Systems Linux Linux test command
# 1  
Old 07-08-2004
Linux test command

Hi,

I was trying to test the presence of some files on Red hat Linux enterprise version 2.4.9 using the following command

test -f /home/pv/T20*

I am getting the error, too many parameters, when I have multiple files starting with T20 on that directory. When there is no files or just one file, no errors and the command works.

Any idea whay I can't check the presence of multiple files? Or is there any other command I can use for this purpose.

Thanks in advance for any help.
# 2  
Old 07-08-2004
You could try iterating through the files instead

Code:
for file in /home/pv/T20*
do
    if [ -f "$file" ]; then
        echo "File $file exists"
    fi
done

Is this what you want?

cheers
ZB
# 3  
Old 07-09-2004
I just want to see(by a a single commad) if there is any file starting with those letters present in that directory so that I can do some processing on those files. test -f /home/pv/T20* command works perfect on HP-UX and I assume it is a bug on this flavor of Linux.
# 4  
Old 07-15-2004
TEST command

Indeed, ran also the test command on both OS'ses and came to the same result.

Maybe you are looking for the find command, and when a file is found you can use the -exec option.

Cheers.
Dieter
# 5  
Old 07-15-2004
Another way to test for files
Code:
if ls /home/pv/T20* >/dev/null 2>&1
then
    echo true
fi

However, this would return true for directories and empty files. Since you want to do some processing on the files why not use a for loop...
Code:
for file in $(ls /home/pv/T20*)
do
   [ -s $file ] || continue
   echo do something with $file
   :
done

If there are no files then the code within the for loop is not executed and this message is sent to standard error: "/home/pv/T20* not found"

PS: It is not good practice to rely on "if test -f" for multiple files. Here's why (on HP-UX)....

$ ls *.txt
b.txt c.txt
$ if test -f *.txt
> then
> echo true
> else
> echo false
> fi
true
$ mkdir a.txt
$ if test -f *.txt
> then
> echo true
> else
> echo false
> fi
false
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture the data in Linux .While doing load test.

Hi All, I am trying to capture the data in linux .While doing load test. is there's any sample script please help me. Linux test4 2.6.18-308.8.1.el5 #1 SMP Fri May 4 16:43:02 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux Thanks, (5 Replies)
Discussion started by: sam1226
5 Replies

2. IP Networking

A wireless test tool for linux?

hi all: I want to find a wireless test tool for linux , just linke netstumbler on windows . i find the tool for long time , but i cann't find one. does somebody give a advice. thanks!!! (0 Replies)
Discussion started by: arnold.king
0 Replies

3. 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

4. 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
Login or Register to Ask a Question