0

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?

Atmo_Sci
  • 1
  • 2
  • 2
    Your input file probably has DOS line endings. Try running `dos2unix` on it and then re-running your script. – tjm3772 Jun 15 '23 at 18:01
  • Aside: You don't need `OLDIFS`. When you use `IFS=","` at the beginning of a command, it's only temporary for that command, not permanent. – Barmar Jun 15 '23 at 18:02
  • Ok, after checking the suggested question about the line endings it does appear that the ending of each of my lines in my .csv file has ^M at the end. I was not aware of this as a problem, but after using `sed -i 's/\r$//' filename` to remove the ^M the script works as expected. Thank you for your assistance. – Atmo_Sci Jun 15 '23 at 18:25

0 Answers0