我是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
Crossposting:http://unix.stackexchange.com/q/164328/74329 – Cyrus 2014-10-26 15:47:10