You are not logged in.

#1 2013-06-11 13:01:54

mibadt
Member
Registered: 2009-09-25
Posts: 377

Need help with BASH script

Hello,
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!

----------the script--------------

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

-------the output----------

$ ./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

Best regards,
Michael Badt

Offline

#2 2013-06-11 13:41:07

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,966
Website

Re: Need help with BASH script

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)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2013-06-11 13:41:47

geno.nullfree
Member
From: en_US
Registered: 2013-05-29
Posts: 17

Re: Need help with BASH script

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.

Helpful:
[http://ss64.com/bash/read.html]
[http://ss64.com/bash/echo.html]

Offline

#4 2013-06-11 13:51:14

mibadt
Member
Registered: 2009-09-25
Posts: 377

Re: Need help with BASH script

Thanks a lot both of you for the helpful comments and proposals!

Regards

Michael Badt


Best regards,
Michael Badt

Offline

#5 2013-06-11 14:00:26

oliver
Member
Registered: 2007-12-12
Posts: 417

Re: Need help with BASH script

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.

Offline

#6 2013-06-11 14:25:19

mibadt
Member
Registered: 2009-09-25
Posts: 377

Re: Need help with BASH script

Thanks again!


Best regards,
Michael Badt

Offline

#7 2013-06-11 14:56:46

Mektub
Member
From: Lisbon /Portugal
Registered: 2008-01-02
Posts: 647

Re: Need help with BASH script

mibadt,

to validate a bash script you can use;

http://www.shellcheck.net

Mektub


Follow me on twitter: https://twitter.com/johnbina

Offline

#8 2013-06-11 18:48:05

mibadt
Member
Registered: 2009-09-25
Posts: 377

Re: Need help with BASH script

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!


Best regards,
Michael Badt

Offline

Board footer

Powered by FluxBB