Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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 wrote a fairly simple backup script for my Linux machine in bash. Essentially it is a recursive script that looks at all the files in the directory, sees if they exist in the backup, then sees if they are newer than the backup, and then backs them up if necessary. So, here is the one problem I can't yet solve...
When the script is called it takes from two to an infinite number of arguments. The first is the backup destination, and the following arguments are sources. It goes through this list with the $@ variable in a for loop. When an argument is a directory, it calls itself with the appropriately altered destination directory and a /* added on to the source directory. So, for example, if I wanted to backup /source to /backup and one of the arguments was a subdirectory /source/sub then this is how it would work...
Code:
# First I call the script
backup /backup /source/*
# One of the files in /source/* is the subdirectory /source/sub, so the script calls itself recursively like so...
backup /backup/sub /source/sub/*
# And so on
So, this is the way it works essentially. Now, before all this occurs there's a test to see if the arguments exist (to catch human error, of course). The problem I'm having is that if these files have two consecutive spaces in their names, they return a false value in the test. So, if the variable that we're testing is $arg, this is how I have the test written...
Code:
if [ -e "$arg" ] ; then
...
else
echo "$arg does not exist!"
fi
I have the double quotes so that files with any spaces at all are valid. I just don't know why files with two spaces aren't. Also, when it outputs the error, the error does NOT display two spaces, but only one. I don't know why this is. I can't tell if the problem is in getting the value of $arg (i.e. the variable never gets two spaces), if it's in the test, or in what * returns or what. I'm kind of a newbie at this. Any recommendations?
I read over the thread, but I didn't see anything that solved my problem. The issue is that if the argument in the command line (in this case the return value of the wildcard *) has two spaces in it, when testing that the file exists (which is necessary in case the file name was typed explicitly by the user, and it was inaccurate), it ends up evaluating to false when the file does exist.
Sorry for any confusion. I realize this is probably not the best description, but it's the best I can do. :/
Well, you'll basically need to put the $VARIABLE
that holds the value of the file/direcotry name into
Quotes ("$VARIABLE") so the string with spaces
gets treated like ONE value...
That said, spaces in file-names are a MS abomination
and shouldn't be used ;)
I found the solution. I had a line in the script to remove trailing slashes from any directories, and it was cutting the second space out of the file names. The line was something like this...
Code:
echo $arg | sed 's/\/$//'
and I added quotes around the variable to fix the problem, like so...
Code:
echo "$arg" | sed 's/\/$//'
Thanks for the help, Tinkster.
EDIT: (Post Script, actually)
I'm backing up files on our shared network drive. That means that my wife has files on the drive. That means I'm lucky there aren't files with slashes, asterisks, tabs, etc in them. I have to assume the worst when dealing with her file names.
P.S.: The downside of people not knowing anything
about underlying file-systems is utter stupidity when it
comes to naming conventions of files ;) I had that guy
ask me why the hell he couldn't use that back-slash
in his filename in Word :D
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.