Syntax error piping to bc on command line - works when assigned to var
I have a script which outputs some timing data a line at a time. There are approx. 10 lines echoed, each line looks something like this:
Since I needed to add all the values in the first column, I piped the output to grep, matching and printing the first numerical column, then piped it on to tr to replace new lines with '+', then piped to sed to replace the final '+' with nothing, and finally to bc to add the values. Then bc gave me a stdin syntax error - damn it, maybe I'm not quite so clever as I thought I was.
On examining the code I discovered that if I left off the final pipe to bc I got what I thought I should have - that being:
So I copied and pasted those values into an echo statement, piped to bc, and it worked fine as shown above.
As another test I ran the command again, this time assigning the result to a variable, and then piped the variable to bc. That also worked fine, see code below (the top line has the error shown when piped to bc):
What I want to know is why I get the syntax error from bc when I run it as one line? Any ideas?
Many thanks.
PS. The somewhat simpler alternative works with no problems.
Corona688 and jim mcnamara: Did neither of you see the PS in my post !! I know I failed to realize that $1 would give me the first column (I assumed that would be the whole line). It's not that I can't do it, just that I want to understand what I am doing wrong with the pipe to bc. Anyone know?
Corona688: There are no extra spaces, or any spaces at all come to think of it.
I always endeavour to answer the question, and the actual problem. 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.
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.
In your script this would only matter if there were carriage-return or form-feed characters in the line (which there are clearly not).
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)