If file exists (bourne shell)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If file exists (bourne shell)
# 1  
Old 11-17-2003
If file exists (bourne shell)

Im trying to code some logic into a test script to test for the existence of a file before recreating it. Im using the following line to test for this:
if -r test.txt;
However I get the error message
./testScript.sh: -r: not found
Having read through the man pages im still not clear whats going wrong.



Heres a copy of a script im testing this with.

#!/usr/bin/sh

if -r test.txt;
then
echo test.txt exists;
else
echo test.txt does not exists;
fi
# 2  
Old 11-17-2003
#!/usr/bin/sh
if [ -f ./pressyfile ]
then
echo blabla
else
echo nonblabla
fi


greetings Preßy
# 3  
Old 11-19-2003
test for file exist

You are on the right track, but you need to tell the shell what to do with the -r test.txt. You can either use the command test ef if test -r ..... or enclose your test with square brackets eg
if [ -r test.txt ]
then
....
else
.....
fi
NB the spaces after the sopen square and before the close square brackets. Also you are testing for read permission on file rather than -f file or -s file has content and exists.Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Determine a shell variable exists in file?

Hi Folks, Trying to build up a script that will lookup a username invoked as: ./buildscript.sh <username> This should take <username> and look it up in <username_file> and prepare for further processing. Here is the snippet that isn't working just right: user=$1 if ]; then echo... (1 Reply)
Discussion started by: gdenton
1 Replies

2. Shell Programming and Scripting

Open a file from within a Bourne shell Script

I am suppose to make a program that cuts the extension off of a file. It uses that .ext to decide which program needs to be used to open the file. This only finds the name of the program from a list of programs I made. My problem is when it has the name of the program it needs to use, how can I... (3 Replies)
Discussion started by: dordrix
3 Replies

3. Shell Programming and Scripting

How to check the repetition values in a file using bourne shell

Hi all, I have a scenario, like consider a file abc.txt, inside abc.txt, the contents is value1 = aaa, value2 = bbb, value3 = ccc, value1 = ddd. In this situation i need to throw an error for the repeatation of keys like "value1" is repeating twice. how to handle this using bourne... (1 Reply)
Discussion started by: Nandagopal
1 Replies

4. Shell Programming and Scripting

Checking if file exists using a NOT operator and shell variable

Hi, I'm new to UNIX, at least shell programming and am having trouble figuring out a problem i'm having. In one section in my nested if statement, i want the program to test if the file does not exist, based on an argument supplied at the command line by the user. What i have is elif ; then... (3 Replies)
Discussion started by: rowlf
3 Replies

5. Shell Programming and Scripting

Shell script to check if any file exists in 4 folders

Hi All, working on AIX 5.3. Requirement is: Shell script in ksh to check if any file exists in 4 folders as below: 1. /FILE/INB/INT1 2. /FILE/INB/INT2 3. /FILE/INB/INT3 4. /FILE/INB/INT4 Thanks a lot for your time! a1_win. (3 Replies)
Discussion started by: a1_win
3 Replies

6. Shell Programming and Scripting

Delete File 5 Min old in Unix (Bourne Shell)??

Guys, I am writing a script to delete files 5 min old in Sun Os Unix. find . -name "*.txt" -mtime +1 -print I could find an equivalent command to look for files which are 5 min old. i tried -mmin option it didn't work either. Can any body trow a light on this. Cheers :) S:b: (5 Replies)
Discussion started by: sudharma
5 Replies

7. UNIX for Dummies Questions & Answers

"if exists" Bourne Shell

hi all! new to the forum and i love how nice alot of you are. and i was hoping to get some help. a friend of mine asked me to make him a script for backtrack which is based on slax. it makes a new changes.lzm for the usb system and merges 2 files to update it. now i got that handled but i cant seem... (2 Replies)
Discussion started by: pcstalljr
2 Replies

8. Shell Programming and Scripting

Chek if a file exists in Ubuntu and Cent OS using shell script

I have tried few examples in the internet but all of them are different and none worked. I need to check if a file exists in a directory if it does not then exit . here is what I have for now $filename ="/usr/local/net/var/lib/directoryservice/sync.disable" if ; then echo "The file exists"... (2 Replies)
Discussion started by: m_kk
2 Replies

9. UNIX for Dummies Questions & Answers

How do I test for the presence of a file in Bourne Shell

How do I test for the presence of a file in Bourne Shell (3 Replies)
Discussion started by: vins
3 Replies

10. Shell Programming and Scripting

Read a file and replace a value- Bourne and Korn shell

Hello, I have a file called Delete and within the delete file I have the following statement: delete from employee where employee_id = " " how do I write a script that read from this file and replace: employee_id = " " with employee_id is null Please assist...greatly... (3 Replies)
Discussion started by: kenix
3 Replies
Login or Register to Ask a Question