LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-02-2012, 05:25 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
[bash-script] Problem by "if [ -e $NAME ]; then" ...


Hi,

My script looks as follow:
Code:
#!/bin/sh

NAME=$(find ./out -type f -name "test*")

echo Name=$NAME

if [ -e $NAME ]; then
  echo "file exist"
fi
There is no file under ./out but it returns that there is file test* exist as follwo. Why?

Code:
Name=
file exist

Last edited by thomas2004ch; 03-02-2012 at 05:31 AM.
 
Old 03-02-2012, 05:49 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well this is clearly nothing to do with find itself.

You can reduce it to this:


Code:
if [ -e ]
then
   echo "file exist"
fi
and that "works", which I don't understand. I suppose you could say that "nothing" does exist, but it's a bit philosophical really!

don't use [ ], under bash, use [[ ]] instead and you'll have a better time in general as [ is the test binary, whilst [[ is a bash built in, so it *KNOWS* about the NAME variable you have there, like a normal perl / c program would, but [ / test just sees if after string substitutiong have occurred. Alternatively, if you do use [, ALWAYS put your variables in double quote marks.

Last edited by acid_kewpie; 03-02-2012 at 05:57 AM.
 
Old 03-02-2012, 06:46 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
I reduce the code as you wrote but it doesn't work.

Besides I am not sure if we can use if [ -e ] without an operant.
 
Old 03-02-2012, 06:58 AM   #4
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
An alternative maybe
Code:
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\n' -exec echo "file exist" \;
name=./out/test
file exist
bash-4.1$ rm out/test
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\n' -exec echo "file exist" \;
bash-4.1$ >out/test
bash-4.1$ >out/test1
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\n' -exec echo "file exist" \;
name=./out/test
file exist
name=./out/test1
file exist
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\nfile exist\n' 
name=./out/test
file exist
name=./out/test1
file exist
bash-4.1$

Last edited by whizje; 03-02-2012 at 07:04 AM. Reason: exec is not neccesary.
 
Old 03-02-2012, 07:04 AM   #5
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Now I change the
Code:
...
if [ -e $NAME ]; then
...
to

Code:
...
if [[ -z $NAME ]];  then
...
And this works.
 
Old 03-02-2012, 07:05 AM   #6
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Wink

Quote:
Originally Posted by whizje View Post
An alternative maybe
Code:
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\n' -exec echo "file exist" \;
name=./out/test
file exist
bash-4.1$ rm out/test
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\n' -exec echo "file exist" \;
bash-4.1$ >out/test
bash-4.1$ >out/test1
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\n' -exec echo "file exist" \;
name=./out/test
file exist
name=./out/test1
file exist
bash-4.1$ find ./out -type f -name "test*" -printf 'name=%p\nfile exist\n' 
name=./out/test
file exist
name=./out/test1
file exist
bash-4.1$
Truely to say I don't understand what you mean here.
 
Old 03-02-2012, 07:09 AM   #7
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
You could use only find when a name with test is found printf is executed
Code:
-printf 'name=%p\nfile exist\n'
name= is just printed
%p is the name of the file found
\n is a newline
file exist is also just printed
\n another newline
 
Old 03-02-2012, 07:11 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by thomas2004ch View Post
Now I change the
Code:
...
if [ -e $NAME ]; then
...
to

Code:
...
if [[ -z $NAME ]];  then
...
And this works.
Yes, that's absolutely what I'd recommend.

The bit *I* didn't understand is how you CAN use -e without an operant, but you can, it doesn't error, and that was actually *exactly* what you were doing, as [ doesn't evaluate it's contents until after $NAME is already long gone.
 
  


Reply



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
[SOLVED] Bash script problem - using "read" and Festival TTS Fjerr Fjerrson Programming 4 09-04-2011 12:05 PM
[SOLVED] Bash script problem "No such file or directory" cnmoore Programming 23 03-31-2011 03:55 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:23 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration