问题描述
我正在尝试使用按计划从 AppleScript 运行的 bash 脚本每天晚上自动将文件从文件夹自动移动到新文件夹.我试图在 Mac OSX 上编写一个 bash 脚本,但它一直失败.简而言之,这就是我所拥有的(我所有的 echo
都用于错误检查):
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):
#!/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
现在我的第一个问题是我根本无法设置变量.即使是文字,我只是让它等于一些文字.我所有的 echo
都是空白的.我也无法获取日期、月份或年份,它也显示为空白.
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.
我收到一条错误消息,说找不到 -f.
I get an error saying that -f is not found.
我收到一条错误消息,指出文件意外结束.
I get an error saying there is an unexpected end of file.
我制作了文件并做了一个 chmod u+x scriptname.sh
I made the file and did a chmod u+x scriptname.sh
我不知道为什么什么都不起作用.我对 OSX 上的这个 bash 脚本非常陌生,并且只有 Windows vbscript 的经验.任何帮助都会很棒,谢谢!
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!
推荐答案
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
这篇关于无法在 bash 脚本中设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!