简体   繁体   English

bash 变量插值用连字符或下划线分隔变量

[英]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.在这个脚本中, find 命令总是计算为零——即使它没有找到任何东西。 So I commented it out.所以我把它注释掉了。

on the filename="day_CTRwFEES_hoo01M_" I had to add an underscore to the end of the filename.filename="day_CTRwFEES_hoo01M_"我必须在文件名的末尾添加一个下划线。

Using an underscore $filename_$yesterday.CSV to separate the two did not work.使用下划线$filename_$yesterday.CSV将两者分开不起作用。 - 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 . - 我不得不去掉下划线,将它添加到文件名中,然后组合变量以使其像这样工作 - $filename$yesterday

How could I get it to work without adding the underscore to the end of the variable $filename ?如何在不将下划线添加到变量$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

~ ~

通过告诉 bash 变量名在哪里结束。

"${filename}_$yesterday.CSV"

Several possibilities:几种可能:

  • The most natural one: enclose your variable name in curly brackets (Ignacio Vazquez-Abrams's solution):最自然的一个:将变量名括在大括号中(Ignacio Vazquez-Abrams 的解决方案):

     echo "${filename}_$yesterday.CSV"
  • Since your separator is a rather special character, you may use a backslash (Sriharsha's Kallury's solution):由于您的分隔符是一个相当特殊的字符,您可以使用反斜杠(Sriharsha 的 Kallury 解决方案):

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

     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构建它:

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

(feel free to add other methods to this post). (随意在这篇文章中添加其他方法)。

You can use backslash to do that.您可以使用反斜杠来做到这一点。

# filename=test
# yesterday=somedate
# echo $filename_$yesterday.csv
somedate.csv
# echo $filename\_$yesterday.csv
test_somedate.csv
#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM