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
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.
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!
You can use variables as in any programming languages. There are no data types. A variable in bash can contain a number, a character, a string of characters. You have no need to declare a variable, just assigning a value to its reference will create it.
$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.
To set an environment variable everytime, use the export command in the . bashrc file (or the appropriate initialization file for your shell). To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With