if test in bash - can't see error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if test in bash - can't see error
# 1  
Old 07-03-2012
if test in bash - can't see error

Hi,
I have the following script under bash

Code:
        if [ test -f "$file_name" ]; then
             echo File $file_name Not Found
        else
             echo File $file_name Found
        fi

I get the message
Code:
[: -f: binary operator expected

even the file is found; there is only one file with that name in the directory

I can't figure out what's wrong.
Thanks in advance,
# 2  
Old 07-03-2012
Hi

Either this
Code:
  if  test -f "$file_name"

or this:
Code:
if [ -f "$file_name" ]

This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 07-03-2012
So how the final script should be e.g. in the first case?
# 4  
Old 07-03-2012
Code:
if [ -f "$file_name" ]; then
        echo File $file_name Found
else
        echo File $file_name Not Found
fi

Alternate one ..
Code:
$ [ -f "$file_name" ] && echo "Filename $file_name Found" || echo "Filename $file_name Not Found"

This User Gave Thanks to jayan_jay For This Post:
# 5  
Old 07-03-2012
If I use

Code:
if [ -f "$file_name" ]; then
        echo File $file_name Found
else
        echo File $file_name Not Found
fi

then it works in the opposite direction I hae the impression, i.e. the first if detects non existing file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash condition test at the end of string

I want to check (using bash condition test function) if string contains three spaces, ignoring last three spaces at the end of string. string_to_report='foo bar foo bar ' string_to_ignore='foo bar ' (8 Replies)
Discussion started by: useretail
8 Replies

2. Shell Programming and Scripting

Operator test in bash

Hello, can you please help me because I am totally confused with a simple script: #!/bin/bash ] || ] && echo "Good Morning" ] || ] && echo "Good Night" For me, these two strings are indentical: false || false and there is no point to execute echo command. But the run result is... (5 Replies)
Discussion started by: AndreiM
5 Replies

3. Shell Programming and Scripting

[Solved] Bash test 2 variables to see if ones greater by n

Experts, I have a bash shell script that generates 2 variables that have the current minute and a minute from a log file. Can someone please show me the best way to test if the minutes stray by 5. So basically if: This is ok: Last Fitting Min ============= 02 Current Minute =============... (2 Replies)
Discussion started by: jaysunn
2 Replies

4. Shell Programming and Scripting

Bash script to test IP range on server

Hello, We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately. I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Discussion started by: boxgoboom
4 Replies

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

6. Shell Programming and Scripting

bash script to test network connection - please help

I want to test if my host can connect to any of the following 10 hosts (192.168.1.0 to 192.168.1.9) I didnt know which command i should use, so i chose ping. (i wonder if i should use tracepath or sth else) my code is the following: #!/bin/bash clear firstPart="192.168.1" maxNum="9" ... (2 Replies)
Discussion started by: shadow_boi
2 Replies

7. Shell Programming and Scripting

FIle (directory) test operator (bash)

I'm almost pulling out my hair trying to figure out what's wrong with this... there's no reason I can see that it shouldn't be working. It seems that the code acts as though the conditional statement is true no matter what - I've even tried removing the negation operator, but it always goes into... (5 Replies)
Discussion started by: wildbluefaerie
5 Replies

8. Shell Programming and Scripting

[bash]regexp to test PrintableString

Hello all, I would like some help to find the regexp to test that a word does not contains anything than the character set of printable strings. Especially the word should not contain a underscore. echo "$word" | grep "( \'\(\)+,-\.:=\?)+" if then echo success else echo failed... (2 Replies)
Discussion started by: dolphin06
2 Replies

9. Shell Programming and Scripting

test-and-set in bash

I'm not talking about the assembly instruction TAS, a better name could be check-and-set :) Anyway, is there a way to simplify the following if ; then VAR="something"; fi I have ~20 variables that should be test-and-set like this, and it really looks lame. (2 Replies)
Discussion started by: rayne
2 Replies

10. Shell Programming and Scripting

regex test in bash

Hi I want to do a regex test and branch based on the test result, but this doesn't seems to work :confused: if \) ]] then echo success else echo failed fi (1 Reply)
Discussion started by: subin_bala
1 Replies
Login or Register to Ask a Question