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)?

3 Answers 3

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.

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.

3
  • I was a bit confused before the edit!
    – Arronical
    Jul 23, 2015 at 15:58
  • 1
    @Arronical Yes, I'm know. I'm on mobile, that's difficult :)
    – A.B.
    Jul 23, 2015 at 15:59
  • I've tried using a phone a couple of times, tough stuff ;)
    – Arronical
    Jul 23, 2015 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .