Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash variable interpolation separate variables by a hyphen or underscore

This is a simple script just to see if the file has been downloaded. On this script the find command always evaluated to zero - even if it didn't find anything. So I commented it out.

on the filename="day_CTRwFEES_hoo01M_" I had to add an underscore to the end of the filename.

Using an underscore $filename_$yesterday.CSV to separate the two did not work. - I had to take out the underscore, add it to the filename and then combine the variables to make it work like this - $filename$yesterday.

How could I get it to work without adding the underscore to the end of the variable $filename?

#!/bin/bash
set -x
dayofweek=$(/bin/date +%w)
today=$(/bin/date +%Y%m%d)
yesterday=$(/bin/date -d "1 day ago" +%Y%m%d)
friday_morning=$(/bin/date -d "3 days ago" +%Y%m%d)
filename="day_CTRwFEES_hoo01M_"

#if find /data/today/ -type f -name "$filename_$yesterday.CSV" ; then
if ls "/data/today/$filename$yesterday.CSV" ; then
    echo "successful"
else
    echo "$filename$yesterday.CSV was not downloaded, please check." | mail -s "$filename$yesterday.CSV not downloaded" casper@big_bank.com
    fi

casper@good_host5981dap:~/walt/morning_checks$ ./check_day_CTRwFEES_hoo01M
++ /bin/date +%w
+ dayofweek=5
++ /bin/date +%Y%m%d
+ today=20141024
++ /bin/date -d '1 day ago' +%Y%m%d
+ yesterday=20141023
++ /bin/date -d '3 days ago' +%Y%m%d
+ friday_morning=20141021
+ filename=day_CTRwFEES_hoo01M_
+ ls data/today/day_CTRwFEES_hoo01M_20141023.CSV
/data/today/day_CTRwFEES_hoo01M_20141023.CSV
+ echo successful
successful

~

like image 336
capser Avatar asked Mar 19 '23 02:03

capser


2 Answers

By telling bash where the variable name ends.

"${filename}_$yesterday.CSV"
like image 191
Ignacio Vazquez-Abrams Avatar answered Mar 20 '23 16:03

Ignacio Vazquez-Abrams


Several possibilities:

  • The most natural one: enclose your variable name in curly brackets (Ignacio Vazquez-Abrams's solution):

    echo "${filename}_$yesterday.CSV"
    
  • Since your separator is a rather special character, you may use a backslash (Sriharsha's Kallury's solution):

    echo "$filename\_$yesterday.CSV"
    
  • (Ab)use quotes:

    echo "$filename""_$yesterday.CSV"
    

    or

    echo "$filename"_"$yesterday.CSV"
    
  • Use an auxiliary variable for the separator:

    sep=_
    echo "$filename$sep$yesterday.CSV"
    
  • Use an auxiliary variable for the final string, and build it step by step:

    final=$filename
    final+=_$yesterday.CSV
    echo "$final"
    

    or in a longer fashion:

    final=$filename
    final+=_
    final+=$yesterday
    final+=.CSV
    echo "$final"
    
  • Use an auxiliary variable for the final string, and build it with printf:

    printf -v final "%s_%s.CSV" "$filename" "$yesterday"
    echo "$final"
    

(feel free to add other methods to this post).

like image 20
gniourf_gniourf Avatar answered Mar 20 '23 15:03

gniourf_gniourf