在一系列条件变化的变量 [英] Changing variable on set of conditions
问题描述
此问题是相关的线程在这里:
This question is related to the thread here:
<一个href=\"http://stackoverflow.com/questions/13533661/todays-date-minus-x-days-in-shell-script#13533753\">Today's迄今为止,减去X天的shell脚本
不过,因为我现在操纵变量,我开始另一个线程。
But because I'm now manipulating the variable, I started another thread.
如上所述,我需要得到今天的日期减去200天,年,月,并在不同的变量日(在这个问题上我会用200,尽管在其他它是222)。但是,我需要present月重新为0,2月份1(或01),三月2(或02),等等。我试过这样:
As described above, I need to get today's date minus 200 days, with the Year, Month, and Day in separate variables (in this question I'll use 200, though in the other it's 222). However, I need to represent January as 0, February as 1 (or 01), March as 2 (or 02), etc... I tried this:
MONTHS200=$(date -j -v-200d -v-1m +"%m")
if ${MONTHS200}=01; then
${MONTH200}=0
else ${MONTHS200}=${MONTH200}
fi
但我得到的错误 ./ update_newdateformat.sh:第20行:12 = 01:找不到命令./update_newdateformat.sh:第23行:12 =:命令未找到
的 -v-1M
适用于除了1月份之外的所有月份,因为它关系到12,而不是0
But I get the error ./update_newdateformat.sh: line 20: 12=01: command not found ./update_newdateformat.sh: line 23: 12=: command not found
The -v-1m
works for all months except January, because it goes to 12, instead of 0
推荐答案
下面是如何通过1 n移位所有月份数字向下脚本:
Here's how to shift all the month number down by 1 n your script:
MONTHS200=$(date -j -v-320d +"%m")
# Remove leading zero if there is one, so it doesn't cause problems later
MONTHS200=${MONTHS200#0}
MONTHS200=$((MONTHS200-1))
下面是如何使用如果
和 =
(分配)语法在外壳:
Here is how to use if
and =
(assignment) syntax in shell:
if [[ "${MONTHS200}" == "01" ]]; then
MONTHS200="0"
else
MONTHS200=${AnotherVariable}
fi
请注意,对于数值的比较,你需要使用:
Note that for numerical comparisons, you need to use:
-
-eq
而不是==
-
-ne
而不是!=
-
-lt
而不是&LT;
-
-le
而不是&LT; =
-
-gt
而不是&GT;
-
-ge
而不是&GT; =
-eq
instead of==
-ne
instead of!=
-lt
instead of<
-le
instead of<=
-gt
instead of>
-ge
instead of>=
例如:
if [[ "${MONTHS200}" -eq 1 ]]; then
这篇关于在一系列条件变化的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!