忽略Bash for循环中的字符串比较

时间:2017-06-14 17:03:26

标签: bash

我正在尝试重命名一些文件,其他文件的信息具有相同的名称但其他结尾。完全忽略for循环中的字符串比较。无论我使用==,哪个应该给我想要的结果,或者!=,这应该不返回任何内容。这是一个示例脚本:

for file in *.bz2; do
fullname="$file"
extension="${fullname#*.}"
filename="${fullname%.out.*}"
    for tfile in *.txt; do
        tfullname="$tfile"
        tfilename="${tfullname%.*}"
        if [ $filename==$tfilename ]; then # this satement is ignored, also when I try  != it gives me the same result!
            echo $tfilename
            wcn=$(wc -l "$tfullname")
            n=$(expr "$wcn" : '^[^0-9]*\([0-9]*\)')
            echo "mv -i "${fullname}" "${filename}_files_${n}.${extension}""  
        fi
    done
done

这是输出,与if语句中的表达式无关:

text1
mv -i text1.out.bz2 text1_files_3.out.bz2
text2
mv -i text1.out.bz2 text1_files_7.out.bz2
text3
mv -i text1.out.bz2 text1_files_8.out.bz2
text1
mv -i text2.out.bz2 text2_files_3.out.bz2
text2
mv -i text2.out.bz2 text2_files_7.out.bz2
text3
mv -i text2.out.bz2 text2_files_8.out.bz2
text1
mv -i text3.out.bz2 text3_files_3.out.bz2
text2
mv -i text3.out.bz2 text3_files_7.out.bz2
text3
mv -i text3.out.bz2 text3_files_8.out.bz2

这将是所需的输出:

text1
mv -i text1.out.bz2 text1_files_3.out.bz2
text2
mv -i text2.out.bz2 text2_files_7.out.bz2
text3
mv -i text3.out.bz2 text3_files_8.out.bz2

我做错了什么?

0 个答案:

没有答案