DaleInnis
15 years ago
Geeky Linux/bash/Python question that I'm bringing to Plurk 'cause no one else seems to know:
latest #43
jjacek
15 years ago
stands ready. 8-)
Michele shares
15 years ago
whoppers candy in the wait of the question
DaleInnis
15 years ago
How do I make a bash script (or any other shell script for that matter) that passes its arguments *completely unchanged* to a python script?
立即下載
DaleInnis
15 years ago
So that typing "shellscriptname stuff" does exactly what "python /long/complex/path/pythonscriptname.py stuff" does...
DaleInnis
15 years ago
... even if "stuff" has like quotation marks and dots and stars and whatnot init?
DaleInnis has
15 years ago
the impression so far that it's not actually possible, sigh...
[Codie ~]#
15 years ago
i did that for Perl before...
[Codie ~]#
15 years ago
something like: exec (path to python) %1
jjacek says
15 years ago
When you enter a command with stars, the shell will usually expand them before the script sees them, unless you escape or 'quote' them.
[Codie ~]#
15 years ago
$# is apparently any number of arguments
[Codie ~]#
15 years ago
try and do echo $# in your bash script, then if it works, it should be able to pass them to your python executable using exec
jjacek says
15 years ago
E.g. this command will print "*?": echo '*?' -- or this would do the same: echo \*\?
jjacek says
15 years ago
That's the hard part. The script itself is simple: python /long/complex/path/pythonscriptname.py "$@"
Honour says
15 years ago
I've always found chocolate to be helpful!
jjacek
15 years ago
"$@" expands into all the args that were passed to the script, but each one is put in quotes to preserve grouping, which is usually best
Honour says
15 years ago
or failing that - a baseball bat! :-)
jjacek says
15 years ago
To clarify: use the single quotes when you're typing in the command (shellscriptname 'stuff'), and use "$@" in the script.
DaleInnis
15 years ago
Yeah, that was one of the things we tried. Doesn't work, though, when there are double-quotes containing blanks in the argument string.
DaleInnis
15 years ago
... and you have to get users to remember to put single quotes around everything. :-)
DaleInnis
15 years ago
... I mean, when there are double-quoted scripts containing blanks. You know...
DaleInnis
15 years ago
like say:
DaleInnis
15 years ago
whatever -a "foo bar"
DaleInnis
15 years ago
if I do scriptname ' -a "foo bar" '
DaleInnis
15 years ago
it arrives at the actual script weirdly tokenized, and causes an error, whereas
DaleInnis
15 years ago
/path/to/actual/script -a "foo bar"
DaleInnis
15 years ago
works just fine.
DaleInnis is
15 years ago
amazed at how hard this is. :-) I mean, isn't this the very first thing one wants to write a script *for*? To just function as an alias?
Phoenix59
15 years ago
8-)
jjacek
15 years ago
(thinking) Have you considered just making an alias? alias scriptname="/path/to/actual/script"
DaleInnis
15 years ago
Nods. Should probably consider that approach; user will have to run an "install program" or something to set up alias, rather than just...
DaleInnis
15 years ago
... copying files around, but may be the best solution.
DaleInnis has
15 years ago
had a certain amount of success using Python script that passes arglist along to real script via Popen. shell still expands "*", but...
DaleInnis
15 years ago
... everything else seems to work.
DaleInnis
15 years ago
goes off to try alias approach. :-)
Tiessa says
15 years ago
you can't do what you want - the shell you type the command into will always do things like expansions, matching, etc.
Tiessa says
15 years ago
to the arguments, I don't know of a way to get what the shell saw before it mangled it
DaleInnis
15 years ago
agrees, and is thinking more an' more that a shell alias is the way to go; no pre-mangling occurs there I don't think.
Lexi is
15 years ago
getting a headache trying to solve this. :-)
Michele shares
15 years ago
more chocolate :-)
Lexi likes
15 years ago
Michele now and nomnoms while flipping through her bash and korn shell books
Lexi has
15 years ago
just cracked it .. yes it is possible :-D
Lexi shares
15 years ago
The code!, perhaps sorta ugly, but it does actually work. :-) That was fun. :-)
DaleInnis
15 years ago
Thanks, Lexi! I may actually use that. Or the Python equivalent. Or a shell alias... :-)
back to top