1

I'm having difficulties passing arguments to a embedded bash script.

 #!/bin/bash/
 function my_function() {
 MYPARSER="$1" python - <<END
 <<Some Python Code>>

 class MyParser(OptionParser):
 def format_epilog(self, formatter):
    return self.epilog

 parser=MyParser(version=VER, usage=USAGE, epilog=DESC)

 parser.add_option("-s", "--Startdir", dest="StartDir", 
               metavar="StartDir"
             )
 parser.add_option("-r", "--report", dest="ReportDir", 
               metavar="ReportDir"
             )
<<More Python Code>>

 END
 }

 foo="-s /mnt/folder -r /storagefolder/"
 my_function "$foo"   

I've read Steve's Blog: Embedding python in bash scripts which helped but I'm still unable to pass the argument. I've tried both parser and myparser as environmental variables.

Is it as simple as defining $2 and passing them individually?

Thanks

7
  • Where in this code are you even trying to read the value? Commented Jun 25, 2015 at 21:32
  • @CharlesDuffy - I am working with a completed python script (a co-workers) but because of portability need to embed into a bash script I wrote. The python script requires me to declare -s and -r to run. I had thought (perhaps wrongly) that when I called the my_function with the variable it would pass it as "python script -s /mnt/folder -r /storagefolder". Commented Jun 25, 2015 at 21:34
  • Maybe that's helpful: stackoverflow.com/a/24897057/3776858 Commented Jun 25, 2015 at 21:36
  • Now, as a separate issue, don't ever pass store an argument list in a scalar string variable (which you're doing here). Argument lists belong in arrays, and yes, bash supports them. Commented Jun 25, 2015 at 21:36
  • As an aside -- using the function keyword in bash is bad form: It's incompatible with POSIX sh, but -- unlike most of bash's incompatibilities -- gives you absolutely no improvement in expressiveness or functionality over the POSIX-standard syntax. Just use myfunc() {, not function myfunc() {. Commented Jun 25, 2015 at 21:40

1 Answer 1

4

You're overcomplicating this rather a lot. Why mess with a parser where

value="hello" python -c 'import os; print os.environ["value"]'

Or, for a longer script:

value="hello" python <<'EOF'
import os
print os.environ["value"]
EOF

If you need to set sys.argv for compatibility with existing code:

python - first second <<<'import sys; print sys.argv'

Thus:

args=( -s /mnt/folder -r /storagefolder/ )
python - "${args[@]}" <<'EOF'
import sys
print sys.argv # this is what an OptionParser will be looking at by default.
EOF
Sign up to request clarification or add additional context in comments.

2 Comments

How would you pass multiple values?
The same way you pass a single value. a=b c=d python ... puts both a and c in os.environ. And the args example already passes multiple values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.