2

Following on from this question I've used the following command in a bash script:

find ${svn_root} -maxdepth 2 -type d -exec bash -c 'if svnlook info "$1" &>/dev/null ;then echo"$1" >>${log_file} ;svnadmin verify "$1" 2>>{log_file} ;fi' _ {} \;

Both variables are defined at the beginning of the script. The variable $svn_root is appropriately expanded, but the $log_file variable is not.

I see that the variable is not passed into any of the -exec bash -c command, I assume that's because it's a subshell? I can slip log_file=/path/to/log ; in front of the echo part of the line, but that kind of hard codes that part of the command, if I change the variable in the script without changing it within the find command I'll be outputting to two separate log files!

Can I import/export that variable into that subshell (if that's what it is)?

Arronical
  • 19,893

3 Answers3

4

There are mainly two ways:

  1. Pass it as a second argument to the shell

    find ... -exec bash -c '...' _ {} "$log_file" \;
    

And then you use "$2" instead of ${log_file} inside the script

  1. Pass it via the environment:

    log_file=$log_file find ... -exec bash -c '...' _ {} \;
    

BTW, it's not a subshell.

geirha
  • 46,101
2

You should be able to see such variables in your subshell if you used double quotes with bash -c "".

A simpler method would be to just export the log_file variable in your main script as follow:

export log_file

Source: How to “send” variable to sub-shell?

2

Using

find ${svn_root} -maxdepth 2 -type d -exec bash -c 'if svnlook info "$0" &>/dev/null ;then echo"$0" >> "$1" ;svnadmin verify "$0" 2>> "$1" ;fi' {}  "$log_file" \;

You need $0 and $1, because this is not a function or a script.

A.B.
  • 90,397