I am new to bash scripting, but I am trying to make a small portion of a larger bash script work and I am running into a strange problem. I want to read input data from a .csv file (test_events.csv in this case) and then use each of those inputs to build a .grb file name. I used a while loop to go through the .csv file, read each line, and assign each cell to a different variable (event_name, lat, lon, year, month, day, and time). For this section of the code, I only need to use the year, month, day, and time variables. Inside the while loop is a simple echo statement of the .grb file name that should be created using the data from each line of the .csv file. The problem is that when I run my script it changes the order of my output from what I have inside my loop.
Here is the script I have:
#!/usr/bin/bash
echo "Enter the name of the .csv file you wish to use (format: filename.csv): "
read INPUT
OLDIFS=$IFS
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while IFS="," read -r event_name lat lon year month day time
do
#Create the rap analysis file name and check if it exists
echo
echo "Creating RAP file name"
file="rap_130_20"$year$month$day"_"$time"00_000.grb2"
echo $file
done < <(tail -n +2 $INPUT)
IFS=$OLDIFS
echo End of input csv
Let's assume the first few lines of my .csv file contain the following data:
event_name,lat,lon,year,month,day,time
MS1,32.33,-89.91,18,03,11,08
AL1,33.74,-86.56,18,03,20,00
The output I expect should be something like
Creating RAP file name
rap_130_20180311_0800_000.grb2
Creating RAP file name
rap_130_20180320_0000_000.grb2
However, the output I get is this:
Creating RAP file name
00_000.grb280311_08
Creating RAP file name
00_000.grb280320_00
It seems like it is deleting the first part of the echo statement and then moving all the variables to the end. I do not understand this behavior. To make me more confused, I previously had the name of the time variable misspelled in the echo statement and it returned the expected output just missing the two digit time variable.
So, my question is why is the script doing this and what do I need to know so I can fix it?