采用:
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
,但您可以使用 just 运行它,bash
并且您只会跳过一点输出,因为它set -x
在子 shell 代码中包含 a 。
+ 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