使用:
mv "$onam" "$dnam"
基本上,在包含可能包含空格的文件名的变量周围使用引号.但是,除非文件名或目录名本身包含引号,否则这些值本身不应包含引号.
不幸的是,我不能立即想到一个简单的方法来使用像$1这样的值和硬编码的引号.你必须弄乱eval或其他地方.
此代码与您显示的代码同构,但实际上更多.运行和清理自己也是安全的.它假设您正在使用bash.请注意,带空格的名称在一行中包含多个空格;如果您尝试使用包含单个空格的名称,很容易被误导为虚假的安全感.
#!/bin/bash
testdir=test.$$
mkdir "$testdir" || exit 1
trap "rm -fr '$testdir'; exit 1" 0 1 2 3 13 15
(
set -x
x=$'\n\n'
cd test.$$
basedir="$PWD"
sub1dir="rename test"
sub2dir="Test Folder"
subdir="$sub1dir/$sub2dir"
mkdir -p "$basedir/$subdir"
oldfile="test file"
cp /etc/group "$basedir/$subdir/$oldfile"
echo "$x"
ls -lR .
echo "$x"
newfile=$(echo "$oldfile" | sed 's/ /-/g')
mv "$basedir/$subdir/$oldfile" "$basedir/$subdir/$newfile"
ls -lR .
echo "$x"
new2dir=$(echo "$sub2dir" | sed 's/ /-/g')
mv "$basedir/$subdir" "$basedir/$sub1dir/$new2dir"
ls -lR .
echo "$x"
new1dir=$(echo "$sub1dir" | sed 's/ /-/g')
mv "$basedir/$sub1dir" "$basedir/$new1dir"
ls -lR .
)
rm -fr "$testdir"
trap 0
# $mv "/home/bryan/renametest/TestFolder/test file" "/home/bryan/renametest/TestFolder/test---file"
#
#Here are some inputs and results.
#
# mv $1 $2
# mv: cannot stat `"/home/bryan/renametest/TestFolder/test file"': No such file or directory
#
# mv $onam $dnam
# mv: cannot move `/home/bryan/renametest/TestFolder' to a subdirectory of itself, `/home/bryan/renametest/TestFolder/test---file'
#
# mv $1 $dnam
# mv: cannot stat `"/home/bryan/renametest/TestFolder/test file"': No such file or directory
#
# mv $onam $2
# mv: cannot move `/home/bryan/renametest/TestFolder' to `"/home/bryan/renametest/TestFolder/test---file"': No such file or directory
#
#Ideas?
你必须要小心;请注意我是如何从片段中建立名称并对其进行分解等的.但是要小心,它可以完成.
示例输出
使用bash -x运行,但你可以只用bash运行它,你只需跳过一点输出,因为它在子shell代码中包含一个set -x.
+ testdir=test.47648
+ mkdir test.47648
+ trap 'rm -fr '\''test.47648'\''; exit 1' 0 1 2 3 13 15
+ set -x
+ x='
'
+ cd test.47648
+ basedir=/Users/jleffler/tmp/soq/x3/test.47648
+ sub1dir='rename test'
+ sub2dir='Test Folder'
+ subdir='rename test/Test Folder'
+ mkdir -p '/Users/jleffler/tmp/soq/x3/test.47648/rename test/Test Folder'
+ oldfile='test file'
+ cp /etc/group '/Users/jleffler/tmp/soq/x3/test.47648/rename test/Test Folder/test file'
+ echo '
'
+ ls -lR .
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 rename test
./rename test:
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 Test Folder
./rename test/Test Folder:
total 8
-rw-r--r-- 1 jleffler staff 2151 Apr 3 18:43 test file
+ echo '
'
++ echo 'test file'
++ sed 's/ /-/g'
+ newfile=test---file
+ mv '/Users/jleffler/tmp/soq/x3/test.47648/rename test/Test Folder/test file' '/Users/jleffler/tmp/soq/x3/test.47648/rename test/Test Folder/test---file'
+ ls -lR .
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 rename test
./rename test:
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 Test Folder
./rename test/Test Folder:
total 8
-rw-r--r-- 1 jleffler staff 2151 Apr 3 18:43 test---file
+ echo '
'
++ echo 'Test Folder'
++ sed 's/ /-/g'
+ new2dir=Test---Folder
+ mv '/Users/jleffler/tmp/soq/x3/test.47648/rename test/Test Folder' '/Users/jleffler/tmp/soq/x3/test.47648/rename test/Test---Folder'
+ ls -lR .
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 rename test
./rename test:
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 Test---Folder
./rename test/Test---Folder:
total 8
-rw-r--r-- 1 jleffler staff 2151 Apr 3 18:43 test---file
+ echo '
'
++ echo 'rename test'
++ sed 's/ /-/g'
+ new1dir=rename--test
+ mv '/Users/jleffler/tmp/soq/x3/test.47648/rename test' /Users/jleffler/tmp/soq/x3/test.47648/rename--test
+ ls -lR .
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 rename--test
./rename--test:
total 0
drwxr-xr-x 3 jleffler staff 102 Apr 3 18:43 Test---Folder
./rename--test/Test---Folder:
total 8
-rw-r--r-- 1 jleffler staff 2151 Apr 3 18:43 test---file
+ rm -fr test.47648
+ trap 0