LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2005, 11:34 AM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 48
how to execute sfdisk command quietly


I am writing a shell script in which a certain portion of the script has the following lines

echo '0,
'\
| sfdisk -L /dev/hdc


The above command basically partitions the IDE device 'hdc'. The above commands works perfectly but it produces a bunch of text(help, options) on the screen also. I don't want all that to appear on the screen. In short I want to execute the command in background or something like that so that on the terminal windows I can output my personal message while it is partitioning the drive like echo "Please wait, while your IDE device is partitioned."

Thanks
 
Old 09-16-2005, 11:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi,

If you do not want any output append this:

1>/dev/null 2>&1

I.e.:

echo '0,
'\
| sfdisk -L /dev/hdc 1>/dev/null 2>&1


Both the normal (stdout,1) and error (stderr,2) messages are rerouted to /dev/null instead of outputting it to your screen.

Hope this helps.

Last edited by druuna; 09-16-2005 at 11:48 AM.
 
Old 09-16-2005, 12:06 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Quote:
Originally posted by druuna
Hi,

If you do not want any output append this:

1>/dev/null 2>&1

I.e.:

echo '0,
'\
| sfdisk -L /dev/hdc 1>/dev/null 2>&1


Both the normal (stdout,1) and error (stderr,2) messages are rerouted to /dev/null instead of outputting it to your screen.

Hope this helps.

Thanks it worked!!!! A further extention to this question is suppose there is an error while running that command. How can I detect that there is an error and make my program quit after displaying that error.

Does all the commands in linux returns some value like '0' for successful exection?

Or is there any 'try catch finally' error checking thing in shell scripting like C++ and C#


If anyone can provide me an example that would be great.

Kushal
 
Old 09-16-2005, 12:33 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi,

All programs produce an exit status. 0 is ok, something else is not ok.
You can check the $? variable, immediately after a command has executed.

I.e.:

$ ls /tmp

... output ....

$ echo $?
0

Or

$ ls /FUBAR
ls: /FUBAR: No such file or directory

$ echo $?
1

Appending something like 1>/dev/null does not change the output of $?.

Another way of approaching this is to put the output of stderr in a file, which you can work with.

I.e.:

echo '0,
'\
| sfdisk -L /dev/hdc 1>/dev/null 2>/tmp/progname.errors


Normal output goes to /dev/null and all errors go to /tmp/progname.errors.

Hope this helps.
 
Old 09-16-2005, 01:19 PM   #5
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Quote:
Originally posted by druuna
Hi,

All programs produce an exit status. 0 is ok, something else is not ok.
You can check the $? variable, immediately after a command has executed.

I.e.:

$ ls /tmp

... output ....

$ echo $?
0

Or

$ ls /FUBAR
ls: /FUBAR: No such file or directory

$ echo $?
1

Appending something like 1>/dev/null does not change the output of $?.

Another way of approaching this is to put the output of stderr in a file, which you can work with.

I.e.:

echo '0,
'\
| sfdisk -L /dev/hdc 1>/dev/null 2>/tmp/progname.errors


Normal output goes to /dev/null and all errors go to /tmp/progname.errors.

Hope this helps.

Wow! that was a pretty easy one but difficult to know. Thanks a lot once again!!!!

You have helped me to over come my two important hurdles.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
execute command from c code alaios Programming 3 06-06-2005 05:26 AM
Cooling dual athlons quietly mastahnke Linux - Hardware 18 09-27-2004 12:19 AM
command execute without ./ kaon Slackware 11 09-15-2004 05:14 PM
Execute Command lawrencegoodman Linux - Newbie 2 10-23-2003 08:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration