More Offshore Stupidity



  • More offshore stupidity from the same geniuses that brought you this...

    I had written a ksh script to perform a couple of tasks, and needed a new script to perform another, similar, task. I asked our offshore associates to do the work, and told them to model it after the script that I had written. In my script, I used the construct:

    BaseFileName="${FileNameAndPath##*/}
    

    which is an internal ksh way of doing:

    BaseFileName=`basename ${FileNameAndPath}`
    

    For those not familiar with ksh, a "##" following a variable name means remove the longest leading text that matches the following regular expression. How many WTF's can you find?

    while [[ `echo "${File}" | grep -c "/"` -gt 0 ]]; do      # while there are more path-nodes
          File=`echo ${File} | sed -e 's/^\(.\)\(.*\)/\2/'`   # strip the leading character
    done
     


  • Sorry, didn't notice the missing trailing double quote on the first code snippet until after the edit timeout...



  • WTF1. A loop where a single statement suffices (something like sed 's/^.*\///' to use their idiom).

    WTF2. Raking out a single character at a time! This loop might have to be iterated hundreds of times for long path names...

    WTF3 (perhaps). Doesn't ksh have match operators in conditions?

    How many can you find? 



  • while [[ `echo "${File}" | grep -c "/"` -gt 0 ]]; do      # while there are more path-nodes
          File=`echo ${File} | sed -e 's/^\(.\)\(.*\)/\2/'`   # strip the leading character
    done
    1. <FONT face="Courier New">exec'ing 4 tasks each time through loop (echo, grep, echo again, sed)</FONT>
    2. <FONT face="Courier New">removing one char at a time instead of slurping the entire path up to the last "/"</FONT>
    3. <FONT face="Courier New">using the external command "echo" instead of the internal "print"</FONT>
    4. <FONT face="Courier New">not asking how something that already worked and did what they needed worked</FONT>
    5. <FONT face="Courier New">inventing something new even though they had working code, and access to its author</FONT>
    6. <FONT face="Courier New">being able to understand complex sed regex's and not knowing that what they wrote was way too complicated given what it did, and that there had to be a better way</FONT>

    <FONT face="Courier New">And yes, ksh does have internal string manipulations (e.g.: the one I used in my template script), and they can be used anywhere you reference a variable.</FONT>

    <FONT face="Courier New">For example: </FONT>

    <FONT face="Courier New">if [[ "${FilePathAndName##*/}" = "xxx.txt" ]]; then             # use internal string manipulation</FONT>

    <FONT face="Courier New">which is equivalent to:</FONT>

    <FONT face="Courier New">if [[ "$(basename ${FilePathAndName})" = "xxx.txt" ]]; then     # new way, but uses external command</FONT>

    <FONT face="Courier New">which is equivalent to;</FONT>

    <FONT face="Courier New">if [[ `basename ${FileNameAndPath}` = "xxx.txt" ]]; then        # old way, using external command</FONT>



  • TRWTF is the idiosynracies and syntax of Unix shell scripts. I'm tempted to say it's worse than Perl.



  • @TGV said:

    WTF2. Raking out a single character at a time! This loop might have to be iterated [b]hundreds[/b] of times for long path names...

    Clearly you haven't heard tales of the app I worked on with 4K URLs.  Or pretty much any Java source code.....

    /vobs/main/project/src/java/com/wtf/enterprise/message/format/impl/WTFEnterpriseMessageFormatImpl.java

    One of the standard "processes" here is to tar up java code from a unix box and untar it in windows using winzip.  Several files tend to end up getting lost due to the crazy length.



  • @fbjon said:

    TRWTF is the idiosynracies and syntax of Unix shell scripts. I'm tempted to say it's worse than Perl.

    That's true. I don't write a lot of scripts, so every time I need a test, I have to open the man page and try a few times before hitting on the correct test. And even then I sometimes forget to put arguments between quotes, and all that jazz. I work in bash mostly, csh previously, and the joy that is portable scripts with arithmetic. Ah well.

    @vt_mruhlin said:

    Clearly you haven't heard tales of the app I worked on with 4K URLs.
     

    You've got to be kidding! Is that within limits? A file path can only be 1K normally (PATH_MAX somewhere in syslimits.h). Let's hope this code doesn't get used in a cgi script...


Log in to reply