473,379 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

outputting a command to the terminal?

Here's my new project: I want to write a little script that I can type
at the terminal like this:

$ scriptname package1 [package2, ...]

where scriptname is my module name and any subsequent arguments are the
names of Linux packages to install. Running the script as above will
create this line:

sudo aptitude install package1 package2 ...

It will run that line at the terminal so the package(s) will be installed.

Now, the extra functionality I want to add (otherwise I would just
install them normally!) is to save the package names to a text file so I
can now the names of programs I've manually installed, if I ever want to
check the list or remove packages.

So creating the proper bash command (sudo aptitude install ...) is easy,
and writing the names to a file is easy. But I have two questions:

1. First of all, does Linux keep track of the packages you manually
install? If so, then I won't have to do this at all.

2. Assuming I write this, how do output the bash command to the
terminal? Is there a particular module that Python uses to interact with
the terminal window that I can use to send the install command to the
terminal?

Thanks.
Aug 13 '06 #1
16 2673
John Salerno schrieb:
Here's my new project: I want to write a little script that I can type
at the terminal like this:

$ scriptname package1 [package2, ...]

where scriptname is my module name and any subsequent arguments are the
names of Linux packages to install. Running the script as above will
create this line:

sudo aptitude install package1 package2 ...

It will run that line at the terminal so the package(s) will be installed.

Now, the extra functionality I want to add (otherwise I would just
install them normally!) is to save the package names to a text file so I
can now the names of programs I've manually installed, if I ever want to
check the list or remove packages.

So creating the proper bash command (sudo aptitude install ...) is easy,
and writing the names to a file is easy. But I have two questions:

1. First of all, does Linux keep track of the packages you manually
install? If so, then I won't have to do this at all.

2. Assuming I write this, how do output the bash command to the
terminal? Is there a particular module that Python uses to interact with
the terminal window that I can use to send the install command to the
terminal?

You don't put a command to the terminal. The shell executes commands.
But it is mainly just a program itself - it can spawn subprocesses and
make these execute the actual commands. so - the module you need is most
probably subprocess.

Diez
Aug 13 '06 #2
John Salerno wrote:
1. First of all, does Linux keep track of the packages you manually
install? If so, then I won't have to do this at all.
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

Generally, after the initial installation, all subsequent operations are
either updates of existing packages or packages you installed manually.
Only rarely do you get new packages installed automatically as a result
of an additional dependency from an original automatically installed
package.

If you know when you completed your initial installation, you can easily
parse the log files to determine what else was installed after that.
2. Assuming I write this, how do output the bash command to the
terminal? Is there a particular module that Python uses to interact with
the terminal window that I can use to send the install command to the
terminal?
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.
Aug 13 '06 #3
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. :)
Aug 14 '06 #4

John Salerno wrote:
>
I think I'll take a look at the subprocess module, just for fun. :)
.... and for learning too :-)

Also, consider that some operating system commands are built into the
shell (i.e. not run as a separate process), which makes using the
subprocess module a bit difficult -- for more fun and learning, check
out os.system()

Cheers,
John

Aug 14 '06 #5
Dennis Lee Bieber wrote:
On Sun, 13 Aug 2006 20:21:26 -0400, John Salerno
<jo******@NOSPAMgmail.comdeclaimed the following in comp.lang.python:
>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.
Pardon, between when?
Wow, I can't believe how time goes. Aug. 5 *was* the first day! I knew I
had installed it a week ago, but I was thinking it was the last Saturday
in July, not Aug. 5 already!
>
August 5 was "last Saturday" if you ignore "yesterday" (well, since
my watch says it is now Monday... "day before last").

I'd guess the "May 31" entries are those that were "snapshots" of
the OS installer date. August 5, first Saturday in the month, might be
the first non-standard installed package.
>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).
Well, I don't do shell scripts either, but... looking at the
sample... "$@" is likely the shell equivalent of Python's sys.argv -- or
*sys.argv if passed down
Aug 14 '06 #6
John Salerno wrote:
Here's my new project: I want to write a little script that I can type
at the terminal like this:

$ scriptname package1 [package2, ...]

where scriptname is my module name and any subsequent arguments are the
names of Linux packages to install. Running the script as above will
create this line:

sudo aptitude install package1 package2 ...

It will run that line at the terminal so the package(s) will be installed.

Now, the extra functionality I want to add (otherwise I would just
install them normally!) is to save the package names to a text file so I
can now the names of programs I've manually installed, if I ever want to
check the list or remove packages.

So creating the proper bash command (sudo aptitude install ...) is easy,
and writing the names to a file is easy. But I have two questions:

1. First of all, does Linux keep track of the packages you manually
install? If so, then I won't have to do this at all.

2. Assuming I write this, how do output the bash command to the
terminal? Is there a particular module that Python uses to interact with
the terminal window that I can use to send the install command to the
terminal?

