Syntax error piping to bc on command line - works when assigned to var


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax error piping to bc on command line - works when assigned to var
# 8  
Old 01-09-2012
Quote:
Originally Posted by Corona688
I always endeavour to answer the question, and the actual problem. Smilie Seeing your ps actually encouraged me to suggest more awk things.

It may be the complete lack of a newline that causes bc to fail, too. echo will add one on the end, but just piping it in would end up with nothing. Many commands are picky about lines without newlines -- some versions of awk and sed, for instance.
I hope I did not sound ungrateful about the PS, I certainly didn't mean to (I did add a smiley), I just assumed you'd missed it.

Yes it is the lack of a newline causing bc the problem, see my reply to methyl.

---------- Post updated at 06:43 PM ---------- Previous update was at 06:42 PM ----------

Quote:
Originally Posted by vgersh99
how about:
Code:
getip test | grep -o "^[0-9.]*" | tr "\n\r\f" "+" | sed 's/+$/&0/' | bc

Nope same problem, it's the lack on newline.

---------- Post updated at 06:49 PM ---------- Previous update was at 06:43 PM ----------

Quote:
Originally Posted by methyl
Explanation of the error.
Your "tr" command strips out all line terminators (\n) for some unknown reason and converts them to "+" signs. My version of "sed" will not process lines without a line terminator, but yours appears to work (to my surprise).

The "bc" program fails because the input stream is not terminated with a linefeed character (\n).

When you "echo" the variable $var1 the "echo" statement appends a linefeed character (\n).

Btw. The unix standard version of "tr" will not translate three different characters into one. The length of the translate list must be the same both sides.
Code:
echo "xyz" | tr 'xyz' '+'
+yz
echo "xyz" | tr 'xyz' '+++'
+++

In your script this would only matter if there were carriage-return or form-feed characters in the line (which there are clearly not).
The reason why I'm translating the newlines to "+" is to get the "+"s in between the numbers for input into bc.

Yes, you are right the bc works fine once there's a newline appended.

Thanks for the tr info, amended my code.

Fully working and fully understood:
Code:
$ getip test | grep -o "^[0-9.]*" | tr "\n" "+" | sed "s/\(.*\)+/\1\n/" | bc
3.589

EDIT: vgersh99's sed code is more elegant than mine, slightly modified to replace the final '+' with a new line and we have:

getip test | grep -o "^[0-9.]*" | tr "\n" "+" | sed "s/+$/\n/" | bc

I'll use the more concise awk command next time.

Thanks all.

Last edited by gencon; 01-09-2012 at 06:28 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command works at command line but not from cron

Oracle Linux 6. Trying to set up a simple monitoring of memory usage. This command does exactly what I want at the command line: echo $(date +%Y-%m-%d" "%H:%M:%S) $(grep PageTables /proc/meminfo) >> /home/oracle/meminfo.logBut when I put it in my crontab: * * * * * echo $(date +%Y-%m-%d"... (2 Replies)
Discussion started by: edstevens
2 Replies

2. Shell Programming and Scripting

Command assigned to a variable is failed or not having any data - error

Hi, My command is getting stuck while running it. observed that the grep command doesn't returned any data ($? was 1) and it failed. This command is assigned into the variable and used in other command as script progresses. To continue the script output, i have to press ^C twice and script... (2 Replies)
Discussion started by: abhii
2 Replies

3. Shell Programming and Scripting

TCL - quotes inside a string assigned to a var

I know that set line "This is a line." puts whats between " inside the var line. But How do I do the same for set line "This is a line "with quotations" inside the string." (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Works on command line but not in script

OSX 10.9 I am building a script that evaluates the difference between 2 files. Here is a command that does not work transparently. Running this command in Terminal yields great results; however when I put that line in a .sh script, I get the errors shown below. Am I doing something silly? ... (1 Reply)
Discussion started by: sudo
1 Replies

5. Shell Programming and Scripting

Function works, trigger causes syntax error, whats my fault??

Needing a hint. Creating that function called Meter my simple script works well. What I want now is to start the last four commented lines to include or trigger a reaction, if there are more than n lines in that .txt-file it shall display that message for example. But the interpreter says there is... (3 Replies)
Discussion started by: 1in10
3 Replies

6. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

7. UNIX for Dummies Questions & Answers

Works on command line but not in script

Hey guys. Hopefully this is an easy one but having reference similar problems on the web I still can't fix it. I am doing a recursive find and replace from a script. Of course I could just run the damn thing from the command line but it's bugging me now and want to get it working. grep -rl... (4 Replies)
Discussion started by: anthonyjstewart
4 Replies

8. Shell Programming and Scripting

Command line not getting assigned in script.

I am running the following script and passing the following command line arguments : DBCheckSum_control CA_SITE CA_SITE Can someone please tell me, why my command line args are not being accepted and assigned to variable "TABLE_NAME'' and "ACTION_TYPE"?? usage() { print "Usage: ${0} {... (3 Replies)
Discussion started by: Veenak15
3 Replies

9. UNIX for Advanced & Expert Users

Piping Mail with Exim Only Works Locally

Whenever I send an email to my catchall alias via SSH (i.e., locally), Exim successfully pipes the e-mail to a PHP script, as I told it to do. However, when I try to send an e-mail to my catchall alias via my e-mail client (i.e., remotely), Exim won't pipe the e-mail. Any ideas? My exim.conf... (0 Replies)
Discussion started by: Zhay
0 Replies

10. Shell Programming and Scripting

KSH script: piping passes command-line arguments

Dear forum I have the following small script: #!/bin/ksh echo -e "abba-o" | awk -F '-' '{ print $2 }' | cut -b 1It needs to be ksh.. in bash I don't have this problem. If I run this on opensuse 10.2 I get this as output: e If I run this on suse enterprise 10 sp2 then I get this: o ... (1 Reply)
Discussion started by: gemtry
1 Replies
Login or Register to Ask a Question