bashshellhive

How to include a shell variable in the file name of a csv from hive


I'm running a script that will be automated to run every quarter so I would like to include the dynamic date variable in the name of the csv output at the end of this script. Here is the relevant snippet, it works fine without the $qtr in the file name, but when I add it to the filename, the resulting .csv is unreadable.

#!/bin/bash

qtr=$(date -d "-2 month" +%Y)Q$(( ($(date -d "-2 month" +%-m)-1)/3+1 ))

hiveconnector -e "set hive.cli.print.header=true;select * from hivetable;" | tr "\\t" "," >file_name_$qtr.csv

Edit: I've tried the following changes to file_name_$qtr.csv:

file_name_${qtr}.csv, file_name_"${qtr}".csv, $"file_name_${qtr}.csv"

Edit 2: It looks like a line break is getting added between the $qtr var and the plain text, even though an echo of file_name_"${qtr}".csv does not show the line break--

Error message:

Can't create file 'C:\...\file_name_2024Q1

.csv'
    
The filename, directory name, or volume label syntax is incorrect.

Edit 3 - Solution: I was able to solve by Unix EOL Conversion and calling a new variable that held the whole path/filename.


Solution

  • Solution: I was able to solve by Unix EOL Conversion and calling a new variable that held the whole path/filename.