I don't know the answer to the first bit here, but I think the following
should get you most of what you want as far as the second bit is concerned:
---------------------------- scriptname.py ----------------------------
import argparse # http://argparse.python-hosting.com/
import subprocess
import sys

def outputfile(filename):
return open(filename, 'w')

if __name__ == '__main__':
# parse the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('packages', metavar='package', nargs='+',
help='one of the packages to install')
parser.add_argument('--save', type=outputfile, default=sys.stdout,
help='a file to save the package names to')
namespace = parser.parse_args()

# call the command
command = ['sudo', 'aptitude', 'install'] + namespace.packages
subprocess.call(command)

# write the package name file
for package_name in namespace.packages:
namespace.save.write('%s\n' % package_name)
-----------------------------------------------------------------------
$ scriptname.py -h
usage: scriptname.py [-h] [--save SAVE] package [package ...]

positional arguments:
package one of the packages to install

optional arguments:
-h, --help show this help message and exit
--save SAVE a file to save the package names to

STeVe
Aug 14 '06 #7
Steven Bethard wrote:
John Salerno wrote:
>Here's my new project: I want to write a little script that I can type
at the terminal like this:

$ scriptname package1 [package2, ...]

where scriptname is my module name and any subsequent arguments are
the names of Linux packages to install. Running the script as above
will create this line:

sudo aptitude install package1 package2 ...

It will run that line at the terminal so the package(s) will be
installed.

Now, the extra functionality I want to add (otherwise I would just
install them normally!) is to save the package names to a text file so
I can now the names of programs I've manually installed, if I ever
want to check the list or remove packages.

So creating the proper bash command (sudo aptitude install ...) is
easy, and writing the names to a file is easy. But I have two questions:

1. First of all, does Linux keep track of the packages you manually
install? If so, then I won't have to do this at all.

2. Assuming I write this, how do output the bash command to the
terminal? Is there a particular module that Python uses to interact
with the terminal window that I can use to send the install command to
the terminal?


I don't know the answer to the first bit here, but I think the following
should get you most of what you want as far as the second bit is concerned:
---------------------------- scriptname.py ----------------------------
import argparse # http://argparse.python-hosting.com/
import subprocess
import sys

def outputfile(filename):
return open(filename, 'w')

if __name__ == '__main__':
# parse the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('packages', metavar='package', nargs='+',
help='one of the packages to install')
parser.add_argument('--save', type=outputfile, default=sys.stdout,
help='a file to save the package names to')
namespace = parser.parse_args()

# call the command
command = ['sudo', 'aptitude', 'install'] + namespace.packages
subprocess.call(command)

# write the package name file
for package_name in namespace.packages:
namespace.save.write('%s\n' % package_name)
-----------------------------------------------------------------------
$ scriptname.py -h
usage: scriptname.py [-h] [--save SAVE] package [package ...]

positional arguments:
package one of the packages to install

optional arguments:
-h, --help show this help message and exit
--save SAVE a file to save the package names to

STeVe
yikes! I'll have to take some time to study this! I appreciate it. :)
Aug 14 '06 #8
Dennis Lee Bieber wrote:
Well, I don't do shell scripts either, but... looking at the
sample... "$@" is likely the shell equivalent of Python's sys.argv -- or
*sys.argv if passed down
Yeah, kinda equivalent to *sys.argv[1:].
Aug 14 '06 #9
Steven Bethard wrote:
import argparse # http://argparse.python-hosting.com/
import subprocess
import sys
Why not the standard lib's optparse?
Aug 14 '06 #10
John Salerno wrote:
Steven Bethard wrote:
>---------------------------- scriptname.py ----------------------------
import argparse # http://argparse.python-hosting.com/
import subprocess
import sys

def outputfile(filename):
return open(filename, 'w')

if __name__ == '__main__':
# parse the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('packages', metavar='package', nargs='+',
help='one of the packages to install')
parser.add_argument('--save', type=outputfile, default=sys.stdout,
help='a file to save the package names to')
namespace = parser.parse_args()

# call the command
command = ['sudo', 'aptitude', 'install'] + namespace.packages
subprocess.call(command)

# write the package name file
for package_name in namespace.packages:
namespace.save.write('%s\n' % package_name)
-----------------------------------------------------------------------
$ scriptname.py -h
usage: scriptname.py [-h] [--save SAVE] package [package ...]

positional arguments:
package one of the packages to install

optional arguments:
-h, --help show this help message and exit
--save SAVE a file to save the package names to


yikes! I'll have to take some time to study this! I appreciate it. :)

For just calling the command, the important lines are::

command = ['sudo', 'aptitude', 'install'] + namespace.packages
subprocess.call(command)

where you could have probably used ``sys.argv[1:]`` instead of
namespace.packages.
For writing the file, as I'm sure you've already figured out, the
important lines are::

