Bash Bc Standard_in 1 Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the bash syntax error near unexpected token done' company Business Learn more about hiring developers or posting ads with us Unix & Linux bash syntax error near unexpected token else' Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of bash syntax error near unexpected token fi' Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to bash syntax error invalid arithmetic operator the top Cannot sum numbers received from stdin using bc up vote 7 down vote favorite I'm trying to calculate the average entropy of files contained in a folder using: { echo '('; find . -type f -exec entropy {} \; | grep -Eo '[0-9.]+$' | sed -r 's/$/+/g'; echo '0)/'; find . -type f | wc -l; } | tr -d '\n' | bc -l entropy being an executable which calculates the Shannon entropy
Bash Syntax Error Near Unexpected Token Then'
of a file, giving outputs of the form: $ entropy foo foo: 5.13232 The aforementioned command errors out with: (standard_in) 1: syntax error However, the generated output seems to have no problems: $ { echo '('; find . -type f -exec entropy {} \; | grep -Eo '[0-9.]+$' | sed -r 's/$/+/g'; echo '0)/'; find . -type f | wc -l; } | tr -d '\n' (5.13232+2.479+1.4311+0)/3 And this works too: $ echo '(2.1+2.1)/2' | bc -l 2.1 What is wrong with the mentioned command? bash shell bc share|improve this question edited Jun 23 '14 at 11:43 asked Jun 23 '14 at 11:36 user2064000 562414 Are you willing to use awk? Would be substantially easier. –Bernhard Jun 23 '14 at 11:46 2 You're just missing a trailing endline for the bc command: compare printf '(5.13232+2.479+1.4311+0)/3' | bc -l with echo '(5.13232+2.479+1.4311+0)/3' | bc -l. (your tr -d '\n' command removes the trailing newline that bc needs). –gniourf_gniourf Jun 23 '14 at 11:49 3 An easy fix is to insert { cat; echo; } between the tr and the bc: tr -d '\n' | { cat; echo; } | bc -l or to replace the tr -d '\n' part with: { tr -d '\n'; echo; } –gniourf_gniourf Jun 23 '14 at 11:53 2 Use paste -sd'\0' - instead of
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Bash Syntax Error Near Unexpected Token Do'
About Us Learn more about Stack Overflow the company Business Learn more about bash syntax error near unexpected token newline' hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss bash syntax error unexpected end of file Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Bash Scripting and bc http://unix.stackexchange.com/questions/138709/cannot-sum-numbers-received-from-stdin-using-bc up vote 9 down vote favorite 1 I'm trying to write a bash script and I needed to do some floating point math. Basically I want to do something like this: NUM=$(echo "scale=25;$1/10" | bc) if [ $? -ne 0 ] then echo bad fi The problem I'm running into is $? tends to hold the output from the echo program and not the bc http://stackoverflow.com/questions/2331936/bash-scripting-and-bc call. Is there a way I save the output from the bc program into a variable? EDIT: Thanks for the quick replies. Here's another way of looking at the problem. Say I modified the script a little bit so it looks like this: #!/bin/bash NUM=$(echo "scale=25;$1/10" | bc) if [ $? -ne 0 ] then echo bad exit fi echo "$NUM" When the user inputs a normal floating point value, it works fine: bash script.sh 1.0 output: .1000000000000000000000000 However, when the user enters an incorrect value, the script can't recover: bash script.sh 1.0a output: (standard_in) 1: parse error What I'm trying to do is get it to exit gracefully. bash scripting sh bc share|improve this question edited Jul 9 '13 at 23:44 asked Feb 25 '10 at 5:50 LandonSchropp 3,8791247104 add a comment| 3 Answers 3 active oldest votes up vote 6 down vote accepted I don't see anything wrong. $NUM is supposed to hold your bc command results see: NUM=$(echo "scale=25;$1/10" | bc) echo "\$? is $?" echo "NUM is $NUM" output $ ./shell.sh 10 $? is 0 NUM is 1.0000000000000000000000000 another way is to use awk NUM=$(awk -vinput="$1" 'BEGIN{printf "%.25f", i
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more http://stackoverflow.com/questions/3143529/standard-in-error-in-bash-script about Stack Overflow the company Business Learn more about hiring developers or posting ads http://www.linuxquestions.org/questions/programming-9/standard_in-1-parse-error-438431/ with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Standard_in error in bash script up vote 6 down vote favorite I syntax error have two bash scripts that are almost identical. One works and one doesn't and I can't figure out what's going on. Here are the scripts: This one works fine: #!/bin/bash CURDIR=$HOME/Documents/Development/road/Earthmoving TOL=0.05 echo -e "\nRunning Unit Tests" echo -e "------------------\n" for infile in $CURDIR/utest/*.csv do file=$(basename $infile .csv) echo -n " Test $file" value=`$CURDIR/Release/earthmove -f $infile` res=`cat $CURDIR/utest/$file.res` if [ "$(echo "$res+$TOL*$res >= $value && $res-$TOL*$res <= $value" | bash syntax error bc)" -eq 1 ]; then echo -e "\t\t PASSED." else echo -e "\t\t FAILED." fi; done echo "" But this one gives me all kinds of errors: #!/bin/bash CURDIR=$HOME/Documents/Development/road/Vertical TOL=0.05 echo -e "\nRunning Unit Tests" echo -e "------------------\n" for infile in $CURDIR/utest/*.csv do file=$(basename $infile .csv) echo -n " Test $file" value=`$CURDIR/Release/vertical -f $infile` res=`cat $CURDIR/utest/$file.res` if [ "$(echo "$res+$TOL*$res >= $value && $res-$TOL*$res <= $value" | bc)" -eq 1 ]; then echo -e "\t\t PASSED." else echo -e "\t\t FAILED." fi; done echo "" The two files are IDENTICAL besides the directory (Earthmoving vs Vertical) and the program name (earthmove vs vertical). I know this because I created the second by copying and pasting the first. Running the second script results in this: Running Unit Tests ------------------ Test flatroad(standard_in) 1: illegal character: S (standard_in) 1: syntax error (standard_in) 1: illegal character: : (standard_in) 2: illegal character: S (standard_in) 2: illegal character: : (standard_in) 2: syntax error (standard_in) 2: illegal character: N (standard_in) 2: illegal character: : (standard_in) 2: illegal character: I (standard_in) 2: illegal character: : (standard_in) 2: illegal character: : (standard_in) 2: illegal character: O (standard_in) 2: illegal character: : (standard_in) 3: illegal character: P (standard_in) 3: illegal character: : (standard_in) 3:
Wiki Search Tutorials/Articles Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Non-*NIX Forums > Programming (standard_in) 1: parse error User Name Remember Me? 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 Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. Click Here to receive this Complete Guide absolutely free. Search this Thread 04-24-2006, 03:03 PM #1 timmay9162 LQ Newbie Registered: Feb 2006 Posts: 20 Rep: (standard_in) 1: parse error Code: input=input.file echo -e "Centigrade Temperature\tFahrenheit Temparature" echo -e "----------------------\t----------------------" for temp in $input do C=`cat $input | grep $temp | cut -f1` F=$(echo "scale=0; 32+(1.8*$C)" | bc) echo -e "\t\t $C \t\t $F" done exit #end There is the script i do n