无法设置在bash脚本变量 [英] Unable to set variables in bash script

查看:115
本文介绍了无法设置在bash脚本变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图自动从文件夹自动每天晚上利用AppleScript的计划运行bash脚本文件移动到一个新的文件夹。我试图写在Mac OSX上一个bash脚本,它不停地进行故障。总之这是我有什么(我所有的回声是错误检查):

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 echos 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

现在我的第一个问题是,我不能在所有的设置变量。即使那些文字,我只是使它等于一些立即。我所有的回声的问世空白。我不能抢了日,月或年或者,它出来空。

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 echos 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.

我做了该文件,并做了一个文件模式ü+ X scriptname.sh

I made the file and did a chmod u+x scriptname.sh

我不知道为什么没有在所有工作。我是很新的OSX上这个bash脚本,只有有窗户的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脚本不能有大约空格 = 键,您可能希望包含在你的约会命令< S>反引号 $()

Assignment in bash scripts cannot have spaces around the = and you probably want your date commands enclosed in backticks $():

#!/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脚本变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