473,765 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2720
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******@NOSPA Mgmail.comdecla imed the following in comp.lang.pytho n:
>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(file name):
return open(filename, 'w')

if __name__ == '__main__':
# parse the command line arguments
parser = argparse.Argume ntParser()
parser.add_argu ment('packages' , metavar='packag e', nargs='+',
help='one of the packages to install')
parser.add_argu ment('--save', type=outputfile , default=sys.std out,
help='a file to save the package names to')
namespace = parser.parse_ar gs()

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

# write the package name file
for package_name in namespace.packa ges:
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(file name):
return open(filename, 'w')

if __name__ == '__main__':
# parse the command line arguments
parser = argparse.Argume ntParser()
parser.add_argu ment('packages' , metavar='packag e', nargs='+',
help='one of the packages to install')
parser.add_argu ment('--save', type=outputfile , default=sys.std out,
help='a file to save the package names to')
namespace = parser.parse_ar gs()

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

# write the package name file
for package_name in namespace.packa ges:
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
Steven Bethard wrote:
import argparse # http://argparse.python-hosting.com/
import subprocess
import sys
Why not the standard lib's optparse?
Aug 14 '06 #9
John Salerno wrote:
Steven Bethard wrote:
>---------------------------- scriptname.py ----------------------------
import argparse # http://argparse.python-hosting.com/
import subprocess
import sys

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

if __name__ == '__main__':
# parse the command line arguments
parser = argparse.Argume ntParser()
parser.add_argu ment('packages' , metavar='packag e', nargs='+',
help='one of the packages to install')
parser.add_argu ment('--save', type=outputfile , default=sys.std out,
help='a file to save the package names to')
namespace = parser.parse_ar gs()

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

# write the package name file
for package_name in namespace.packa ges:
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.packa ges
subprocess.call (command)

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

for package_name in namespace.packa ges:
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.pack ages) 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 #10

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

Similar topics

6
3583
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 stdin pipe, it seems that if I give it some error nous expression, the pipe would return the exception data, though nothing else comes through. A friend of mine also tried this using win32api on delphi, and got the same result.
3
4217
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. (For those who haven't played with them, these languages -- unlike C, Java, C#, or Perl -- are designed to let you carry on an interactive conversation with them, so you can run each function in your app independently and interactively from a...
5
16967
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. I have successfully had other operations performed on these files by using backticks, ie: $doit = `my exec command`;
4
2009
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 on specified coordinates? And also: is it possible to override, from python code, something on a
4
10513
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 doesn't seem to write to the screen in the way I would expect. The output is:
17
3378
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 programme: printf("\nCommand line arguement %d: %s.", i , argv ); The porgramme outputs 3 of the command line arguements, then gives a segmentation fault on the next line, followed by other strange
3
1330
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 Konqueror in command, so if we want to start the terminal what is the command that we use? sokoun.
0
1278
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 Blast program. I can run this program only from command prompt or terminal and for several hundred files I need to do this. So I want to run this dynamically through programming. But I found that blast program does not work either with exec() or with...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.