55

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.

I get an error saying that -f is not found.

I get an error saying there is an unexpected end of file.

I made the file and did a 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!

Jahid
  • 21,542
  • 10
  • 90
  • 108
cohortq
  • 677
  • 1
  • 5
  • 10

4 Answers4

98

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
Isaac
  • 10,668
  • 5
  • 59
  • 68
  • 3
    Back ticks are evil (non-nestable). I'd use $() myself. But +1 for a good answer. – paxdiablo Mar 17 '10 at 01:25
  • @paxdiablo I'm very slowly learning to use `$()` myself, but it's a little hard to reprogram a 15-year-old habit. – Isaac Mar 17 '10 at 01:33
  • @Isaac: it is so worth it. I wrote a lint script that periodically crawls `~/bin` and yammers about backticks. Expunge them! – Norman Ramsey Mar 17 '10 at 01:35
  • Oh really ` is different from '. that's really good to know. – cohortq Mar 17 '10 at 01:36
  • 2
    It's worth pointing out that backticks can actually be nested with backslash-escaping, but it becomes very ugly very quickly so `$()` is a much better choice. – Isaac Mar 17 '10 at 01:42
  • Done. Will never use backticks. am using the $() syntax now. Thanks! – cohortq Mar 17 '10 at 01:47
32

Five problems:

  1. Don't put a space before or after the equal sign.
  2. Use "$(...)" to get the output of a command as text.
  3. [ is a command. Put a space between it and the arguments.
  4. Commands are case-sensitive. You want echo.
  5. Use double quotes around variables. rm "$folderToBeMoved"
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    +1, that pretty much covers it. #4 isn't accurate for most OS X systems though. Since the standard setup has a case-insensitive filesystem, ECHO will work just fine. I still wouldn't recommend it, but it's not going to cause a problem with this script. – Carl Norum Mar 17 '10 at 01:16
  • The default filesystem on Mac OS X is case-insensitive. `ECHO` works just as well as `echo`, although for portability it is not a wise idea. – dreamlax Mar 17 '10 at 01:16
  • 2
    Fair enough, but then `ECHO` executes the executable, and not the bash built-in. – Ignacio Vazquez-Abrams Mar 17 '10 at 01:17
7

folder = "ABC" tries to run a command named folder with arguments = and "ABC". The format of command in bash is:

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]

Generally for cases like this, paste your code in shellcheck and see the feedback.

Jahid
  • 21,542
  • 10
  • 90
  • 108
5

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
ghostdog74
  • 327,991
  • 56
  • 259
  • 343