LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 11-20-2007, 07:24 AM   #1
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Rep: Reputation: 15
Shell script problem. check file already exists


Hai there,

I have a question regarding shell scripting.

I've made a script. In this script i am gonna look if a file in the /etc/apache2/sites-avalaible/ already exists.

I can do a simple GREP but if i enter to look if "www" already exists it gives me a message 'File alredy exists'! Because grep also looks at part of the filenames.

[EXAMPLE]
in the directory is a file named www-test so if i grep for 'www' he also finds www-test.

[QUESTION]
Can i change so that the script only searches for the full entry in that directory and doesn't look for part of the filenames?

[MY SCRIPT]

while true; do
echo -n "Which Domain: "
read DOMAIN

if test -z "$DOMAIN"; then
echo "You didnt entered a Domain!"
elif ls -als /etc/apache2/sites-available/ |grep $DOMAIN; then
echo "Domain already exists!"
else
echo "ok!"
break;
fi


THANKS IN ADVANCED
 
Old 11-20-2007, 07:30 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Code:
test -f "$DOMAIN"
 
Old 11-20-2007, 07:58 AM   #3
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Code:
test -f "$DOMAIN"
Tried that but keeps saying domain already exists. any other ideas?
 
Old 11-20-2007, 08:01 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Can you post the script with the "test -f" statement? Maybe there is some ambiguity in the logic.
 
Old 11-20-2007, 08:02 AM   #5
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Can you post the script with the "test -f" statement? Maybe there is some ambiguity in the logic.
Hereby:

while true; do
echo -n "Which Domain: "
read DOMAIN

if test -z "$DOMAIN"; then
echo "You didn't entered a Domain!"
elif test -f"$DOMAIN"; then
echo "Domain already exists!"
else
echo "ok!"
break;
fi
done
 
Old 11-20-2007, 08:20 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by sinister1 View Post
elif test -f"$DOMAIN"; then
Just a little typo which do not bring to a syntax error. Put a space between -f and "$DOMAIN" and it should do the trick. In the code above the shell interprets -f"$DOMAIN" as a whole string and the test is always true!
 
Old 11-20-2007, 08:37 AM   #7
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Just a little typo which do not bring to a syntax error. Put a space between -f and "$DOMAIN" and it should do the trick. In the code above the shell interprets -f"$DOMAIN" as a whole string and the test is always true!
YES!!!! it works! THANKS!!!

What does -f stands for?
 
Old 11-20-2007, 10:28 AM   #8
_phenix_
LQ Newbie
 
Registered: Nov 2007
Posts: 7

Rep: Reputation: 0
test manual page, line 92/93

Code:
       -f FILE
              FILE exists and is a regular file
 
Old 11-20-2007, 03:13 PM   #9
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by _phenix_ View Post
test manual page, line 92/93

Code:
       -f FILE
              FILE exists and is a regular file
didn't even knew that there was a man page for. thanks!!!
 
  


Reply

Tags
already, check, file, script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
Shell script: how to check read only file mode. knockout_artist Linux - Newbie 2 11-01-2007 08:28 AM
ksh check if file exists (using wildcard) problem r18044 Linux - Newbie 5 02-22-2005 07:52 AM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM
How can I check wether a file is empty in Linux by using shell script programming ??? Bassam Linux - General 2 02-26-2004 01:01 AM


All times are GMT -5. The time now is 05:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration