outputting a command to the terminal?

Simon Forman rogue_pedro at yahoo.com
Mon Aug 14 14:29:35 EDT 2006


John Salerno wrote:
> Yu-Xi Lim wrote:
>
> > I assume you're using a Debian-based distro with aptitude as the front
> > end. In which case, all dpkg operations should be logged in
> > /var/log/dpkg.log
>
> Yes, I'm using Ubuntu. But I checked this log file and I'm a bit
> confused. It has a lot of listings for 5-31-06, but I didn't even
> install Linux until last Saturday. The next date after 5-31 is 8-5-06,
> and I know I installed things between last Saturday and Aug. 5.
>
> (But this is OT, so don't worry about it.)
>
> > I'm wondering about the need to "output the bash command to the
> > terminal". It would probably suffice if your Python script just spawned
> > an instance of the shell with the necessary command line. Take a look at
> > the subprocess module.
> >
> > But this really calls for a bash script:
> >
> > #!/bin/bash
> > echo $@ >> /path/to/manual_install.log
> > sudo aptitude install $@
> >
> >
> > Shorter than the equivalent Python code. You could probably declare this
> > as a function in your bash initialization files too, if you know how to
> > do this.
>
> Hmm, interesting. I figured I could do this with a bash script, but I
> don't know bash at all and I'm trying to stick with Python. I don't
> quite understand your bash script (not familiar with the $@ syntax).
>
> I think I'll take a look at the subprocess module, just for fun. :)

Hey John, Yu-Xi Lim's right.  This is one of those (thankfully few)
cases where bash makes more sense to use than python (at least IMHO.)

To figure out about that $@, fire up your teminal and type "man bash"
("!man bash" in IPython) (BTW, apropos of nothing, "man bash" is one of
my all time favorite commands ever.  I always think of some comic-book
hero/monster shouting it, "MAN BASH!!" lol.  Anyway...)

So, now you're looking at the man page for bash.  It's very very long
and ubergeeky.  Deep and amazing mysteries are contained (and kind of
explained) within it.  You want information on $@ so we'll use the
search incantation to find and reveal it.

Type "/\$@" without the quotes, then press return.  (What this
means/does: "/" is the manpage search command, it uses a regular
expression syntax not dissimilar to python's own. "\" escapes the next
character ("$", in this case) and we need to do that because "$" is
regular expression syntax for "end of line".  The "$@" will now match,
um, "$@" correctly.)

Once you press return, man will scroll to put the first occurance of
"$@" at the top of your terminal and highlight it.  On my system it's
this line (I narrowed my terminal so that quoted portions wouldn't wrap
badly in this posting):

       "$@"  as  explained  below  under Special Parameters.

So far so good, '"$@" as explained below' looks promising.  Rather than
scrolling down to find this "Special Parameters" section, let's keep
using the search.

Press "n" to scroll to the next occurance of our pattern "$@".  On my
system this brings me to:

              separate word.  That is, "$@" is equivalent to
              "$1" "$2" ...  If the double-quoted  expansion

Ah ha!  Scrolling up a few lines, we see:

       @      Expands to the positional parameters, starting
              from  one.   When  the expansion occurs within
              double quotes, each  parameter  expands  to  a
              separate word.  That is, "$@" is equivalent to
              "$1" "$2" ...  If the double-quoted  expansion
              occurs  within  a  word,  the expansion of the
              first parameter is joined with  the  beginning
              part  of  the original word, and the expansion
              of the last parameter is joined with the  last
              part  of the original word.  When there are no
              positional parameters, "$@" and $@  expand  to
              nothing (i.e., they are removed).

Not extraordinarily enlightening, maybe, but better than sitting in the
dark, lighting your farts. :-D   (Hit "q" to exit man.)

Basically what this means is that $@ will become the positional
arguments that you pass to your script.  You can play with this by
writing a simple bash script like this

#!/bin/bash
echo $@

and passing it args to see what it echos. (Remember to chmod +x it..)

So, long story short, Yu-Xi Lim's bash script echos your package names
to the /path/to/manual_install.log file (">>" in bash means "append the
output of the command to the left to the file on the right",) then it
calls aptitude with those same package names.

It's simple, short, and to-the-point.  The equivalent python script
would be much longer, for no appreciable gain.  I write most of my tiny
little helper scripts in python, but in this case, bash is the clear
winnar.  (And on *nix. man pages are your best friend. Plus you get to
feel all l33t when you grok them. lol)

Peace,
~Simon




More information about the Python-list mailing list