LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2012, 04:17 AM   #1
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
find in bash script returns "missing argument to `-exec'" while cmd runs fine


Hi folks,

i wrote a little shell script involving find. I use variables to construct the final find command. While running the echoed find command on cmd line everything works fine but inside the bash script find returns "/usr/bin/find: missing argument to `-exec'". Anyone can help me out on this?

Code:
#!/bin/bash
#############
# zip'em up #
#############

# Files older than btime are tared
btime="+0"                              # has to be zero to take last day into account
# Which directory to start from
startd="/usr/coremedia/cap-solr-replication/jakarta-tomcat/logs"
saved="./moved"

TAR_CMD="/bin/tar"
TAR_OPT="--remove-files -czf"
TAR_SUF="tar.gz"

FIND_CMD="/usr/bin/find"
FIND_OPT="-daystart -maxdepth 1 -mtime $btime"
#FIND_EXC_TAR="-execdir $TAR_CMD $TAR_OPT '{}'.$TAR_SUF ´{}´ \;"
#FIND_EXC_MV="-execdir $MV_CMD ´{}´ $saved"
FIND_EXC_TAR="-exec ls \{\} \;"
FIND_EXC_MV="-exec ls \{\} \;"

# Tar files older than btime
echo "Finding files older than today and tar'em"
TAR_EM="$FIND_CMD $startd $FIND_OPT $FIND_EXC_TAR"
echo -e $TAR_EM
$TAR_EM

# move tar files into moved
echo 'Moving files that end of ' $TAR_SUF
MV_EM="$FIND_CMD $startd -name '*.'$TAR_SUF $FIND_EXC_MV"
echo -e $MV_EM
$MV_EM
 
Old 01-27-2012, 05:23 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I haven't tested but you may be finding files with spaces in the path, try adding some quotes.
 
Old 01-27-2012, 06:01 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
FIND_EXC_TAR="-exec ls \{\} \;"
FIND_EXC_MV="-exec ls \{\} \;"
instead:
Code:
FIND_EXC_TAR="-exec ls {} ;"
FIND_EXC_MV="-exec ls {} ;"
 
1 members found this post helpful.
Old 01-27-2012, 06:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
More often than not you will find it better to place commands into a function and call it then using variables which may perform unexpectedly.
 
Old 01-27-2012, 06:33 AM   #5
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748

Original Poster
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
@kdp: So where should I add the quotes?

@NevemTeve: That way it works, thanks a lot. I guess its the way bash handles variables. You have an idea where I can read up on this in more depth? As man find says that one has to escape {} and ;...

@grail: I'll consider this.
 
Old 01-27-2012, 07:25 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by zhjim View Post
@NevemTeve: That way it works, thanks a lot. I guess its the way bash handles variables. You have an idea where I can read up on this in more depth? As man find says that one has to escape {} and ;...
From Grouping Commands (about {}): "Bash provides two ways to group a list of commands to be executed as a unit. When commands are grouped, redirections may be applied to the entire command list. For example, the output of all the commands in the list may be redirected to a single stream" and "Placing a list of commands between curly braces causes the list to be executed in the current shell context". A bare {}, is thus an empty list of commands and the shell will discard it. This meaning may be removed by backslash escaping the { and the } as the find command man page suggests or by putting it in quotes as '{}' to ensure it is passed to find. Your problem was that you were doing both -- quoting and escaping. The result was that \{\} was passed to find instead of {}.

From Looping Constructs (about ;): "Note that wherever a ‘;’ appears in the description of a command's syntax, it may be replaced with one or more newlines ". As for {}, this meaning may be removed by escaping or quoting but using both passes \; to find.

Last edited by catkin; 01-27-2012 at 07:27 AM. Reason: sense and legibility
 
1 members found this post helpful.
Old 01-29-2012, 06:54 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Read here for why you shouldn't store command syntax in a variable:

http://mywiki.wooledge.org/BashFAQ/050

Do as grail suggested and set it up as a function. Or if you absolutely insist on using variables, at least put it an array and store each option in a separate position.
 
1 members found this post helpful.
Old 01-30-2012, 06:23 AM   #8
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748

Original Poster
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Thanks to be both of you catkin and David. I already skimed over the links but are about to understand them fully.

So catkin if I understand it right you say that.
Code:
" \{\} "
is both qouting and escaping. I always thought that "" (double quotes) is a quotation that allows interpretation of special characters in contrast to '' (single quotes). But I'm not sure if this also implies escaping.

I'll just have myself another look at the bash man page as well as the BASH Scripting Tutorial to get on better ground. I think I have to dig some more on how bash interprets files in relation to command lines.
 
Old 01-30-2012, 07:14 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
From the GNU Bash Reference on Double Quotes: "The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified".
 
Old 01-31-2012, 04:52 AM   #10
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748

Original Poster
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
All right. Thanks catkin.
 
Old 01-31-2012, 05:47 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If the problem is now solved you can mark the thread SOLVED via the Thread Tools menu.

EDIT: actually you can mark it SOLVED whether or not it is actually solved ... but you know what I am expressing badly.

Last edited by catkin; 01-31-2012 at 05:48 AM.
 
Old 01-31-2012, 08:56 AM   #12
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748

Original Poster
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
thanks for the reminder. Jup the problem itself is solved.
 
  


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
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM
please help with bash find / -name "*.sized.jpg" -exec rm -f {} ; rioguia Programming 4 01-16-2010 03:48 PM
find: missing argument to `-exec' mkyp Linux - General 4 10-09-2009 11:13 AM
find: missing argument to `-exec' pavodive Linux - Newbie 4 10-05-2009 02:24 PM
Help please - "#exec cmd" stops working after downloading to Windows crabjoe Linux - Newbie 3 02-15-2006 01:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 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
Open Source Consulting | Domain Registration