You are not logged in.
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
Replace & with && and it will work as expected.
Offline
Sorry, nothing to see here.
Last edited by karol (2010-07-24 11:07:30)
Offline
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
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
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
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
thanks bernarcher your method works perfectly!
Offline
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
fsckd, that makes sense.
thanks
Offline