简体   繁体   中英

Copy File: Name Contains Spaces

I have a large shell script that processes files each of my Solaris systems.

In the beginning the script creates a variable FILENAME Sometimes people create directories/files that contain spaces. eg

/users/ldap/Anukriti's System Backup/BACKUP/workspace/BP8/scripts/yui/editor/simpleeditor.js

Later in the script I run

cp $FILENAME $DESTDIR/

As you can imagine this always fails because the following is invalid.

cp /users/ldap/Anukriti's System Backup/BACKUP/workspace/BP8/scripts/yui/editor/simpleeditor.js $DESTDIR

I have tried putting the Variable in Quotes, but this is not working. I have used find with -exec option before, but for this circumstance that is not really an option, especially since Solaris does not support the -wholename or -path options

What can i do here?

You just have to protect the variables with quotes :

cp "$FILENAME" "$DESTDIR"

NOTE

Don't use single quotes ' , the variables can't be expanded this way.

看起来我需要使用花括号进行变量扩展和双引号

cp "${FILENAME}" $DESTDIR

Make sure that

  • $DESTDIR exists
  • is a directory
  • and yes, use double quotes for both variables and get rid of the trailing / .

You might not believe it, but that is your problem. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM