[Solved] Problem bash if file exists then < do...> else <do...>


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Problem bash if file exists then < do...> else <do...>
# 1  
Old 05-27-2013
[Solved] Problem bash if file exists then < do...> else <do...>

Hi !

I have a problem with an if/else statement in bash.
I want to check if the file exists before running a task (for example here, counting lines), and if not I need to create an empty output file
Code:
if [ -f /input.tab ]
then
wc -l input.tab > output.temp
else
>output.temp
fi

The problem is even if the condition is true (the file actually exists) it goes directly to the else part and create an empty file in any case.

Could someone explain me what is wrong with my syntax... Smilie!!!
# 2  
Old 05-27-2013
Quote:
Originally Posted by beca123456
Hi !

I have a problem with an if/else statement in bash.
I want to check if the file exists before running a task (for example here, counting lines), and if not I need to create an empty output file
Code:
if [ -f /input.tab ]
then
wc -l input.tab > output.temp
else
>output.temp
fi

The problem is even if the condition is true (the file actually exists) it goes directly to the else part and create an empty file in any case.

Could someone explain me what is wrong with my syntax... Smilie!!!
Does the file input.tab really exist in the root directory (/input.tab), or is it in the current directory (input.tab)? It would seem that you should use the same pathname in both the test and the wc commands.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-27-2013
Smilie

Thanks again Don Cragun !
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] File Splitting And Renaming Problem

OK So I Recently Bought A whatbox Seed-box Act!!:cool: I am connected to whatbox via SSH!!! Now i have downloaded a movie and renamed it to 2yify.mp4 (800MB):o When I TYPE the command to split it which is:) split -b 400m 2yify.mp4 It gets renamed into two parts with different names... (4 Replies)
Discussion started by: anime12345
4 Replies

2. Shell Programming and Scripting

Problem with ssh and checking if file exists

Hi All, I am facing a problem while checking for existence of file over ssh ! Basically, i want to ssh and check if file exists.. If file exists return 1. If file does not exits return 0 (or any value) I am using the below code file_avail=`ssh username@host "if ]; then exit 1;... (10 Replies)
Discussion started by: galaxy_rocky
10 Replies

3. UNIX for Dummies Questions & Answers

[solved] Where & what bash env file, Mac OS?

Hi! I wanted to simplify my bash prompt, so I edited my etc/bashrc file. I thought this was the file that would override any other env files. When I opened it, I saw that the way it was setup was not what my prompt looked like, although I forget exactly what was there. But i edited it the way I... (1 Reply)
Discussion started by: sudon't
1 Replies

4. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

5. Debian

Bash script to STOP installation 'if' a file exists...

Hey all, Here's my dilemma: 1. I'm a newbie at scripting! 2. I need to create a script that checks: If a file size is equal to zero, then stop the installation. Is there a way to do this or am I wasting my time??? Thanx in advance! :b: (2 Replies)
Discussion started by: thazsar
2 Replies

6. Shell Programming and Scripting

[Solved] problem - connecting code with external file

hello. this is the code #!/bin/sh total1024=0 total2048=0 total8192=0 if ; then if ; then while read variable do if ; then total1024=$(( $total1024 + 1 )) fi if ; then total2048=$((... (4 Replies)
Discussion started by: Telis
4 Replies

7. Shell Programming and Scripting

bash: check if file exists(without using if)

basically im trying to make this work in a bash shell script without using if statements if then echo testfile exists! fi what it does is check if the file exists or not i have this line but its not working, it checks if the testfile exists if it doesnt it 2> to the dev null... (3 Replies)
Discussion started by: gangsta
3 Replies

8. Shell Programming and Scripting

bash: checking file size -solved

Hello I have srv RHEL5, file system UTDM (EMC DiskXtender Unix/Linux File System Manager 3.5 & EMC Centera). it all works under the scheme: have disk is formatted with a file system UTDM, drive open network - NFS, it write data, then migrate the data in the repository - EMC Centera. There are... (0 Replies)
Discussion started by: moskovets
0 Replies

9. Shell Programming and Scripting

bash script quesiton - if any file exists then ...

I'd like a bash script to simply check to see if any file/files are present in a source directory, and if so move it/them to a target directory. I have this but it doesn't work: #!/bin/bash if then mv /source/* /target fi What is the right syntax? Thanks! (4 Replies)
Discussion started by: graysky
4 Replies

10. Shell Programming and Scripting

html problem: get file name dialog exists?

Hello, I wonder whether if we have a open file dialog as (in gtk+) in html. I know that a submit-type form (such as <input value="Add File" type="submit">) opens such dialog, but it -naturally- tries to submit the file chosen. I only want to find out the file name, not upload it. Thanks! (0 Replies)
Discussion started by: f33ldead
0 Replies
Login or Register to Ask a Question