Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

~

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By telling bash where the variable name ends.

"${filename}_$yesterday.CSV"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...