Strange BASH behavious

Need help with C, C++, perl, python, etc?

Strange BASH behavious

Postby funkytwig » 2015-08-30 16:09

Hi, in a lot of the bash scripts I do I load the command I want to run, echo it to a logfile and run it. This has worked fine up to now but I am having a problem with a find -exec. Here is the code
Code: Select all
find_cmd="find -maxdepth 1 -type d -name "
find_cmd+=" -cmin +$cmin -print -exec rm -r {} \;"

echo $find_cmd


And here is what I get when I run it:

Code: Select all
find -maxdepth 1 -type d -name "bash*_H_*" -cmin +60 -print -exec rm -r {} \;
find: missing argument to `-exec'

Aon the strange thing, if I simply cut and paste 'find -maxdepth 1 -type d -name "bash*_H_*" -cmin +60 -print -exec rm -r {} \;' to the shell it works fine.
Posts: 2
Joined: 2015-08-30 16:04

Re: Strange BASH behavious

Postby kiyop » 2015-09-01 13:41

Maybe you can do as follows, although I do not know the reason:
Code: Select all
bash -c "$find_cmd"
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
User avatar
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan

Re: Strange BASH behavious

Postby HarborView » 2015-09-08 13:12

The reason is there are bugs in Bash when you have characters like [ or { or \ or / in a string.

By definition these should not happen but they do. Here is an example from a program I was working on yesterday. The project has a folder for a batch of temporary files. Each time the program is run the folder has to be cleaned out. (No, the system /tmp will not work for this purpose.)

Note: Because these are errors in the language we should always say which version it is. I am using Bash 4.2(37).

Anyway, we enter my program after the location of the temporary folder has is stored in a variable named $temp:
Code: Select all
  rm $temp"/*"

That is rejected wth an error message. But it works in the command line.

After too much time I finally gave it this syntax:
Code: Select all
  rm $tempfiles

That works OK.

(Disclosure: Actually the program has rm -f $tempfiles so it won't annoy me with an error message that the folder is already empty. I simplified to illustrate. The above examples work the same with or without the -f option.)

Possibly changing the order of your arguments to }{\; would make a difference.
Posts: 38
Joined: 2014-02-01 02:21
Location: The Space Coast of the USA

Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests