I'm new to scripting, so please don't be too harsh...
I've tried to create a bash script which:
a. enables me to select 1 out of 3 cameras (actually memory cards used in different cameras).
b. enables me to select 1 out of 2 backup destinations
c. uses rsync to backup these
I'm having multiple problems as can be seen from the output, and I'd appreciate your help. The rsync related error stems from the fact that the memory card wasn't mounted. But that's the single error I know how to solve...
Thanks a lot up front!
#!/bin/bash read -p "Camera: 1-D800, 2-D300, 3-P7100 " CAMERA echo -n #read -p camera echo -n "selected $CAMERA" case CAMERA in [1-1]*) SOURCE="/run/media/miki/NIKON\ D800/DCIM" ;; [2-2]*) SOURCE="/run/media/miki/NIKON\ D300/DCIM" ;; [3-3]*) SOURCE="/run/media/miki/NIKON\ P7100/DCIM" ;; esac echo -n "Selected source camera is $SOURCE" echo -n read -p "Destination: 4-Linux, 5Windows" DEST case DEST in [4-4]*) TARGET="/home/miki/photos/" ;; [5-5]*) TARGET="/windows/photos/" ;; esac echo -n "Selected target is $TARGET" read p "Back up photos? y/n " if [ "$REPLY" != "y" ]; then rsync -av "$SOURCE/*" "$TARGET/" echo -n "Photos copied " echo -n "remaining space " df -h | grep home df -h | grep windows fi exit
$ ./photo Camera: 1-D800, 2-D300, 3-P7100 2 selected 2Selected source camera is Destination: 4-Linux, 5Windows4 Selected target is y ./photo: line 28: read: `Back up photos? y/n ': not a valid identifier sending incremental file list rsync: link_stat "/*" failed: No such file or directory (2) sent 12 bytes received 12 bytes 48.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9] Photos copied remaining space /dev/sda3 103G 7.2G 91G 8% /home /dev/sda1 112G 43G 69G 39% /windows
There are several typos which lead to issues. First, the variables in the case statements need to be expanded, otherwise it will always go to the default option:
- case CAMERA in + case $CAMERA in
But, the error that it's telling you about on line 28 is ... on line 28. The p flag to read doesn't have the dash:
- read p "Back up photos" + read -p "Back up photos"
Also, you don't store the answer to that question in a variable, instead you use REPLY - it is better style to be consistent. Either use a variable name for all reads, or use REPLY for all.
Last edited by Trilby (2013-06-11 13:43:09)
I think the lines:
read p "Back up photos? y/n " if [ "$REPLY" != "y" ]; then
should actually be
read -p "Back up photos? y/n " if [ "$REPLY" != "n" ]; then
with a '-p' and a 'n'
This is only helping the logic of the script, and only the first thing that jumped out at me. The lack of newlines makes me cringe (I personally would get rid of all of the '-n' to make the output a bit more readable...and it looks like you are using the 'echo's with the intentions of creating newlines, but using the -n to cancel that out. every line that is 'echo -n' isnt actually doing anything.), though it will not impair the function of it.
Overall, nice script, hopefully you can get it working the way you want.
Thanks a lot both of you for the helpful comments and proposals!
A couple of options you might want to add are:
set -o nounset
This will cause the script to exit if a variable is not set
set -o errexit
This will cause the script to exit if any statement returns a non-true value.
Thanks again all of you for your help.
Good to know about the spell check site!
BTW, thanks to you, my scripts now works as planned!