Bash script undesired output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script undesired output
# 1  
Old 08-13-2008
Bash script undesired output

hi guys,

When for example, I want to get the size of a partition, I have done the following:

# result=`fdisk -l | grep /dev/sda1`
# echo $result
/dev/sda1 1 15000 120487468+ 83 Linux

and then I parse this output to get the size. That's just fine. The problem here is that, when I have a disk that does not have a good filesystem on it, it shows:

# result=`fdisk -l | grep /dev/sda1`
Disk /dev/hdd doesn't contain a valid partition table
#

I don't want to see that error message. I just want to turn it off. How can I do that?

By the way, I have tried redirecting to /dev/null, but no luck!

Thanks

Last edited by alirezan; 08-13-2008 at 08:42 PM..
# 2  
Old 08-13-2008
Probably the fdisk utility sends the error message to fd2, the <stderr>. By using a pipeline you only redirect fd1, the <stdout> but leave <stderr> intact.

Probably this might work (i have changed the old-fashioned backticks, which shouldn't be used any more):

result=$(fdisk -l 2>/dev/null | grep /dev/sda1)

I hope this helps.

bakunin
# 3  
Old 08-13-2008
Using $() instead of the 'old fashioned backticks' won't work on bourne though Smilie

How come they shouldn't be used anymore BTW (geniune question, not stirring like my first comment was Smilie)?
# 4  
Old 08-13-2008
Alright! Thanks alot! it worked!
# 5  
Old 08-13-2008
Quote:
Originally Posted by Smiling Dragon
Using $() instead of the 'old fashioned backticks' won't work on bourne though Smilie
True, but AFAIK Bourne Shell is a bit outdated by now - this is what i meant by "oldfashioned". Its similar to using mono-character variable names in programs because FORTRAN/77 didn't honour more than 6 characters of an identifier. This might be, but nobody in his right mind would use a compiler which is more than 30 years old today.

Quote:
How come they shouldn't be used anymore BTW (geniune question, not stirring like my first comment was Smilie)?
In Korn-Shell, bash and most other modern shells the "$(...)" construct offers more flexibility and better readability (backticks are easily confused with single quotes as several threads here show) and better functionality. The reason why backticks are still working in these shells is not because the authors thought these were desirable but for backward compatibility. This is not a reason to still use them, even more so as the threadstarter seems to be learning shell scripting and should adopt proper practices as early as possible.

For an in-depth discussion about backticks see Barry Rosenbergs "Hands-On ksh93 Programming" or have a look here or take into account that backticks cannot be nested whereas "$(...)" can be, etc..

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script unable to disable expected output

I'm trying to understand why a script behaves different when run through a pipe. My OS: Linux myip 3.13.0-92-generic #139-Ubuntu SMP Tue Jun 28 20:42:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux This script (myscript.sh): #!/bin/bash echo whoami: whoami echo who: who echo who... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

Bash output analysis script

I want to create a script to analyse each output from a task while it is running and launch a sub-function if a specific error message is found at any point or to continue as normal. #!/bin/bash read checker <<< $('Running process') if ; then 'Sub-function' elif "continue as normal" fi (2 Replies)
Discussion started by: 3therk1ll
2 Replies

3. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

4. Shell Programming and Scripting

Undesired removal of white space with awk

Hi, I'm fairly new to scripting and I have a problem that I am having difficulty solving. What I'd like to do is run an awk script to adjust the string in the first field depending on the string in another field. This is best explained with an example: Here is my script: cat... (4 Replies)
Discussion started by: calbrex
4 Replies

5. Shell Programming and Scripting

[Solved] Output in bash script not captured in variable

I'm tring to write down a simple script that would execute a command and wait until it returns a specific result. This is what i did: bjobs_out=`bjobs` while ]; do bjobs_out=`bjobs` sleep 6 done It seems to work until the command 'jobs' return the list of jobs in execution, but... (4 Replies)
Discussion started by: lifedj
4 Replies

6. Shell Programming and Scripting

Bash script show Kill system output

Hi we are calling kill -9 $pid command from bash script it gives below output, but we need to hide the output. i tried /dev/null but ni luck. is there any alternate way to schive this. ../kill_scr.sh: line 42: 1891 Killed /tmp/anr_rest_mul_wc.sh Soalris 10. ... (2 Replies)
Discussion started by: sachinbutala
2 Replies

7. Shell Programming and Scripting

Piping output from a command into bash script

Hi all. I am using procmail to deliver an email to a script I am developing. Procmail delivers the email to the script on standard input. I imagine this is the same as piping input from a command into the script. Hence I've been testing my script by running echo 'test' | sms-autosend-backup.sh ... (2 Replies)
Discussion started by: akindo
2 Replies

8. Shell Programming and Scripting

Reading output from terminal back into bash script

How can I get a bash script to wait and read and count $i messages that a running program (drbl clonezilla) sends to the console (terminal) and only then move on to the next line in the script when the count is matched (the next line is the last line of the script and is a reboot)? The script... (0 Replies)
Discussion started by: dp123
0 Replies

9. UNIX for Dummies Questions & Answers

How do i tell my bash shell script to test the output of a command against something

How do i tell my bash shell script to test the output of the command i'm using?? I want this script to look for lines not equal to 1 then let me know.. $ cat blah ; echo ---- ; cat blah.sh 1 fe 1 fi 1 fo 0 fum 1 blahda 1 blah 0 blahh 1 bla 1 bl 1 blahhh ---- #!/bin/bash while... (1 Reply)
Discussion started by: phpfreak
1 Replies

10. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies
Login or Register to Ask a Question