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

编程入门 行业动态 更新时间:2024-10-27 16:34:08
本文介绍了bash 变量插值用连字符或下划线分隔变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个简单的脚本,只是为了查看文件是否已下载.在这个脚本中, find 命令总是计算为零——即使它没有找到任何东西.所以我把它注释掉了.

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.

在 filename="day_CTRwFEES_hoo01M_" 上,我必须在文件名的末尾添加一个下划线.

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

使用下划线 $filename_$yesterday.CSV 将两者分开不起作用.- 我不得不去掉下划线,将其添加到文件名中,然后组合变量以使其像这样工作 - $filename$yesterday.

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.

如何在不将下划线添加到变量 $filename 末尾的情况下使其工作?

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 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 变量名在哪里结束.

By telling bash where the variable name ends.

"${filename}_$yesterday.CSV"

更多推荐

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

本文发布于:2023-10-31 18:26:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   下划线   字符   插值   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!