无法在 bash 脚本中设置变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2459286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to set variables in bash script
提问by cohortq
I am attempting to automate moving files from a folder to a new folder automatically every night using a bash script run from AppleScript on a schedule. I am attempting to write a bash script on Mac OSX, and it keeps failing. In short this is what I have (all my echo
s are for error checking):
我正在尝试使用按计划从 AppleScript 运行的 bash 脚本每天晚上自动将文件从文件夹自动移动到新文件夹。我试图在 Mac OSX 上编写一个 bash 脚本,但它一直失败。简而言之,这就是我所拥有的(我所有的echo
s 都是用于错误检查):
#!/bin/bash
folder = "ABC"
useracct = 'test'
day = date "+%d"
month = date "+%B"
year = date "+%Y"
folderToBeMoved = "/users/$useracct/Documents/Archive/Primetime.eyetv"
newfoldername = "/Volumes/Media/Network/$folder/$month$day$year"
ECHO "Network is $network" $network
ECHO "day is $day"
ECHO "Month is $month"
ECHO "YEAR is $year"
ECHO "source is $folderToBeMoved"
ECHO "dest is $newfoldername"
mkdir $newfoldername
cp -R $folderToBeMoved $newfoldername
if [-f $newfoldername/Primetime.eyetv];
then rm $folderToBeMoved;
fi
Now my first problem is that I cannot set variables at all. Even literal ones where I just make it equal some literal. All my echo
s come out blank. I cannot grab the day, month, or year either,it comes out blank as well.
现在我的第一个问题是我根本无法设置变量。即使是文字,我只是让它等于一些文字。我所有echo
的出来都是空白的。我也无法获取日期、月份或年份,它也显示为空白。
I get an error saying that -f is not found.
我收到一条错误消息,说找不到 -f。
I get an error saying there is an unexpected end of file.
我收到一条错误消息,指出文件意外结束。
I made the file and did a chmod u+x scriptname.sh
我制作了文件并做了一个 chmod u+x scriptname.sh
I'm not sure why nothing is working at all. I am very new to this bash script on OSX, and only have experience with windows vbscript. Any help would be great, thanks!
我不知道为什么什么都不起作用。我对 OSX 上的这个 bash 脚本非常陌生,并且只有 Windows vbscript 的经验。任何帮助都会很棒,谢谢!
回答by Isaac
Assignment in bash scripts cannot have spaces around the =
and you probably want your date commands enclosed in backticks$()
:
bash 脚本中的赋值不能在 周围有空格=
,您可能希望将日期命令括在反引号中$()
:
#!/bin/bash
folder="ABC"
useracct='test'
day=$(date "+%d")
month=$(date "+%B")
year=$(date "+%Y")
folderToBeMoved="/users/$useracct/Documents/Archive/Primetime.eyetv"
newfoldername="/Volumes/Media/Network/$folder/$month$day$year"
ECHO "Network is $network" $network
ECHO "day is $day"
ECHO "Month is $month"
ECHO "YEAR is $year"
ECHO "source is $folderToBeMoved"
ECHO "dest is $newfoldername"
mkdir $newfoldername
cp -R $folderToBeMoved $newfoldername
if [-f $newfoldername/Primetime.eyetv]; then rm $folderToBeMoved; fi
With the last three lines commented out, for me this outputs:
注释掉最后三行后,对我来说这是输出:
Network is
day is 16
Month is March
YEAR is 2010
source is /users/test/Documents/Archive/Primetime.eyetv
dest is /Volumes/Media/Network/ABC/March162010
回答by Ignacio Vazquez-Abrams
Five problems:
五个问题:
- Don't put a space before or after the equal sign.
- Use
"$(...)"
to get the output of a command as text. [
is a command. Put a space between it and the arguments.- Commands are case-sensitive. You want
echo
. - Use double quotes around variables.
rm "$folderToBeMoved"
- 不要在等号之前或之后放置空格。
- 用于
"$(...)"
以文本形式获取命令的输出。 [
是一个命令。在它和参数之间放置一个空格。- 命令区分大小写。你要
echo
。 - 在变量周围使用双引号。
rm "$folderToBeMoved"
回答by Jahid
folder = "ABC"
tries to run a command named folder
with arguments =
and "ABC"
. The format of command in bash is:
folder = "ABC"
尝试运行folder
以参数=
和命名的命令"ABC"
。bash中的命令格式为:
command arguments separated with space
while assignment is done with:
当分配完成时:
variable=something
- In
[ -f $newfoldername/Primetime.eyetv]
,[
is a command (test
) and-f
and$newfoldername/Primetime.eyetv]
are two arguments. It expects a third argument (]
) which it can't find (arguments must be separated with space) and thus will show error. [-f $newfoldername/Primetime.eyetv]
tries to run a command[-f
with argument$newfoldername/Primetime.eyetv]
- 在
[ -f $newfoldername/Primetime.eyetv]
,[
是命令 (test
)-f
和$newfoldername/Primetime.eyetv]
是两个参数。它需要一个]
找不到的第三个参数 ( )(参数必须用空格分隔),因此会显示错误。 [-f $newfoldername/Primetime.eyetv]
尝试运行[-f
带参数的命令$newfoldername/Primetime.eyetv]
Generally for cases like this, paste your code in shellcheckand see the feedback.
通常对于这种情况,将您的代码粘贴到shellcheck 中并查看反馈。
回答by ghostdog74
here's your amended script
这是你修改后的脚本
#!/bin/bash
folder="ABC" #no spaces between assignment
useracct='test'
day=$(date "+%d") # use $() to assign return value of date command to variable
month=$(date "+%B")
year=$(date "+%Y")
folderToBeMoved="/users/$useracct/Documents/Archive/Primetime.eyetv"
newfoldername="/Volumes/Media/Network/$folder/$month$day$year"
ECHO "Network is $network" $network
ECHO "day is $day"
ECHO "Month is $month"
ECHO "YEAR is $year"
ECHO "source is $folderToBeMoved"
ECHO "dest is $newfoldername"
mkdir "$newfoldername"
cp -R "$folderToBeMoved" "$newfoldername"
if [ -f "$newfoldername/Primetime.eyetv" ]; then # <-- put a space at square brackets and quote your variables.
rm "$folderToBeMoved";
fi