mv包含shell脚本空格的文件

时间:2013-04-04 00:08:36

标签: linux shell mv

我正在尝试mv一个文件(将其重命名为用短划线替换空格)。如果我在没有引号的情况下传递source和dest参数,则源会被分解,因为mv认为它是参数的结尾。

如果我用参数包围source和dest,mv会报告无法找到源代码。目前,$ 1和$ 2在开头和结尾都有硬编码的报价。 $ onam和$ dnam是没有硬编码报价的等价物。

来自提示的此命令可以正常工作,因为source和dest被引号括起来:

mv "/home/bryan/renametest/TestFolder/test file" "/home/bryan/renametest/TestFolder/test---file"

这是我到目前为止所尝试的(输入和结果)

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

如何使用空格移动这些文件?

1 个答案:

答案 0 :(得分:4)

使用:

mv "$onam" "$dnam"

基本上,在包含可能包含空格的文件名的变量周围使用引号。但是,除非文件名或目录名本身包含引号,否则这些值本身不应包含引号。

不幸的是,我无法立即想到一种简单的方法来使用像$1这样的值以及围绕它们硬编码的引号。你必须弄乱eval或其他地方。


SSCCE(Short, Self-Contained, Correct Example

此代码与您展示的代码同构,但实际上更多。运行和清理自己也是安全的。它假设您使用的是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