ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm a newbie at scripting so was trying to adapt this script I found at:
.linuxplanet.com/linuxplanet/tutorials/5257/3/
when I try and run it I get errors "unrecognized command" which is
to do with the blank lines. I removed the blank lines and get a syntax error
scripts/backup.sh: line 37: syntax error near unexpected token `fi'
scripts/backup.sh: line 37: `fi"
#!/bin/bash
## This is the famous "sha-bang" statement,
## that tells the shell which command
## interpreter to use. See man magic
## to learn the inner workings of sha-bang.
## this script uses the tar, mkdir, and cp commands
## plus Bash's conditional statements
## smart admins write lots of comments
# test for existing backup directory
# if it does not exist, create it
# -e is the Bash way of asking "does this file exist"
# $ means variable substitution.
if [ -e /mnt/backup/1backups ]
then
echo "The backup directory exists."
else
echo "A backup directory does not exist, and will be created."
mkdir /mnt/backup/1backups
echo "A backup directory has been created"
fi
# now we define our own variables
# location of files to backup
FILES=/usr/local/rpm /etc/samba /etc/httpd
# name of compressed archive
ARCHIVENAME=backups.tgz
# location of backup directory
BACKUPDIR=/mnt/backup/
# create compressed archive, copy to backup directory
tar czf $ARCHIVENAME $FILES
cp -ap $ARCHIVENAME $BACKUPDIR
if [ -e $BACKUPDIR/$ARCHIVENAME ]
then
echo "Jolly good show, the backup worked!"
else
echo "Dagnabit, the backup failed. Time to debug."
fi
The only problems I see in the script as posted is that some of the variables in the script might point to files or directories that don't exist of that you don't have permission to. Check out the vaiables
This is the original script, the previous post is my adjusted script.
#!/bin/bash
## This is the famous "sha-bang" statement,
## that tells the shell which command
## interpreter to use. See man magic
## to learn the inner workings of sha-bang.
## this script uses the tar, mkdir, and cp commands
## plus Bash's conditional statements
## smart admins write lots of comments
# test for existing backup directory
# if it does not exist, create it
# -e is the Bash way of asking "does this file exist"
# $ means variable substitution. HOME is a builtin
# Bash variable.
if [ -e $HOME/1backups ]
then
echo "The backup directory exists."
else
echo "A backup directory does not exist, and will be created."
mkdir $HOME/1backups
echo "A backup directory has been created"
fi
# now we define our own variables
# location of files to backup
FILES=$HOME/archive-files
# name of compressed archive
ARCHIVENAME=backups.tgz
# location of backup directory
BACKUPDIR=/archive/carla/
# create compressed archive, copy to backup directory
tar czf $ARCHIVENAME $FILES
cp -ap $ARCHIVENAME $BACKUPDIR
if [ -e $BACKUPDIR/$ARCHIVENAME ]
then
echo "Jolly good show, the backup worked!"
else
echo "Dagnabit, the backup failed. Time to debug."
fi
I've got a question: how did you get the script? Did you download it? Did you type it up?
The reason I ask is this. You say you received "unrecognized command" errors with the original script. That's really, really odd. One possible explanation is the difference in line endings between DOS/Windows and Linux. If you downloaded the file, and it was originally written in Windows, then that's a problem. If you typed it up in Windows and transferred it to Linux (by any of: shared partition, network transfer, or floppy disk), then that's the same problem.
If this is the case, you can easily fix the problem by using this command: dos2unix filename
That will convert the line endings from Windows to Linux.
That's a good point, but his script made it to line 37. I take this to mean that there is some other problem. I know it can be really frustrating to work with shell scripts...
Thanks for the replies, the dos2unix did the trick.
Script ran but complained about the samba dir ?
[root@jekyll scripts]# sh backup.sh
A backup directory does not exist, and will be created.
A backup directory has been created
backup.sh: line 24: /etc/samba: is a directory
tar: Cowardly refusing to create an empty archive
Try `tar --help' for more information.
cp: cannot stat `backups.tgz': No such file or directory
Dagnabit, the backup failed. Time to debug.
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,786
Rep:
Although without relation to the error message, the line:
FILES=/usr/local/rpm /etc/samba /etc/httpd
should be
FILES="/usr/local/rpm /etc/samba /etc/httpd"
The book Learning the bash shell (O'Reilly, by C. Newham & B. Rosenblatt) is a good resource for everyone learning shell scripting.
I would also recommend sed & awk (O'Reilly, by D. Dougherty & A. Robbins) or a good perl book (O'Reilly again) for some more advanced scripting.
Sed, perl and awk are all very good at handling text, and that's basically what shell-scripting is all about: Running commands based on certain presumptions and taking actions accordingly. This often involves formatting of the output from commands so that the next command or function can work with it.
1) sh, ksh, and bash don't allow unquoted spaces in shell-variable assignments.
So:
FILES=/usr/local/rpm /etc/samba /etc/httpd
will set FILES to "/usr/local/rpm" and then try to execute:
/etc/samba /etc/httpd
as a command. Old idiom from /bin/sh days. Bad then. Confusing today.
Change it to:
FILES="/usr/local/rpm /etc/samba /etc/httpd"
2) bash on linux has a hang-up with ! (exclamation point) inside double-quotes. Try using single quotes.
Try changing:
echo "Jolly good show, the backup worked!"
to:
echo 'Jolly good show, the backup worked!'
It has to do with C-shell like command history I believe.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.