Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to grab value back from external script in bash?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading