Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard_in error in bash script

Tags:

linux

bash

I 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" | 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: syntax error
(standard_in) 3: illegal character: M
(standard_in) 3: illegal character: :
(standard_in) 3: illegal character: H
(standard_in) 3: illegal character: :
(standard_in) 3: illegal character: :
(standard_in) 4: illegal character: P
(standard_in) 4: illegal character: :
(standard_in) 4: syntax error
(standard_in) 4: illegal character: M
(standard_in) 4: illegal character: :
(standard_in) 4: illegal character: H
(standard_in) 4: illegal character: :
(standard_in) 4: illegal character: :
(standard_in) 5: syntax error
(standard_in) 5: illegal character: :
(standard_in) 6: illegal character: P
(standard_in) 6: illegal character: :
(standard_in) 6: illegal character: M
(standard_in) 6: syntax error
(standard_in) 6: illegal character: :
(standard_in) 7: syntax error
(standard_in) 7: illegal character: :
(standard_in) 8: illegal character: P
(standard_in) 8: illegal character: :
(standard_in) 8: syntax error
(standard_in) 8: illegal character: M
(standard_in) 8: illegal character: :
(standard_in) 15: syntax error
(standard_in) 16: syntax error
(standard_in) 16: illegal character: M
(standard_in) 16: illegal character: I
(standard_in) 16: illegal character: P
(standard_in) 17: syntax error
(standard_in) 18: illegal character: T
(standard_in) 18: illegal character: S
(standard_in) 18: illegal character: T
(standard_in) 18: syntax error
(standard_in) 19: illegal character: T
(standard_in) 19: illegal character: S
(standard_in) 19: illegal character: T
(standard_in) 19: syntax error
(standard_in) 20: illegal character: T
(standard_in) 20: illegal character: S
(standard_in) 20: illegal character: T
(standard_in) 20: syntax error
(standard_in) 21: illegal character: T
.
.
.
(standard_in) 90: illegal character: I
(standard_in) 90: illegal character: O
(standard_in) 90: syntax error
(standard_in) 90: illegal character: P
(standard_in) 90: illegal character: I
(standard_in) 90: illegal character: I
(standard_in) 90: illegal character: T
(standard_in) 91: syntax error
(standard_in) 91: syntax error
(standard_in) 91: syntax error
(standard_in) 91: syntax error
(standard_in) 93: illegal character: S
(standard_in) 93: syntax error
(standard_in) 94: illegal character: O
(standard_in) 94: syntax error
(standard_in) 95: syntax error
(standard_in) 96: illegal character: U
(standard_in) 96: syntax error
(standard_in) 96: illegal character: '
(standard_in) 96: illegal character: O
(standard_in) 96: illegal character: '
                 FAILED.

There are plenty more lines where the "..." is, but they're basically just a variation on the same idea.

like image 853
Jessica Avatar asked Jun 29 '10 18:06

Jessica


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does $_ mean in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails. $@

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.

What are $( and $(( )) in bash?

$(...) is an expression that starts a new subshell, whose expansion is the standard output produced by the commands it runs. This is similar to another command/expression pair in bash : ((...)) is an arithmetic statement, while $((...)) is an arithmetic expression. Follow this answer to receive notifications.


1 Answers

The error messages are produced by bc. The problem is that there is something like "S:S:N:I::O:P..." in the file "$HOME/Documents/Development/road/Vertical/utest/$file.res" that's not in the corresponding file that's used in the other script. Chances are that you expect that file to contain a single numeric value instead of whatever is in there now.

like image 187
Dennis Williamson Avatar answered Sep 19 '22 18:09

Dennis Williamson