for package_name in namespace.packages:
namespace.save.write('%s\n' % package_name)

where again, if you weren't using argparse, you could have used
``sys.argv`` to determine the package names (namespace.packages) and the
file to write to (namespace.save).
The remaining lines involving the ``parser`` object are basically
defining a command line interface in a similar way to what optparse in
the stdlib does. Sure, you could do all of this by fiddling with
sys.argv, but the argparse module will do all the parsing and
conversions for you, and give your script a meaningful usage message.
And I'm a firm believer in meaningful usage messages. =)

STeVe

P.S. Thank *you* for posting this. As a result, I've been convinced
that argparse should grow a 'outfile' type, something I've been debating
with myself about for a while now.
Aug 14 '06 #11
Steven Bethard wrote:
P.S. Thank *you* for posting this. As a result, I've been convinced
that argparse should grow a 'outfile' type, something I've been debating
with myself about for a while now.
Heh heh. I'm glad my ignorance can inspire those around me. ;)
Aug 14 '06 #12
Yu-Xi Lim wrote:
Steven Bethard wrote:
>import argparse # http://argparse.python-hosting.com/
import subprocess
import sys

Why not the standard lib's optparse?
The page referenced above gives a variety of reasons, but the two most
important things in this example are: argparse supports parsing of both
positional and optional arguments, and argparse generates better usage
messages.

Since argparse supports positional arguments, I can write something like::

parser.add_argument('packages', ..., nargs='+', ...)

and then the arparse module will enforce that at least one positional
argument was given. With optparse, you'd do something like:

options, args = parser.parse_args()
if not args:
parser.error('wrong number of arguments')

Basically, with optparse, anything that involves positional arguments
has to be handled by the user.

It's also worth pointing out the better usage messages. Notice that the
output looked like::

$ scriptname.py -h
usage: scriptname.py [-h] [--save SAVE] package [package ...]

positional arguments:
package one of the packages to install

optional arguments:
-h, --help show this help message and exit
--save SAVE a file to save the package names to

With the optparse, you'd get something like::

$ scriptname.py -h
usage: scriptname.py [OPTIONS]

options:
-h, --help show this help message and exit
--save SAVE a file to save the package names to

The argparse module knows how to create a meaningful usage message
instead of just "%prog [OPTIONS]", and the argparse module knows about
positional arguments, so you can have help messages for them too.

Ok, enough propaganda for now. ;-)

STeVe
Aug 14 '06 #13
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

Aug 14 '06 #14
Simon Forman wrote:
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)
Thanks for the info! I might grudgingly decide to use a bash script in
this case. :)

And yes, it seems every time I ask a Linux question, everyone points me
to a man page. Sometimes I find them difficult to decipher, but they are
still a great help.
Aug 14 '06 #15
John Salerno wrote:
Simon Forman wrote:
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)

Thanks for the info! I might grudgingly decide to use a bash script in
this case. :)
You're welcome. lol :)
And yes, it seems every time I ask a Linux question, everyone points me
to a man page. Sometimes I find them difficult to decipher, but they are
still a great help.
Yup. When I started with Linux, man pages were one of those things I
resisted... until I started actually reading them. Then I kicked
myself for not starting sooner. :)

Neat trick: man -k <search term>
Shows you man pages matching your search term (I think it's the same
thing as the "apropos" command.) It searches the command names and
summary lines.

Peace,
~Simon

Aug 14 '06 #16
Simon Forman wrote:
Neat trick: man -k <search term>
Ah, so much command line magic to learn! Maybe I should just go back to
Windows....or maybe not. ;)
Aug 14 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Avi Berkovich | last post by:
Hello, I was unable to use popen2.popen4 to grab python.exe's (2.3) output, for starts, it doesn't show the version information at the beginning and won't return anything when writing to the...
3
by: Tuang | last post by:
I'd like to create my own mini "IDE" for working with several programming languages that provide interactive "toplevel" command line interpreters, such as Python, Ruby, Lisp, Scheme, OCaml, etc....
5
by: Good Man | last post by:
Hi there I am trying to execute a custom-built java program on my linux server via PHP. Basically, a user uploads files via PHP, and then the java program performs some action on these files. ...
4
by: fivestars | last post by:
Hi there. I'm computer science student at the end of my degree. I'm new about python. I've a question for all of you. Do you know how to write, from python code, on a unix(linux) terminal...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
17
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the...
3
sokoun
by: sokoun | last post by:
Hi all, I am wondering whether we can start terminal by using command line or not? if we can what is the command to open the terminal process? for example if we want to run Konqueror we type...
0
by: dinesh1440 | last post by:
I want to execute all the command that I can execute in command prompt or terminal through PHP. exec() and system() work only for some of the commands. I am working in linux. I installed offline...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.