You are not logged in.

#1 2010-07-24 10:46:35

scarletxfi
Member
From: Australia
Registered: 2010-04-19
Posts: 105

my first bash script - simple backup

so I wrote this script that makes a backup of a folder from one path to another.
It's a bit buggy (first bash script ever)

I would appreciate any comments

it seems to work, the files get copied\removed but the "echos" are sometimes a bit off..

#!bin/sh

#remove old backup
echo removing old backup &
#exec rm -rf -R /path/to/backup/
exec /bin/rm -rf /path/to/remove/ &
echo copy removed &
echo copying &
#exec cp -R -v /path/to/location /path/to/backup > record.txt
echo done, bye bye &
exit

Offline

#2 2010-07-24 11:03:17

Media
Member
Registered: 2010-07-24
Posts: 2

Re: my first bash script - simple backup

Replace & with && and it will work as expected.

Offline

#3 2010-07-24 11:04:54

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: my first bash script - simple backup

Sorry, nothing to see here.

Last edited by karol (2010-07-24 11:07:30)

Offline

#4 2010-07-24 11:10:00

scarletxfi
Member
From: Australia
Registered: 2010-04-19
Posts: 105

Re: my first bash script - simple backup

right... I wasn't sure where to add them but it seem to work ok now.. thanks!

Last edited by scarletxfi (2010-07-24 11:13:55)

Offline

#5 2010-07-24 11:12:09

Media
Member
Registered: 2010-07-24
Posts: 2

Re: my first bash script - simple backup

scarletxfi wrote:

I also thought so.. but adding "&&" renders the program useless (ie it doesn't work at all..)

Use IF .. ELSE ? && works as AND ..
echo is a bit off because & means that you send it to the background and continue with the next line without waiting for the previous command to be executed / exited.

Last edited by Media (2010-07-24 11:16:48)

Offline

#6 2010-07-24 11:27:37

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: my first bash script - simple backup

scarletxfi wrote:

it seems to work, the files get copied\removed but the "echos" are sometimes a bit off..

1. There is no need to background (append "&") the echo commands.
2. For backgrounded command sequences keep message and command together, link them with && and group with brackets when backgrounding, i.e.

( command && echo "success" ) &

3. exec usually is not needed
4. Do not background if commands should run sequentially
5. No explicit exit command needed at the end of the script
6. Better use strings as arguments to echo

Thus (not tested at all):

#!bin/sh

#remove old backup
echo "removing old backup"
#rm -rf -R /path/to/backup/
rm -rf /path/to/remove/
echo "copy removed"
echo "copying"
#cp -R -v /path/to/location /path/to/backup > record.txt
echo "done, bye bye"

To know or not to know ...
... the questions remain forever.

Offline

#7 2010-07-24 11:31:44

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: my first bash script - simple backup

Media wrote:

Use IF .. ELSE ? && works as AND ..

&& in fact is a "hidden if-else conditional" as it fires the second part only if the first one was successfully completed (i.e. returned a zero value in $?).


To know or not to know ...
... the questions remain forever.

Offline

#8 2010-07-24 11:37:40

scarletxfi
Member
From: Australia
Registered: 2010-04-19
Posts: 105

Re: my first bash script - simple backup

thanks bernarcher your method works perfectly!

Offline

#9 2010-07-24 11:50:30

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: my first bash script - simple backup

To clarify further, exec replaces the current shell (that is bash) with the specified executable. Any commands after the exec statement will never be executed.

Last edited by fsckd (2010-07-24 11:51:19)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#10 2010-07-24 12:04:15

scarletxfi
Member
From: Australia
Registered: 2010-04-19
Posts: 105

Re: my first bash script - simple backup

fsckd, that makes sense.

thanks

Offline

Board footer

Powered by FluxBB