开发者

Find not working in script, working in terminal prompt

I'm trying to run a bash script in linux (ubuntu but also fedora) but it the find command won't work.

search=\"*${exten[iterext]}\"
find $direc{iterdir} $r_option -iname $search exec -rm {} \\\;

Now to explain the variables: Exten is array with file extensions read from a text file (no problem here) direc is also an array of directories read from the command line. Iterdir and iterext are cicle integer variables.

Now I have two problems:

1- This find command will not delete or display for that matter if I run it inside a script; however if I put an echo before the find and copy paste the output to a command 开发者_运维技巧prompt find works fine. I've tried the script under ubuntu and fedora so I assume it's not a bash configuration issue. I should note that the issue seems to the $search as I replaced $search with a hardcoded string (like "*txt) and it works inside the script so it's seems to be a quotation issue.

2 - I run that entire find command and also get find:missing argument to '-exec'

Please help :-( it's driving me insane.


Start simple by placing everything in the find command then worry about parameterizing it.

  1. ${exten[iterext]} should be ${exten[$iterext]}
  2. $direc{iterdir} should be ${direc[$iterdir]}
  3. exec -rm should be -exec rm
  4. \\\; should be \;
  5. Quote your variables to prevent word splitting

The following will perform a dry run thanks to the echo. Simply remove the echo when you are satisfied with the output to perform the deletions.

find "${direc[$iterdir]}" "$r_option" -name "*${exten[$iterext]}" -exec echo rm {} \;


Your use of quotes seems a little odd to me. Try this:

find "$direc{iterdir}" $r_option -iname "*${exten[iterext]}" -exec -rm "{}" ";"

Oh, and run your shell script with the -x option. This will print every command line before it is executed.

set -x
find "$direc{iterdir}" $r_option -iname "*${exten[iterext]}" -exec -rm "{}" ";"
set +x
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