“invalid date ‘TZ=\”UTC\” @’” error while using “date” command in bash script

huangapple go评论80阅读模式
英文:

" invalid date ‘TZ="UTC" @’ " error while using "date" command in bash script

问题

我正在尝试运行从互联网上获取的一些bash脚本,但它一直报错:

date: invalid date ‘TZ="UTC" @’

涉及的代码部分如下:

while [ $(date --date="TZ=\"UTC\" @$var2" +'%D') == $(date --date="TZ=\"UTC\"" +'%D') ] ; do

起初,出现了更多问题,但我通过使用双括号("[["和"]]"")解决了一个问题("[: ==: unary operator expected")。

括号更改后的代码如下:

while **[[** $(date --date="TZ=\"UTC\" @$var2" +'%D') == $(date --date="TZ=\"UTC\"" +'%D') **]]** ; do

另外,我看到这个脚本应该在没有使用sudo的情况下运行。我尝试以root用户身份执行它,但即使我没有使用root用户身份,也因为权限错误而无法运行,而且我无法解决这个问题。

我认为问题可能出在--date参数上,它不能读取整个字符串,而只能读取到"@"(包括"@")。我在论坛和网站上查找了很多,但没有找到解决我的问题的答案。有人可以帮助并可能解释一下这个while语句内部究竟发生了什么吗?

英文:

I'm trying to run some bash script I got on the internet, but it keeps spiting out error:

> date: invalid date ‘TZ="UTC" @’

Refering part of code:

while [ $(date --date="TZ=\"UTC\" @$var2" +'%D') == $(date --date="TZ=\"UTC\"" +'%D') ] ; do

At first, there were more issues, but I managed to resolve one ("[: ==: unary operator expected") by using double brackets ("[[" and "]]").

After bracket change:

while **[[** $(date --date="TZ=\"UTC\" @$var2" +'%D') == $(date --date="TZ=\"UTC\"" +'%D') **]]** ; do

Also, I read that this script should run without sudo. I was executing it as root but even when I didn't, it did not run because of a permission error that I also couldn't resolve.

My thinking is that it may be a problem with --date that can't read entire string but only to "@" ("@" included). I don't have much experience in bash, and so I was looking through forums and sites, but I couldn't find an answer to my problem. Can anyone help and maybe explain what exactly is going on inside this while statement?

答案1

得分: 1

TZ=UTC 不是日期字符串的一部分,它是您在 date 命令之外设置的环境变量:

while TZ=UTC [ "$(date --date "@$var2" +'%D')" = $(date +'%D') ] ; do
英文:

TZ=UTC isn't part of the date string, it's an environment variable you set outside the date command:

while TZ=UTC [ "$(date --date "@$var2" +'%D')" = $(date +'%D') ] ; do

huangapple
  • 本文由 发表于 2023年6月15日 03:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定