[SOLVED] find in bash script returns "missing argument to `-exec'" while cmd runs fine
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
@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 ;...
@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
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.
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.
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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.