| Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-20-2007, 07:24 AM
|
#1
|
|
Member
Registered: Jul 2007
Posts: 70
Rep:
|
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
|
|
|
|
11-20-2007, 07:30 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
|
|
|
|
11-20-2007, 07:58 AM
|
#3
|
|
Member
Registered: Jul 2007
Posts: 70
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
|
Tried that but keeps saying domain already exists. any other ideas?
|
|
|
|
11-20-2007, 08:01 AM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Can you post the script with the "test -f" statement? Maybe there is some ambiguity in the logic.
|
|
|
|
11-20-2007, 08:02 AM
|
#5
|
|
Member
Registered: Jul 2007
Posts: 70
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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
|
|
|
|
11-20-2007, 08:20 AM
|
#6
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Quote:
Originally Posted by sinister1
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!
|
|
|
|
11-20-2007, 08:37 AM
|
#7
|
|
Member
Registered: Jul 2007
Posts: 70
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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?
|
|
|
|
11-20-2007, 10:28 AM
|
#8
|
|
LQ Newbie
Registered: Nov 2007
Posts: 7
Rep:
|
test manual page, line 92/93
Code:
-f FILE
FILE exists and is a regular file
|
|
|
|
11-20-2007, 03:13 PM
|
#9
|
|
Member
Registered: Jul 2007
Posts: 70
Original Poster
Rep:
|
Quote:
Originally Posted by _phenix_
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!!!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:33 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|