can anybody explain why the following bash code involving compound operators is not behaving as expected? basically, nothing enters the if statement inside the for loop but i am passing it correct parameters that should return something by running:
./my_bash_script 20100101 20120101
dates.txt is a list of all days since 2000
#!/bin/bash
old_IFS=$IFS
IFS=$'\n'
lines=($(cat dates.txt)) # array
IFS=$old_IFS
for (( i=1; i<${#lines[@]}; i++ ))
do
if [[ ${line[$i]} -ge $1 && ${line[$i]} -le $2 ]]; then
echo 0 > ${line[$i]} # redirect to file
echo ${line[$i]}
fi
done
The problem is that you’ve declared an array named
lines, but then you try to access it as though it were namedline. You need to change every occurrence of${line[$i]}to${lines[$i]}.Better yet, you can dispense with the arithmetic for-loop, and write:
which will let you refer to the line as
$lineor"$line"rather than as${lines[$i]}.(By the way, how come you have that logic to modify
$IFS? It seems like its default value would work just as well.)