Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Hide program output from terminal

by Milamber (Beadle)
on Sep 15, 2006 at 07:29 UTC ( [id://573060]=perlquestion: print w/replies, xml ) Need Help??

Milamber has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I'm playing with a script on Ubuntu that switches my interface configuration between an interface.dynamic file which allows my network card to use DHCP, and an interface.static file which contains a list of virtual interfaces that I use for various networks and devices I connect to frequently.

Running
system('/etc/init.d/networking stop'); system('cp /this/file /to/there'); system('/etc/init.d/networking start');
works like a charm. However, I want to hide the output from /etc/init.d/networking/start from the terminal, and just display my success or failure message. I've tried directing STDOUT to /dev/null, to no avail (and probably not surprisingly). My Cookbook has a recipe for using IPC::Open3, which I think might do what I want once I suss out what I'm doing, but I was wondering if there's another way of doing this that I am missing in my ignorance. The point of this script is, by the by, simply because I can and because it is more fun than the simple shell script I have to accomplish this task already.

My thanks in anticipation of your forthcoming wisdom ;)

Update:
Considering English is my only language, I really shouldn't have to edit out the mistakes I've made after being given a preview.

Replies are listed 'Best First'.
Re: Hide program output from terminal
by haoess (Curate) on Sep 15, 2006 at 08:03 UTC

    See perldoc -f open for playing with STDOUT and its friends.

    # save STDOUT open my $stdout, '>&STDOUT'; # close it open STDOUT, '>', undef; # don't display system's STDOUT (but it's STDERR!) system '...'; # reopen STDOUT open STDOUT, '>&', $stdout;

    --Frank

Re: Hide program output from terminal
by McDarren (Abbot) on Sep 15, 2006 at 08:02 UTC
    Try something like this:
    print "Stopping network...\n"; system('/etc/init.d/network stop 1>/dev/null 2>&1'); print "Starting network...\n"; system('/etc/init.d/network start 1>/dev/null 2>&1');

    2>&1 redirects STDOUT to the same place that STDERR is going, ie. to /dev/null.

    Update: However, you probably should think about doing something useful with STDOUT and STDERR, such as redirecting the output to a logfile. That way, you have somewhere to check if something goes wrong.

    Hope this helps,
    Darren :)

      Excellent. This worked like a charm. Looks like a lot of what I do on my notebook is now going to be turned into a Perl script :) Thank you.
Re: Hide program output from terminal
by Milamber (Beadle) on Sep 15, 2006 at 08:38 UTC
    My thanks to you both for your help. Much appreciated.
Re: Hide program output from terminal
by Tanktalus (Canon) on Sep 15, 2006 at 16:01 UTC

    I know this isn't quite your question, but I'd recommend writing perl code in perl, rather than shell in perl. ;-) For example, use the File::Copy module to copy your file. Otherwise you may as well write this in shell - it would be just a bit simpler that way ;-)

    (I know that when I started perl, I didn't know about File::Copy, so thought you may want to know about it - it is standard in all perls I've used that I recall.)

      Noted. It probably would make more sense. I know more shell than Perl at the moment, so everything I've written so far is a nasty munge between the two. Thanks for the link ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://573060]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-20 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found