Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Strange BASH behavious

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
funkytwig
Posts: 8
Joined: 2015-08-30 16:04

Strange BASH behavious

#1 Post by funkytwig »

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+='"'
find_cmd+="$backup_name*_${prev_back_type}_*"
find_cmd+='"'
find_cmd+=" -cmin +$cmin -print -exec rm -r {} \;"

echo $find_cmd

$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.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Strange BASH behavious

#2 Post by kiyop »

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
http://kiyoandkei.bbs.fc2.com/

HarborView
Posts: 41
Joined: 2014-02-01 02:21
Location: The Space Coast of the USA

Re: Strange BASH behavious

#3 Post by HarborView »

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

  temp=$BasePath"/.temp"
  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

  temp=$BasePath"/.temp"
  tempfiles=$temp"/*"
  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.

Post Reply