Concat Variable (date) and String in shell unix - bash

a 夏天 提交于 2020-04-29 10:14:19

问题


I'm trying to concat a var (date) and string to have a file with current date name.

My code.

days="$(date +"%Y%m%d_%H%M")"
echo "SKIP" > ${days}_EMERGENCY.txt

but when I run, I get a file with a ? in file name, like this:

Am I doing something wrong?

EDIT

Looking at symbol, ? stands for \r - could it be because I'm writing on notepad and then upload via ftp the .sh script?

EDIT 2

Tried with vi on local machine - now it's also worse.


回答1:


I guess your vi will have made the entire file DOS-style and so there will be another carriage return at the end of the echo statement

Try dos2unix or using an editor that allows you to change the line-ending style or

sed -i "s/$( printf '\015' )//g" yourscript



回答2:


try to remove the redundant double quote in your first variable assignment.

$ days=$(date +%Y%m%d_%H%M)
$ echo $days #see what output will get?
$ echo "SKIP" > ${days}_EMERGENCY.txt



回答3:


It works on my system (OS X) even with double quotes everywhere. For variation I used:

$> days="$(date +'%Y%m%d_%H%M')"
$> echo $days
20160601_1051
$> echo "SKIP" > ${days}_EMERGENCY.txt
$> ll ${days}_EMERGENCY.txt
-rw-r--r--  1 user  team  5 Jun  1 10:52 20160601_1051_EMERGENCY.txt


来源:https://stackoverflow.com/questions/37563704/concat-variable-date-and-string-in-shell-unix-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!