无法在bash脚本中设置变量

时间:2010-03-17 01:08:49

标签: bash scripting

我正在尝试使用从AppleScript按计划运行的bash脚本自动将文件从文件夹自动移动到新文件夹。我试图在Mac OSX上编写一个bash脚本,它一直在失败。简而言之,这就是我所拥有的(我的所有echo用于错误检查):

#!/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都是空白的。我也无法抓住那一天,一个月或一年,它也是空白的。

我收到错误消息,说找不到-f。

我收到错误消息,指出文件意外结束。

我制作了文件并做了chmod u+x scriptname.sh

我不确定为什么一切都没有用。我是OSX上这个bash脚本的新手,只有windows vbscript的经验。任何帮助都会很棒,谢谢!

4 个答案:

答案 0 :(得分:69)

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

最后三行注释掉了,对我来说这是输出:

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

答案 1 :(得分:29)

五个问题:

  1. 请勿在等号前后放置空格。
  2. 使用"$(...)"以文本形式输出命令。
  3. [是一个命令。在它和参数之间加一个空格。
  4. 命令区分大小写。你想要echo
  5. 在变量周围使用双引号。 rm "$folderToBeMoved"

答案 2 :(得分:4)

这是你修改过的脚本

#!/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

答案 3 :(得分:4)

folder = "ABC"尝试使用参数folder=运行名为"ABC"的命令。 bash中的命令格式为:

command arguments separated with space
分配完成时

variable=something
  • [ -f $newfoldername/Primetime.eyetv]中,[是一个命令(test) 而-f$newfoldername/Primetime.eyetv]是两个论点。它 期望它找不到第三个参数(])(参数必须是 与空格分开)因此会显示错误。
  • [-f $newfoldername/Primetime.eyetv]尝试使用参数[-f
  • 运行命令$newfoldername/Primetime.eyetv]

通常对于这种情况,请将代码粘贴到shellcheck并查看反馈。