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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Chek if a file exists in Ubuntu and Cent OS using shell script
# 1  
Old 03-26-2008
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 [-e $filename ];
then
echo "The file exists"
else
echo "Does not exist"
fi

This gives me error saying the ="/usr/local/net/var/lib/directoryservice/sync.disable" is not a directory or file and -e option does not exist. I need to be working on ubuntu and centos.

Any help will be appreciated.

Thank you.
# 2  
Old 03-26-2008
You need a space between the opening square bracket and the -e. The opening square bracket -- believe it or not -- is the name of a command (also known as test) and like other commands, it needs to be its own space-separated token.

Also, you need to drop the dollar sign when assigning to a variable, and not have any spaces between the variable name, the equals sign, and the value.
# 3  
Old 03-26-2008
Oh woow its unbelievable that such a small mistake would cost so much time. Thanks a lot it works fine now.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

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

3. Shell Programming and Scripting

Shell Script for generating NTIDs(Usernames) from the email ids which exists in MSAD Directory

Hi Everyone, I just need a shell script which automatically gives the list of NT IDs mean the Usernames from the list of email ids. We have around 140 users from AMERICAS,ASIAPACIFIC and EMEA User Directories and we have their email ids.For ex. i have email id called naveen-kumar.dasu@hp.com... (7 Replies)
Discussion started by: naveen.dasu
7 Replies

4. Windows & DOS: Issues & Discussions

Script that, if file exists in Samba share, moves file to Unix server

I'm looking to do pretty much what the title says. I want a script that runs, it can run on Unix or Windows, doesn't matter, and searches a Samba shares for a .txt file. If the file exists, the script will move (or possibly copy) the file from the Samba share into a directory on our Unix... (3 Replies)
Discussion started by: twcostello
3 Replies

5. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

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

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

8. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

9. UNIX for Dummies Questions & Answers

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... (2 Replies)
Discussion started by: blakmk
2 Replies
Login or Register to Ask a Question