简体   繁体   English

shell $字符将覆盖先前的变量

[英]shell $ character overwrites the previous variable

I'm trying to write a script on shell but I'm stucked on a point. 我正在尝试在shell上编写脚本,但遇到了麻烦。

I have a program creating data daily and puting it to a directory like this: home/meee/data/2013/07/22/mydata 我有一个程序每天创建数据并将其home/meee/data/2013/07/22/mydata这样的目录中: home/meee/data/2013/07/22/mydata

My problem is I'm trying to change directory using date. 我的问题是我想使用日期更改目录。 Here is my script: 这是我的脚本:

#!/bin/sh

x=$(date -u -v-2H "+%Y-%m-%d")
echo $x
year=$(echo $x | cut -d"-" -f1)
month=$(echo $x | cut -d"-" -f2)
day=$(echo $x | cut -d"-" -f3)
echo $year
echo $month
echo $day

d1='/home/sensor/data/'${year}/${month}
echo $d1

There is no problem related to year, day, month , they are working. year, day, month无关,他们在工作。 But the output of echo d1 is /07me/sensor/data/2013 . 但是echo d1的输出是/07me/sensor/data/2013 Similarly, when I write echo $year$day it gives 2312 (characters of day is overwritten on the first two characater of the year) 同样,当我编写echo $year$day它得到2312 (characters of day is overwritten on the first two characater of the year)

I tried many other syntax like instead of ' character put " or leave it empty. Removing { and so on. But nothing changed. 我尝试了许多其他语法,例如代替' character put "或将其保留为空。删除{等等。但是什么都没有改变。

Shortly, when I write two variable ($var1 $var2) in same line the second $ behaves like go to the beginning of the line and start overwriting the first variable. 不久,当我在同一行中写入两个变量($var1 $var2) ,第二个$行为类似于转到该行的开头并开始覆盖第一个变量。

I've been looking for that but there is nothing related to that or I couldn't find anything related and there are a lot of solution in Stackoverflow that solves the problem using $var1$var2 我一直在寻找,但没有与此相关的东西,或者我找不到任何相关的东西,并且在Stackoverflow中有很多解决方案可以使用$var1$var2解决该问题

What am I doing wrong, or how can I solve that. 我在做什么错,或者我该如何解决。

I'm working on FreeBSD 9.0-RELEASE amd64 and using sh 我正在使用FreeBSD 9.0-RELEASE amd64并使用sh

Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

Somehow, your commands are introducing carriage returns to your variables, which affect the output when the variable is not the last thing echo ed. 不知何故,您的命令正在将回车符引入变量,当变量不是最后echo的变量时,这将影响输出。 You can confirm this by passing the value through hexdump or od : 您可以通过将值传递给hexdumpod来确认这一点:

printf "%s" "$x" | hexdump -C   # Look for 0d in the output.
printf "%s" "$year" | hexdump -C   # Look for 0d in the output.
printf "%s" "$month" | hexdump -C   # Look for 0d in the output.
printf "%s" "$day" | hexdump -C   # Look for 0d in the output.

I don't think this will fix the problem, but you can get the year, month, and day without forking so many external programs: 我认为这不会解决问题,但是您可以得到年,月和日,而无需花费太多外部程序:

IFS=- read year month day <<EOF
$(date -u -v-2H "+%Y-%m-%d")
EOF

or more simply 或更简单

read year month day <<EOF
$(date -u -v-2H "+%Y %m %d")
EOF

You're likely using MinGW or some other not-quite-Unix environment for Windows®, which is introducing Carriage Return (CR, \\r) characters at end-of-line (from the Unix PoV). 您可能在Windows®上使用MinGW或其他非Unix环境,该环境在行尾引入了回车符(CR,\\ r)(来自Unix PoV)。

So change this to either: 因此,将其更改为:

x=$(date -u -v-2H "+%Y-%m-%d" | sed $'s/\r$//')
echo $x
year=$(echo $x | cut -d"-" -f1 | sed $'s/\r$//')
month=$(echo $x | cut -d"-" -f2 | sed $'s/\r$//')
day=$(echo $x | cut -d"-" -f3 | sed $'s/\r$//')

Or, even better: 或者,甚至更好:

x=$(date -u -v-2H "+%Y %m %d ")
echo $x
set -- $x
year=$1
month=$2
day=$3

Note the extra space after %d which ensures that the CR will become $4 instead of attached to the day. 请注意%d之后的多余空间,以确保CR变为$ 4,而不是固定在当天。

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

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