0

I have to write a script that accepts 1+ source files and destination directory as arguments. I've attempted to write an error message for filenames that contain spaces, but its getting an error when I enter more than 1 argument with a space in between as well. Any help would be appreciated.. This is what I wrote so far:

if [ "$#" -eq "$(echo "$@" | wc -w)" ]
    then
    echo "Invalid arguments. Filenames may not contain spaces."
    echo "usage: bkup file1 [file2...] bkup_directory"
    exit 13
fi
2
  • 1
    How is the script supposed to know the difference between two filenames separated by a space, and one filename containing a space? Commented Dec 3, 2013 at 0:01
  • 1
    If you quote argument parameters properly throughout your script, the user can specify filenames with spaces properly on the command line (quoting and/or escaping,) and you can act on them correctly. Commented Dec 3, 2013 at 0:32

1 Answer 1

1

You MAY want to try:

if  [ $filename == "*" "*" ]; then
      echo your error message here
fi

for the testing of spaces in filenames.

As for actually getting the args

$1 is the variable for the first arg and $2 for the second and so on so something like this might work:

if [ $2 == "*" ]; then
       echo NO NO NO NOT TODAY!!!
fi

this would check to see if the second argument says anything at all and so if it did we can assume that it is part of the file name (THIS WOULD ONLY WORK IF THE FILENAME IS THE LAST ARGUMENT) OR:

if [ $2 == "*" ]; then
   fil=$1 + \
   filename=&fil + $2
fi

this would automatically change their filename into an acceptable format for the system an you would not need an error message.

but i am also new to bash so this could not be what you are looking for or I could have the right idea and all this could be complete whooey..... But if I helped then I'm glad I could.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.