-4

I have a bash script with the following line. The variables start_time and start_files[$i] are floating point numbers. I want to compare them using the command bc as follows:

result1=$(bc -l <<< $start_time'>='${start_files[$i]})

When I run the script I always receive the following error.

(standard_in) 1: syntax error

I've checked that this error is due to this line. What am I doing wrong? The thing that this happens to me when using bash 4.1, with bash 4.3 runs fine. However I need to run the script with bash 4.1.

The bc command works however the input doesn't work. The variable start_files is read from a file with this command

IFS=, read -r -a start_files <<< $(head -n 1 file.txt)

Basically I want to read all the values from the first line separated by a coma and store them to different positions of the array. However using bash 4.1 all the values end up stored in start_files[0]. How can I solve this? This lines works for bash 4.3

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • 1
    what are the values of those variables? – glenn jackman Oct 30 '14 at 16:53
  • floating point values like 9.5, 4.2, 34.5678 etc –  Oct 30 '14 at 16:54
  • 1
    What do you see when you debug the script with `bash -x script`? It often helps tell you what is wrong. However, it does not print the here string, so you would have to modify the script to do `echo "bc -l <<< $start_time >= ${start_files[$i]}"` or something similar that accurately reflects what would be executed so as to see the input that `bc` is complaining about. When solving a syntax error problem, the first thing you need to see is the syntax that is being complained about. – Jonathan Leffler Oct 30 '14 at 17:00
  • Sorry, I can not reproduce the error with bash version 4.1.2 (RHEL 6): `start_time=9.5; i=4; start_files[$i]=4.2; result1=$(bc -l <<< $start_time'>='${start_files[$i]}); echo $result1` – Cyrus Oct 30 '14 at 17:05
  • @KoTy, check that the *specific* values really are what you think they are -- a mistake leading to one of them being empty, f'rinstance, is a very likely cause of this error. Echo them out immediately before running `bc`; without doing so, since this is a `bc` syntax error rather than a bash syntax error, means we simply don't have enough information to help. – Charles Duffy Oct 30 '14 at 17:11
  • Please post more of the script. It's likely the syntax error is not where you think. Also, how are you running the script? The fact that the error is in standard_in, not a specific file, could be relevant. – chepner Oct 30 '14 at 17:15
  • The complaint remains. Previously, we could not guess what was in your variables; now, we cannot guess what's in your file. – tripleee Apr 29 '16 at 12:41

4 Answers4

1

I would suggest that you use double quotes:

result1=$(bc -l <<<"$start_time >= ${start_files[$i]}")

This way, you are echoing a single string to bc, containing your variables.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • keeps yielding me the same error, (standard_in) 1: syntax error –  Oct 30 '14 at 16:57
  • 1
    Without knowing the values of the variables, it's impossible to say why. Perhaps you should use `set -x` so you can see what the values are that are causing your error. – Tom Fenech Oct 30 '14 at 16:58
  • @koty It must be the values: What does `echo ">>$start_time >= ${start_files[$i]}<<"` show you – glenn jackman Oct 30 '14 at 16:58
0

Are you reading the values from a file with DOS-style line endings perhaps?

$ start_time=$'1.5\r'
$ i=1
$ start_files[$i]=$'2.5\r'
$ bc -l <<< "$start_time >= ${start_files[$i]}"
(standard_in) 1: illegal character: ^M
(standard_in) 1: illegal character: ^M
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

You may avoid the here-string, add double quotes and remove a $:

result1=$(echo "$start_time >= ${start_files[i]}" | bc -l)
Edouard Thiel
  • 5,878
  • 25
  • 33
0

It would help to give the version of bc you're using. I'd guess it's not the same on the two systems. By the way, your error has nothing to do with Bash.

POSIX bc clearly states that a comparison (referred to as relational_expression in POSIX' bc Specification can only appear in a while, for or if construct.

So, first thing you'll try is this:

bc <<< "if($start_time >= ${start_files[$i]}) 1 else 0"

This might not work, as POSIX' bc doesn't allow an else clause in an if statement. (As amazing as it may seem, you read that last sentence correctly).

If your bc is really POSIX stuck up, then you'll have to do some ugly stuff as, e.g.,

bc <<< "ret=0; if($start_time >= ${start_files[$i]}) { ret=1 } ret"

or even something worse (sorry, I don't have a POSIX bc here, so I can't experiment—if someone has one at hand or remembers the syntax off the top of his head, please edit this line with the proper syntax).

Good luck!

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104