Home > Mobile >  How to grab value back from external script in bash?
How to grab value back from external script in bash?

Time:03-09

I'm sure I'm missing something stupid. I want to pass a full path variable to a perl script, where I do some work on it and then pass it back. So I have:

    echo "Backing up: $f ";
    $write_file="$(perl /home/spider/web/foo.com/public_html/gen-path.cgi $f)";
    echo "WRITE TO: $write_file \n";

However, this gives me:

Backing up: /home/spider/web/foo.com/public_html/websites-uk/uk/q/u
backup-files-all.sh: line 7: =backup-uk-q-u.tar.gz: command not found
WRITE TO:  \n

I can't work out why its not saving the output into $write_file. I must be missing something (bash isn't my prefered language, which is why I'm passing to Perl as I'm a lot more fluent in that :))

CodePudding user response:

Unless your variable write_file already exists, the command $write_file="something" will translate to ="something"(1).

When setting a variable, leave off the $ - you only need it if you want the value of the variable.

In other words, what you need is (note no semicolons needed):

write_file="$(perl /home/spider/web/foo.com/public_html/gen-path.cgi $f)"

(1) It can be even hairier if it is set to something. For example, the code:

write_file=xyzzy
$write_file="something"

will result in something being placed into a variable called xyzzy, not write_file :-)

  •  Tags:  
  • bash
  • Related