The Horror of the Mover



  • I could have sworn I'd already posted this, but I went back and looked and couldn't find it ... I transcribed and sanitized on-the-fly, so any typo's are likely my fault. Not only is this ugly, but there was a (not-so?) subtle bug that went unseen for a number of years which was fixed last year-ish (and I didn't include the fix.) Can you find it?

    #!/bin/csh -f
    # Purpose: Copies and renames new input files to the appropriate
    #          input directories for APP. Compress the file then
    #          move compressed file to archive.

    set DATADIR = /production/data/input/SOURCE

    set APPDIR = /production/APP/data/input/
    set APPDIR1 = /production/APP/data/input1/
    set APPDIR2 = /production/APP/data/input2/

    set ARCHDIR = /archive/SOURCE/

    set LOGDIR = /production/logs/
    set LOGFILE = ${LOGDIR}/mover.log

    set DATE = `date`
    set MONTH = `date +%m`
    set YEAR = `date +%Y`
    set HOUR = `date +%H`

    cd ${DATADIR}

    unalias mv
    unalias rm

    while 1

      ls *.dat >&! /dev/null

      ## if($status != 0) then
      ##   exit
      ## endif

      if ($status == 0) then
        set MONTH = `date +%m`
        set HOUR  = `date +%H`
        set YEAR  = `date +%Y`

        foreach file (*.SOURCE)
          date '+%y/%m/%d %H:%M was the time of the last data pull' >! ${DATADIR}/mover.touch

          # make sure permissions are accessible
          # echo setting permissions on input files
          chmod 777 ${file}

          # copy the input files to the correct directories for APP
          echo "Copying File to APP Directories detail. ${file} " `date` >>! ${LOGFILE}
     
          switch ( $HOUR )
            case "01":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "02":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
            case "03":
              cp ${file} ${APPDIR2}
              mv ${APPDIR2}/${file} ${APPDIR2}/${file}.dat
              echo "copying file for input detail 2"
              breaksw
            case "04":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "05":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
            case "06":
              cp ${file} ${APPDIR2}
              mv ${APPDIR2}/${file} ${APPDIR2}/${file}.dat
              echo "copying file for input detail 2"
              breaksw
            case "07":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "08":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
            case "09":
              cp ${file} ${APPDIR2}
              mv ${APPDIR2}/${file} ${APPDIR2}/${file}.dat
              echo "copying file for input detail 2"
              breaksw
            case "10":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "11":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
            case "12":
              cp ${file} ${APPDIR2}
              mv ${APPDIR2}/${file} ${APPDIR2}/${file}.dat
              echo "copying file for input detail 2"
              breaksw
            case "13":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "14":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
            case "15":
              cp ${file} ${APPDIR2}
              mv ${APPDIR2}/${file} ${APPDIR2}/${file}.dat
              echo "copying file for input detail 2"
              breaksw
            case "16":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "17":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
            case "18":
              cp ${file} ${APPDIR2}
              mv ${APPDIR2}/${file} ${APPDIR2}/${file}.dat
              echo "copying file for input detail 2"
              breaksw
            case "19":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "20":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
            case "21":
              cp ${file} ${APPDIR2}
              mv ${APPDIR2}/${file} ${APPDIR2}/${file}.dat
              echo "copying file for input detail 2"
              breaksw
            case "22":
              cp ${file} ${APPDIR}
              mv ${APPDIR}/${file} ${APPDIR}/${file}.dat
              echo "copying file for input detail"
              breaksw
            case "23":
              cp ${file} ${APPDIR1}
              mv ${APPDIR1}/${file} ${APPDIR1}/${file}.dat
              echo "copying file for input detail 1"
              breaksw
     default:
       echo "Problem"
          endsw

          ## mv ${APPDIR}/${file} ${APPDIR}/${file}.dat

          # compress the files and move them to the archive directory
          echo "Compressing and moving input file" >>! ${LOGFILE}

          compress ${file}

          # we have to put the files in the directories based on month and year
          if !( -d ${ARCHDIR}${YEAR} ) then
            mkdir ${ARCHDIR}${YEAR}
          endif

          switch ( $MONTH )
            case "01":
              if !( -d {$ARCHDIR}${YEAR}/JAN ) then
                mkdir {$ARCHDIR}${YEAR}/JAN
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/JAN;breaksw
            case "02":
              if !( -d {$ARCHDIR}${YEAR}/FEB ) then
                mkdir {$ARCHDIR}${YEAR}/FEB
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/FEB;breaksw
            case "03":
              if !( -d {$ARCHDIR}${YEAR}/MAR ) then
                mkdir {$ARCHDIR}${YEAR}/MAR
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/MAR;breaksw
            case "04":
              if !( -d {$ARCHDIR}${YEAR}/APR ) then
                mkdir {$ARCHDIR}${YEAR}/APR
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/APR;breaksw
            case "05":
              if !( -d {$ARCHDIR}${YEAR}/MAY ) then
                mkdir {$ARCHDIR}${YEAR}/MAY
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/MAY;breaksW
            case "06":
              if !( -d {$ARCHDIR}${YEAR}/JUN ) then
                mkdir {$ARCHDIR}${YEAR}/JUN
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/JUN;breaksw
            case "07":
              if !( -d {$ARCHDIR}${YEAR}/JUL ) then
                mkdir {$ARCHDIR}${YEAR}/JUL
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/JUL;breaksw
            case "08":
              if !( -d {$ARCHDIR}${YEAR}/AUG ) then
                mkdir {$ARCHDIR}${YEAR}/AUG
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/AUG;breaksw
            case "09":
              if !( -d {$ARCHDIR}${YEAR}/SEP ) then
                mkdir {$ARCHDIR}${YEAR}/SEP
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/SEP;breaksw
            case "10":
              if !( -d {$ARCHDIR}${YEAR}/OCT ) then
                mkdir {$ARCHDIR}${YEAR}/OCT
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/OCT;breaksw
            case "11":
              if !( -d {$ARCHDIR}${YEAR}/NOV ) then
                mkdir {$ARCHDIR}${YEAR}/NOV
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/NOV;breaksw
            case "12":
              if !( -d {$ARCHDIR}${YEAR}/DEC ) then
                mkdir {$ARCHDIR}${YEAR}/DEC
              endif
              mv ${file}.Z {$ARCHDIR}${YEAR}/DEC;breaksw
            default:
              echo "There was an error generating the month and the files have been placed"
              echo "  in the " ${ARCHDIR} " directory "
              mv ${file}.Z ${ARCHDIR}
          endsw

        else
     
          if !( -d ${ARCHDIR}${YEAR}/INCOMPLETE ) then
            mkdir ${ARCHDIR}${YEAR}/INCOMPLETE
          endif
          mv ${file}.Z ${ARCHDIR}${YEAR}/INCOMPLETE;breaksw

        endif

        end

      endif

      sleep 50

    end



  • Looks ugly, but it also looks like a bash script and I've never seen a bash script that wasn't ugly.



  • This is way out of my specialty, but the first thing that popped out for me was the hour switch block.  It doesn't include midnight, right?



  • @mott555 said:

    Looks ugly, but it also looks like a bash script and I've never seen a bash script that wasn't ugly.
     

    What gave it away as a bash script?  Was it the line reading "#!/bin/csh" at the top?

     



  • but there was a (not-so?) subtle bug that went unseen for a number of years which was fixed last year-ish (and I didn't include the fix.) Can you find it?

    Was it "You forgot to write the log file at midnight" ? Or was it "You wrote a script in csh" ?



  • @zelmak said:

    I could have sworn I'd already posted this, but I went back and looked and couldn't find it ... I transcribed and sanitized on-the-fly, so any typo's are likely my fault. Not only is this ugly, but there was a (not-so?) subtle bug that went unseen for a number of years which was fixed last year-ish (and I didn't include the fix.) Can you find it?

    It's got a Wilson Pickett bug!

     



  • Is it that you're copying each file to the month? Looks like you copy file1.Z to JAN and then you'd copy file2.Z to JAN as well.

    but that's a quick look-see, it's lunch time and my vendor's just brought me a sandwich for doing alarm testing.



  • @Rootbeer said:

    @mott555 said:
    Looks ugly, but it also looks like a bash script and I've never seen a bash script that wasn't ugly.
    What gave it away as a bash script?  Was it the line reading "#!/bin/csh" at the top?
    Wrong meaning of the phrase "looks like", genius. He literally meant it looks like what it is, not that he's guessing that it's a bash script.

     



  • @Zylon said:

    @Rootbeer said:

    @mott555 said:
    Looks ugly, but it also looks like a bash script and I've never seen a bash script that wasn't ugly.
    What gave it away as a bash script?  Was it the line reading "#!/bin/csh" at the top?
    Wrong meaning of the phrase "looks like", genius. He literally meant it looks like what it is, not that he's guessing that it's a bash script.

     

    Except that it's NOT a bash script.

    Or am I just missing some really obvious irony in these comments?



  • Hell if I know. I don't do *nix.



  • @Zylon said:

    @Rootbeer said:

    @mott555 said:
    Looks ugly, but it also looks like a bash script and I've never seen a bash script that wasn't ugly.
    What gave it away as a bash script?  Was it the line reading "#!/bin/csh" at the top?
    Wrong meaning of the phrase "looks like", genius. He literally meant it looks like what it is, not that he's guessing that it's a bash script.

    If that's what he meant then he worded it confusingly.  That second clause, joined by "but", looks like it's going to be some kind of explanation for or justification of why it "looks ugly".  If it's not mott's assumption that it was a bash script, why would it be relevant that all bash scripts are ugly?  Why would anyone write "It looks ugly, but it also looks ugly, because it looks like something that is always ugly" on purpose?  The "It looks ugly, but it has to because it is an example of something that is always ugly" interpretation seemed more obvious to me.




  • @Zylon said:

    Hell if I know. I don't do *nix.

    Shit was SO csh.

     



  • @Zylon said:

    Hell if I know. I don't do *nix.

    It's a shell script, but not a bash script.

    #!/some_path/sh == bourne SHell. The "lowest common denominator", de facto, always-there *nix shell for a long time.
    #!/some_path/bash == Bourne Again SHell. Seems to be the most popular nowadays. Based on bourne shell, not surprisingly.
    #!/some_path/ksh == Korn SHell. Another bourne shell derivative that was popular in the 90s. #!/some_path/csh == C SHell. The one used in the OP. A shell with somewhat more C-like syntax than the bourne family. Next to plain ol' bourne, was almost a guarantee to be present back in the day.
    #!/some_path/tcsh == Some variation on csh that I know nothing about.

    Fascinating, wot?



  • I notice the switch only accounts for 23 hours out of the day.

    @zelmak said:

    echo "Problem"
    I should say so.



  • Looks like the entirety of the first switch statement can be replaced by an array and a modulus. The second switch statement with just an array for the months.

    Of course, if you want to be really sure it wont break anything you'd check that the numbers are in the same range first.



  • @zelmak said:

    #!/bin/rm -f

    FTFY

    @zelmak said:

        set MONTH = date +%m
        set HOUR  = date +%H
        set YEAR  = date +%Y

    This is a race condition, isn't it?



  • One might add that ksh was the standard shell on most Unixes, bash took over because it was the GNU version so became standard in the Linux world, and csh is an evil mess that should never have been spawned (and I still know people who use it every day, poor buggers).

     There's also zsh, whick is the Dvorak keyboard / Ron Paul of shells.

    Yes, I am this boring in real life too.



  • @fatbull said:

    @zelmak said:

    #!/bin/rm -f

    FTFY

    @zelmak said:

        set MONTH = date +%m
        set HOUR  = date +%H
        set YEAR  = date +%Y

    This is a race condition, isn't it?
    So that's why it omits case "00"!  It avoids the race!



  • @Iago said:

    One might add that ksh was the standard shell on most Unixes

    IIRC, sh was the only one that was guaranteed to always be present, at least up until Linux became popular. I think Linux still always has sh, but it seems it's often just a link to bash.

    There's also zsh, whick is the Dvorak keyboard / Ron Paul of shells.

    Yes, I am this boring in real life too.

    I can't believe I forgot zsh, my shell of choice. It's very similar to bash, but seems to have a few additional features



  • @jverd said:

    IIRC, sh was the only one that was guaranteed to always be present, at least up until Linux became popular. I think Linux still always has sh, but it seems it's often just a link to bash.

    It is indeed often a symlink to bash. I think it was Ubuntu which started symlinking it to dash instead (and broke a ton of poorly written scripts which started #!/bin/sh but assumed bash). And in some cases it's a symlink to busybox.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.