0

I am trying to rename some files, with the information of other files with the same name but other ending. The string comparison within the for loops is completely ignored. No matter if I use ==, which should give me the desired result or !=, which should return nothing. Here is an example script:

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

Here is the output, independent of the expression in the if statement:

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

And here would be the desired output:

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

What am I doing wrong?

codeforester
  • 39,467
  • 16
  • 112
  • 140
user4050567
  • 87
  • 10
  • 2
    I believe spaces matter, change `$filename==$tfilename` to `$filename == $tfilename`. – Jonny Henly Jun 14 '17 at 17:05
  • 2
    And `==` isn't guaranteed to work either. A corrected version of this statement would be `[ "$filename" = "$tfilename" ]`, with the quotes -- `=` is the only string comparison operator in [the POSIX spec for `test`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html). – Charles Duffy Jun 14 '17 at 17:09
  • 2
    Check your script at [shellcheck](http://shellcheck.net) – codeforester Jun 14 '17 at 17:09
  • @Jonny Henly Indeed, That was the issue! Thanks a lot! – user4050567 Jun 14 '17 at 17:09
  • (if it *looks* like it works without the quotes, then you aren't testing with interesting enough filenames -- try creating some with spaces. And if `==` as opposed to `=` is working, then you're using a shell with non-standard-mandated extensions, but your code will break if run on a baseline implementation). – Charles Duffy Jun 14 '17 at 17:12
  • @CharlesDuffy I tried your solutions as well, with quotes, without, with == and with =, all of them work! Seems like it was all about the spaces. – user4050567 Jun 14 '17 at 17:15
  • @user4050567, as I said, they only all work if your data and environment are insufficiently interesting. Tests without the quotes **will** fail if you create and test with filenames containing spaces. Tests using `==` as an operator **will** fail if you test with a shell that doesn't support it (such as busybox dash). – Charles Duffy Jun 14 '17 at 17:16
  • 1
    @user4050567, ...I'm making that point to make it clear that shell has hidden gotchas and pitfalls -- it's thus important to use tools such as http://shellcheck/, rather than trusting that because something works for you when you test it it'll always work. (On which point -- see [BashPitfalls](http://mywiki.wooledge.org/BashPitfalls)). – Charles Duffy Jun 14 '17 at 17:18
  • 1
    @codeforester & CharlesDuffy, just tried shellcheck, amazing tool, didn't know about it before, thanks a lot! – user4050567 Jun 14 '17 at 17:20

0 Answers0