简体   繁体   English

Bash 变量之谜

[英]Bash variable mystery

So at work we use an accounting system and at the end of the month we have to modify a script for the previous month and specify a new name for the log that's printing out.所以在工作中,我们使用会计系统,在月底我们必须修改上个月的脚本,并为正在打印的日志指定一个新名称。 Since I'm fairly lazy and would rather spend my time learning something new than repeating the same task over again I decided I wanted to write the script to do it all for me and set it as a cron job.由于我相当懒惰,宁愿花时间学习新事物也不愿重复相同的任务,所以我决定编写脚本来为我完成这一切并将其设置为 cron 作业。 Since I just started this job and I'm new to Bash programming in general I decided to research a little and found everything I needed to do the job and came up with this.由于我刚开始这项工作,而且我是 Bash 编程的新手,因此我决定进行一些研究,并找到了完成这项工作所需的一切并想出了这个。

#!/bin/bash
month= $(echo $(date --date="last month" +%b))
year=$(echo $(date +%Y))
echo $(date --date="last month" +%Y-%m-%d)
echo $(date --date="yesterday" +%Y-%m-%d)
echo $(date --date="last month" +%b)
echo garuda-summary-$month$year.log;

and it prints out this它打印出这个

test.sh: line 2: Apr: command not found
2011-04-02
2011-05-01
Apr
computer-summary-2011.log

So I played with it some more and got this one to work.所以我又玩了一些,让这个工作。 But it's ugly and I want to know why it didn't work before.但它很丑,我想知道为什么它以前不起作用。 Thanks in adv感谢广告

#!/bin/bash
month= $(echo $(date --date="last month" +%b))
year=$(echo $(date +%Y))
echo $(date --date="last month" +%Y-%m-%d)
echo $(date --date="yesterday" +%Y-%m-%d)
echo $(date --date="last month" +%b)
echo garuda-summary-$(date --date="last month" +%b)$year.log;
test.sh: line 2: Apr: command not found

is because you have a space after your "=" when assigning to month.是因为在分配月份时,“=”后面有一个空格。 This space makes bash think that Apr (the output of $(echo $(date --date="last month" +%b))) is a command you want to call.这个空间使 bash 认为 Apr ($(echo $(date --date="last month" +%b)) 的 output) 是您要调用的命令。

This will also result in month not being set in the shell, because specifying a command on the same line as a variable assignment tells bash only to use this variable value when calling the specified command.这也将导致 shell 中未设置月份,因为在与变量赋值相同的行上指定命令会告诉 bash 在调用指定命令时仅使用此变量值。

First of all, you don't need to do everything in sub-shells through echo.首先,你不需要通过echo在子shell中做所有事情。 This should be the equivalent, and more easy to work with:这应该是等效的,并且更易于使用:

#!/bin/bash
month=`date --date="last month" +%b`
year=`date +%Y`
date --date="last month" +%Y-%m-%d
date --date="yesterday" +%Y-%m-%d
date --date="last month" +%b
echo garuda-summary-$month$year.log

You could dispense with the $(...) stuff by doing:您可以通过以下方式省去$(...)的东西:

year=`date +%Y`
month=`date --date='last month'`

and so on, which should improve legibility somewhat.等等,这应该会在一定程度上提高易读性。

As well, you may want to check what happens when the current month is January of any year.同样,您可能想检查当当前月份是任何一年的一月时会发生什么。 You'll generate "December 2011" for your dates, rather than "December 2010".您将为日期生成“2011 年 12 月”,而不是“2010 年 12 月”。

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

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