Use of ` <` in bash script gives (standard_in) 1: syntax error - bash

I'm getting this error as output
(standard_in) 1: syntax error
something something
something something
when i want is
something something
something something
Below is the bash script. After much trial and error checks i found the error is because of this line
done < /home/afsara/Desktop/ns2_offline/ns_code/wired.out;
What am I possibly doing wrong here?
#!/bin/bash
cd /
cd /home/afsara/Desktop/ns2_offline/ns_code/
#INPUT: output file AND number of iterations
output_file_format="tcp";
iteration_float=2.0;
end=5
iteration=$(printf %.0f $iteration_float);
r=5
while [ $r -le $end ]
do
###############################START A ROUND
l=1;thr=0.0;val=0.0
i=0
while [ $i -lt $iteration ]
do
while read val
do
dir="/home/afsara/Desktop/ns2_offline/ns_code/"
#dir=""
under="_"
all="all"
output_file="$dir$output_file_format$under$r$under$r$under$all.out"
echo -ne "Throughput: $thr " > $output_file
if [ $l == '1' ]; then
thr=$(echo "scale=5; $thr+$val/$iteration_float" | bc )
echo -ne "throughput: $val " >> $output_file
fi
echo "$val"
done < /home/afsara/Desktop/ns2_offline/ns_code/wired.out; #problem because of this
i=$(($i+1))
l=0
#################END AN ITERATION
done
r=$(($r+1))
#######################################END A ROUND
done

That's a bc error message, not a bash error message. It has nothing whatsoever to do with the done < /home/afsara/Desktop/ns2_offline/ns_code/wired.out, except perhaps that if you remove the redirection the loop has no input so its contents don't run at all.

Related

Bash unexpected token near 'then'

I have a syntax error, more like a unexpected symbol near a token 'then', but I can't figure it out..
#!/bin/bash
function Functie(){
LINE=1
while read -r CURRENT_LINE; do
CONTOR=1
for word in "$CURRENT_LINE"; do
if[ "$word" == "$2" ];
then
CONTOR=$CONTOR+1
fi
done
if [ "$CONTOR" -eq "$3" ];
then
echo "$CURRENT_LINE"
fi
LINE=$LINE+1
done < "./"$1""
}
Functie "File1.txt" "Ana" "2"
Run your code through ShellCheck to catch several syntax errors.
Correcting them yields:
#!/bin/bash
function Functie(){
LINE=1
while read -r CURRENT_LINE; do
CONTOR=1
for word in $CURRENT_LINE; do
if [ "$word" == "$2" ];
then
CONTOR=$CONTOR+1
fi
done
if [ "$CONTOR" -eq "$3" ];
then
echo "$CURRENT_LINE"
fi
LINE=$LINE+1
done < ./"$1"
}
Functie "File1.txt" "Ana" "2"
One issue it doesn't detect is the bad assignments. To increment a variable write one of these:
CONTOR=$(($CONTOR+1))
CONTOR=$((CONTOR+1))
((CONTOR += 1))
((++CONTOR))

My max function is throwing an error even though its the same as my min function but flipped, can't find the error?

