How do I pass variable inside find and bash -c?

Hey guys, I'm having issues with passing variable to %exe part of the code. Need your help.

Here is my working code that I use inside bash script:

## Function
targz() {
  find $1 -type f -name "*.$2" -exec \
    bash -c 'old=$(basename {}); new=${old/%exe/tar\.gz}; \
      tar -zcvf $new $old; ' \;
}

## Function Call
## targz [directory] [extension]
targz . 'exe';

and yes I've tried using it some thing like this:

new=${old/%$2/tar\.gz};

but it generates filenames like: file.exetar.gz

Answers


Try:

targz() {
  find $1 -type f -name "*.$2" -exec \
    bash -c 'old=$(basename {}); new=${old/'"$2"'/tar\.gz}; \
      tar -zcvf $new $old; ' \;
}

The trick is to get out of the single quote, so that variable expansion will be performed.


Need Your Help

Android support library v4 and Google Maps API breaks compilation

android eclipse google-maps-api-2 android-support-library

I'm trying to use the Google Maps API v2 in an android project that targets the v10 API.

iOS Send File to Review Application-Loader ( Apple Watch)

application-loader ios8-extension watchkit apple-watch

Can I send the apple watch apps For Review RightNow if Yes which file for review will be sent (Application-Loader) because we have a many file in Products Folder

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.