2014-10-26 67 views
0

我是bash的新手,我正在处理遍历tar.gz文件存档的脚本,并在每个文件中将指定的字符串更改为另一个字符串。脚本的参数:存档的名称,搜索的字符串,目标词。我有以下错误: 在线if [ ! -f $filename ] ; then [我的问题是,当存档名称包含空格(例如我运行具有以下参数脚本:> change_strings.sh“/tmp/tmp.m7xYn5EQ2y/work/data txt”a A)我有以下错误: 在线if [ ! -f $filename ] ; then [ :data:预期的二元运算符,dirname:额外的操作数'txt'。 这里是我的代码:目录名称Bash中的空格

#!/bin/bash 
    filename="${1##*/}" 
    VAR="$1" 
    DIR=$(dirname ${VAR}) 
    cd "$DIR" 


if [ ! -f $filename ] ; then 
echo "no such archive" >&2 
exit 1 
fi 


if ! tar tf $filename &> /dev/null; then 
echo "this is not .tar.gz archive" >&2 
exit 1 
fi 


dir=`mktemp -dt 'test.XXXXXX'` 
tar -xf $filename -C $dir #extract archive to dir 
cd $dir #go to argument directory 

FILES=$dir"/*" 

for f in $FILES 
do 
sed -i "s/$2/$3/g" "$f" 
done 

tar -czf $filename * #create tar gz archive with files in current directory 
mv -f $filename $cdir"/"$filename #move archive 
rm -r $dir #remove tmp directory 
+1

Crossposting:http://unix.stackexchange.com/q/164328/74329 – Cyrus 2014-10-26 15:47:10

回答

1

来处理这个正确的方法是用双引号包围你的变量。

var=/foo/bar baz 
CMD $var # CMD /foo/bar baz 

上面的代码将上/富/ bar和baz

CMD "$var" 

这将在 “/富/酒吧baz” 的执行CMD执行CMD。在大多数地方总是用双引号括住变量是一种最佳做法。

1

欢迎使用stackoverflow!

对于当前和未来的读者的方便,这里的显示问题small, self contained example

filename="my file.txt" 
if [ ! -f $filename ] 
then 
    echo "file does not exist" 
fi 

下面是我们得到的输出:

$ bash file 
file: line 2: [: my: binary operator expected 

下面是我们所期望得到的输出:

file does not exist 

他们为什么不一样?


这里是shellcheck不得不说一下吧:

$ shellcheck file 
In file line 2: 
if [ -f $filename ] 
     ^-- SC2086: Double quote to prevent globbing and word splitting. 

而事实上,如果我们双引号呢,我们得到的预期输出:

$ cat file 
filename="my file.txt" 
if [ ! -f "$filename" ] 
then 
    echo "file does not exist" 
fi 

$ bash file 
file does not exist 

你应该double quoting all your variables

但是,您必须注意$FILES,因为它包含您想要扩展的glob /通配符,以及您不想拼写的潜在空格。最简单的方法是只不把它放在一个变量,而是写出来:

for f in "$dir"/* 
do 
    ... 
相关问题