When I run ShellCheck on my script, it gives me these errors:
Line 27:
{
^-- SC1009: The mentioned syntax error was in this brace group.
Line 30:
for in `cat popconfile`
^-- SC1073: Couldn't parse this for loop. Fix to allow more checks.
^-- SC1058: Expected 'do'.
^-- SC1072: Expected 'do'. Fix any mentioned problems and try again
The script is:
#!/bin/bash
#getalldata() {
#find . -name "ff_*" -exec sed -n '4p' {} \;
#}
#Defining where the population configuration file is which contains all the data
popconfile=$HOME/testarea
#Function to find the average population of all of the files
averagePopulation()
{
total=0
list=$(cat popconfile)
for var in "${list[#]}"
do
total=$((total + var))
done
average=$((total/$(${#list[*]} popconfile)))
echo "$average"
}
#Function to find the maximum population from all the files
maximumPopulation()
{
max=1
for in `cat popconfile`
do
if [[ $1 > "$max" ]]; then
max=$1
echo "$max"
fi
done
}
#Function to find the minimum population from all the files
minimumPopulation()
{
min=1000000
for in `cat popconfile`
do
if [[ $1 < "$min" ]]; then
max=$1
echo "$min"
fi
done
}
#Function to show all of the results in one place
function showAll()
{
echo "$min"
echo "$max"
echo "$average"
}
Although my min function is very similar, I get no error from it; if I switch my min and max function around, then the error is reported for the function that occurs first.
The error is just saying "expected do" - but I already have a do statement. So where is the error?
You are missing the index in the for loop. The immediate fix would be
maximumPopulation()
{
max=1
for i in `cat popconfile`
do
if [[ $i > "$max" ]]; then
max=$i
echo "$max"
fi
done
}
However, you shouldn't use a for loop to iterate over the lines of a file; see Bash FAQ 001. Instead, use a while loop.
maximumPopulation () {
max=1
while IFS= read -r i; do
if (( i > max )); then
max=$i
fi
done < popconfile
printf '%d\n' "$max"
}

Bash Script- Issue with variables and | bc

So I have a variable that I want to compare with another number in an if statement.
b=8.25
if [ $(echo "$b < 10" | bc) -ne 0 ]; then
echo "hey"
fi
I get the following error
(standard_in) 1: syntax error
I know the issue is having the b variable inside, how can I make it so that I can maintain it in there?
Please help
Your script file probably has DOS-style CRLF line endings:
$ b=8.25
$ if [ $(echo "$b < 10" | bc) -ne 0 ]; then
> echo "hey"
> fi
hey
$ b=$'8.25\r'
$ if [ $(echo "$b < 10" | bc) -ne 0 ]; then
> echo "hey"
> fi
(standard_in) 1: illegal character: ^M
bash: [: -ne: unary operator expected
Run dos2unix on your script file.
Store the comparison in a variable separateley
b=8.25
# Capture output outside the if
comparison=$(echo "$b < 10" | bc)
# Use the output in the if
if [ $comparison -ne 0 ]; then
echo "hey"
fi

Simple Shell/Bash Program Syntax

I have the following bash script:
#!/bin/bash
if [ ! -f numbers ]; then echo 0 > numbers; fi
count = 0
while [[$count != 100]]; do
count = `expr $count + 1`
done
When I run it in terminal on my Mac, I get the following output:
seq_file_gen.sh: line 3: count: command not found
seq_file_gen.sh: line 4: [[: command not found
Why am I getting these errors? This script was given to me by my teacher so I have no idea why I can't get this script to run. Any help would be greatly appreciated.
EDIT:
This is the correct way to write this script (with spaces)
#!/bin/bash
if [ ! -f numbers ]; then echo 0 > numbers; fi
count=0
while [[ $count != 100 ]]; do
count=`expr $count + 1`
done
Add spaces before/after [[ and ]] like so:
while [[ $count != 100 ]]; do

Bash Scripting and bc

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 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.
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", input/10 }')
echo "\$? is $?"
echo "NUM is $NUM"
The other way, is to do the check of "$1" before you pass to bc. eg
shopt -s extglob
input="$1"
case "$input" in
+([0-9.]))
IFS="."; set -- $input
if [ $# -ne 2 ];then
echo "bad decimal"
else
NUM=$(echo "scale=25;$1/10" | bc )
echo "$NUM"
fi
esac
you don't have to check for $? from bc anymore
For GNU bc, an error similar to "(standard_in) 1: syntax error" will be output on stderr. You can capture this in your variable and check for it.
#!/bin/bash
NUM=$(echo "scale=25;$1/10" | bc 2>&1)
if [[ $NUM =~ error || $? -ne 0 ]]
then
echo bad
exit
fi
echo "$NUM"
Are you after the result of calculation from bc (which you store in NUM) or the status return from the system call?
As I said you have the result of calculation in $NUM:
#bctest.sh
NUM=$(echo "scale=25;$1/10" | bc)
if [ $? -ne 0 ]
then
echo bad
fi
echo "result: ", $NUM
Test:
bash ./bctest.sh 15
result: , 1.5000000000000000000000000

Resources