LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2011, 02:34 PM   #1
ajcandover
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
executing find from a bash script enviroment


I'm have a script that reads a command from a file, stores it into a variable and then executes the command. This works for most commands however when I try to use the find command with the -name "*.dir" then the command does not work.

Here is my script test.sh
[root@localhost tmp]# cat test.sh
command=`cat /tmp/test.txt`

# Execute the command
/usr/bin/sudo $command

when test.txt contains:
/usr/bin/find /tmp -type d

The script executes fine.
When test.txt contains:
/usr/bin/find /tmp -type d -name "*.dir"

then I don't get any output even though the command itself works from the command line:
[root@localhost tmp]# /usr/bin/find /tmp -type d -name "*.dir"
/tmp/this.dir
/tmp/that.dir

I've even tried to escape the "*.dir" with "\*.dir" and it still doesn't work.

Has anyone ever encountered this problem?
 
Old 09-16-2011, 03:12 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Have you tried without quotes or escapes?
Code:
/usr/bin/find /tmp -type d -name *.dir
 
Old 09-16-2011, 03:22 PM   #3
ajcandover
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Have you tried without quotes or escapes?
Code:
/usr/bin/find /tmp -type d -name *.dir
Unfortunately when I execute without quotes it expands giving me an error:
COMMAND:/usr/bin/find /tmp -type d -name that.dir this.dir
/usr/bin/find: paths must precede expression
Usage: /usr/bin/find [-H] [-L] [-P] [path...] [expression]
 
Old 09-16-2011, 03:33 PM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,326

Rep: Reputation: 253Reputation: 253Reputation: 253
I tried an assigned variable also on the command line - no output too.

It looks like you need:
Code:
eval $command
to split the arguments again.
 
1 members found this post helpful.
Old 09-16-2011, 03:50 PM   #5
ajcandover
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Reuti. It works with eval.

I wonder if eval will affect negatively other commands sent to this script for execution.

I've also found this site which explains the behaviour:
http://fvue.nl/wiki/Bash:_Why_use_ev...e_expansion%3F

Code:
:
command=`cat /tmp/test6.txt`
# Execute the command
echo COMMAND:$command
eval /usr/bin/sudo $command
output:
Code:
COMMAND:/usr/bin/find /tmp -type d -name "*.dir"
/tmp/this.dir
/tmp/that.dir
 
Old 09-16-2011, 04:01 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

this worked for me:
Code:
$ cat test.txt
/usr/bin/find /tmp -type d -name *.dir
$ cat sh.sh 
#!/bin/bash
#set -x
command=$(<test.txt)
/usr/bin/sudo $command
$ ./sh.sh 
/tmp/4.dir
/tmp/2.dir
/tmp/1.dir
/tmp/3.dir
Generally it is not a good idea to pass an unquoted asterisk to 'find' because it might be expanded by bash and mess up 'find'. In this case, however, you do not have to quote it inside test.txt.
When it will be passed to 'sudo' it will be automatically single-quoted by bash. Uncomment 'set -x' to see how it is being interpreted.
 
1 members found this post helpful.
Old 09-16-2011, 04:04 PM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,326

Rep: Reputation: 253Reputation: 253Reputation: 253
To elaborate this: the issue is similar to the link you posted. The problem is with the * expansion by the shell.

1) You use "*.dir" : then literally this will be searched for, i.e. a directory name starting with a " and ending with a " too.

2) You use *.dir : then it will be expanded by the shell already at the assignment of command.

What also works, is to avoid the expansion of the asterisk by the shell:
Code:
set -o noglob on
command=`cat /tmp/test6.txt`
Whether eval is worse for any other command depends what you want to achieve.

Ok, crts was faster - another solution.
 
  


Reply


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
Bash Script - executing answer to another script domz Programming 4 11-07-2010 05:19 PM
bash script: executing string command chuckj Programming 8 06-17-2010 08:22 AM
Bash script run via cron not executing MYSQL command mackstar Linux - Server 4 04-23-2009 06:01 AM
Hi, I am a complete n00b in need of help executing her bash shell script. savoirefaire Linux - Newbie 4 03-12-2007 07:14 AM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 06:05 PM


All times are GMT -5. The time now is 07:33 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration