C-Shell Madness



  • We have a c-shell script that moves files from an upload, data feed directory into the input directories of our processing application. I believe I've discussed this here before, but I can't locate the post.

    Anyhow, previously, there was essentially a brute force modulus operation which would place new files into the input directories based solely on the hour of the day that the script ran (not the file date or a date field in the file date ... the run time of the script.) It looked kind of like this:

    switch ( $HOUR )
      case "00": 
        cp ${file} ${INPUT_1}
        breaksw
      case "01":
        cp ${file} ${INPUT_2}
        breaksw
      case "02": 
        cp ${file} ${INPUT_3}
        breaksw
      case "03":
        cp ${file} ${INPUT_1}
        breaksw
      .
      .
      .
      default:
        echo "Problem"
    endsw
    

    I had recently changed the code from that, to this (so as to not change the behavior, but make it shorter, more obvious that the people managing these things might actually know something about computers):

    @ whichdir = $HOUR % 3
    if ( $whichdir == 0 ) then
      set whichdir = ${INPUT_1}
    else if ( $whichdir == 1 ) then
      set whichdir = ${INPUT_2}
    else if ( $whichdir == 2 ) then
      set whichdir = ${INPUT_3}
    endif
    

    cp ${file} ${whichdir}

    That code was even more recently replaced by another coder to balance new data across the inputs:

    @ INPUT_COUNT1 = `ls $INPUT_1 | wc -l`
    @ INPUT_COUNT2 = `ls $INPUT_2 | wc -l`
    @ INPUT_COUNT3 = `ls $INPUT_3 | wc -l`
    

    if ( $INPUT_COUNT1 < $INPUT_COUNT2 ) then
    if ( $INPUT_COUNT1 < $INPUT_COUNT3 ) then
    set whichdir = ${INPUT_1}
    else if ( $INPUT_COUNT2 < $INPUT_COUNT3 ) then
    set whichdir = ${INPUT_2}
    else
    set whichdir = ${INPUT_3}
    endif
    else
    if ( $INPUT_COUNT2 < $INPUT_COUNT3 ) then
    set whichdir = ${INPUT_2}
    else if ( $INPUT_COUNT1 < $INPUT_COUNT3 ) then
    set whichdir = ${INPUT_1}
    else
    set whichdir = ${INPUT_3}
    endif
    endif

    cp ${file} ${whichdir}

    For the past two years (I honestly don't know what the hold up is -- change is scary?), I've been trying to get the powers-that-be to implement a script that I wrote AT THEIR BEHEST/REQUIREMENT which balanced inputs across the directories in a more UNIX-y way:

    whichdir = `du -s ${INPUTS} | sort -n | head -1 | cut -f2`
    

    cp ${file} ${whichdir}



  • @zelmak said:

    For the past two years (I honestly don't know what the hold up is -- change is scary?), I've been trying to get the powers-that-be to implement a script that I wrote AT THEIR BEHEST/REQUIREMENT which balanced inputs across the directories in a more UNIX-y way

    You've done your job, keep the paper trail and enjoy the untroubled ease of programming beneath [the organization's] sheltering branches



  • @Speakerphone Dude said:

    You've done your job, keep the paper trail and enjoy the untroubled ease of programming beneath [the organization's] sheltering branches

    Thank you for that. I see another deadline missed.



  • I remember trying to write (in BASIC) a subroutine that would sort 5 variables, and getting stuck in all the permutations (I didn't know that there'd be 120 variations :).



  • @realmerlyn said:

    I remember trying to write (in BASIC) a subroutine that would sort 5 variables, and getting stuck in all the permutations (I didn't know that there'd be 120 variations :).

    The 10 variable variant is left as an exercise to the reader.



  • @TGV said:

    @realmerlyn said:

    I remember trying to write (in BASIC) a subroutine that would sort 5 variables, and getting stuck in all the permutations (I didn't know that there'd be 120 variations :).

    The 10 variable variant is left as an exercise to the reader.

    A decorated comparer pattern with solve it with only 11 snippits of code, all less then 10 lines long.  Yes permutations are only n+1.



  • @KattMan said:

    A decorated comparer pattern with solve it with only 11 snippits of code, all less then 10 lines long.

    That sentence makes perfect sense.



  • @blakeyrat said:

    @KattMan said:
    A decorated comparer pattern with solve it with only 11 snippits of code, all less then 10 lines long.

    That sentence makes perfect sense.

    Pfft, what is it that you don't even about patterns? So that.



  • @TGV said:

    @realmerlyn said:

    I remember trying to write (in BASIC) a subroutine that would sort 5 variables, and getting stuck in all the permutations (I didn't know that there'd be 120 variations :).

    The 10 variable variant is left as an exercise to the reader.

    I think this should do it (apologies for the lack of indentation):

    [code] 10 REM Sort!
    20 REM http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
    30
    40 NVARS=10
    50 FILENAME$="Code"
    60 LINE=1000
    70
    80 DIM VARS$(NVARS)
    90 DIM ORIGVARS$(NVARS)
    100 FOR I=1 TO NVARS
    110 READ VARS$(I)
    120 ORIGVARS$(I)=VARS$(I)
    130 NEXT I
    140
    150 FOR I=1 TO NVARS-1
    160 FOR J=I+1 TO NVARS
    170 IF VARS$(I)>VARS$(J) THEN TEMP$=VARS$(I):VARS$(I)=VARS$(J):VARS$(J)=TEMP
    180 NEXT J
    190 NEXT I
    200
    210 X=OPENOUT FILENAME$
    220 PRINT#X;LINE;" SORTED=0"
    230 REPEAT
    240 REM Append IF statements to the file
    250 LINE=LINE+10
    260 PRINT#X;LINE;" IF SORTED=0";
    270 FOR I=1 TO NVARS-1
    280 PRINT#X;" AND ";VARS$(I);"<";VARS$(I+1);
    290 NEXT I
    300 PRINT#X;" THEN SORTED=1";
    310 FOR I=1 TO NVARS
    320 PRINT#X;": TEMP";I;"=";VARS$(I);
    330 NEXT I
    340 PRINT#X
    350 LINE=LINE+10
    360 PRINT#X;LINE;" IF SORTED=1 THEN SORTED=2";
    370 FOR I=1 TO NVARS-1
    380 PRINT#X;": ";ORIGVARS$(I);"=TEMP";(I);
    390 NEXT I
    400 PRINT#X 410 REM Generate the next permutation of the variables
    420 K=NVARS
    430 FOR K_TRY=1 TO NVARS-1
    440 IF VARS$(K_TRY)<VARS$(K_TRY+1) THEN K=K_TRY<br> 450 NEXT K_TRY
    460 IF K=NVARS THEN GOTO 640
    470 L=K+1
    480 FOR L_TRY=K+2 TO NVARS
    490 IF VARS$(K)<VARS$(L_TRY) THEN L=L_TRY<br> 500 NEXT L_TRY
    510 TEMP=VARS$(K)
    520 VARS$(K)=VARS$(L)
    530 VARS$(L)=TEMP
    540 THIS=K+1
    550 OTHER=NVARS
    560 REPEAT
    570 TEMP$=VARS$(THIS)
    580 VARS$(THIS)=VARS$(OTHER)
    590 VARS$(OTHER)=TEMP$
    600 THIS=THIS+1
    610 OTHER=OTHER-1
    620 UNTIL THIS>=OTHER
    630 UNTIL FALSE
    640 CLOSE#X
    640
    900 REM The names of the variables go here. Each line must start with DATA.
    910 REM Make sure there are NVARS entries in total.
    920 DATA A, B, C, D, E
    930 DATA F, G, H, I, J[/code]

    It's been a while since I did anything in BBC BASIC (like, two decades) and I have nothing to test the above on, so I don't guarantee it'll work. It ought to create file named FILENAME$ (line 50) with line numbers starting at the value of LINE (line 60) that has a series of IF statements that sort NVARS (line 40) variables named in the DATA statements (lines 920-930) the hard way. Note use of bubble sort within the program. :)

    Why yes, I did have some time to kill. Why do you ask?



  • @Ibix said:

    @TGV said:

    @realmerlyn said:

    I remember trying to write (in BASIC) a subroutine that would sort 5 variables, and getting stuck in all the permutations (I didn't know that there'd be 120 variations :).

    The 10 variable variant is left as an exercise to the reader.

    I think this should do it (apologies for the lack of indentation):

    <font face="Lucida Console" size="2"> 10 REM Sort!
    20 REM http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
    30
    40 NVARS=10
    50 FILENAME$="Code"
    60 LINE=1000
    70
    80 DIM VARS$(NVARS)
    90 DIM ORIGVARS$(NVARS)
    100 FOR I=1 TO NVARS
    110 READ VARS$(I)
    120 ORIGVARS$(I)=VARS$(I)
    130 NEXT I
    140
    150 FOR I=1 TO NVARS-1
    160 FOR J=I+1 TO NVARS
    170 IF VARS$(I)>VARS$(J) THEN TEMP$=VARS$(I):VARS$(I)=VARS$(J):VARS$(J)=TEMP
    180 NEXT J
    190 NEXT I
    200
    210 X=OPENOUT FILENAME$
    220 PRINT#X;LINE;" SORTED=0"
    230 REPEAT
    240 REM Append IF statements to the file
    250 LINE=LINE+10
    260 PRINT#X;LINE;" IF SORTED=0";
    270 FOR I=1 TO NVARS-1
    280 PRINT#X;" AND ";VARS$(I);"<";VARS$(I+1);
    290 NEXT I
    300 PRINT#X;" THEN SORTED=1";
    310 FOR I=1 TO NVARS
    320 PRINT#X;": TEMP";I;"=";VARS$(I);
    330 NEXT I
    340 PRINT#X
    350 LINE=LINE+10
    360 PRINT#X;LINE;" IF SORTED=1 THEN SORTED=2";
    370 FOR I=1 TO NVARS-1
    380 PRINT#X;": ";ORIGVARS$(I);"=TEMP";(I);
    390 NEXT I
    400 PRINT#X 410 REM Generate the next permutation of the variables
    420 K=NVARS
    430 FOR K_TRY=1 TO NVARS-1
    440 IF VARS$(K_TRY)<VARS$(K_TRY+1) THEN K=K_TRY
    450 NEXT K_TRY
    460 IF K=NVARS THEN GOTO 640
    470 L=K+1
    480 FOR L_TRY=K+2 TO NVARS
    490 IF VARS$(K)<VARS$(L_TRY) THEN L=L_TRY
    500 NEXT L_TRY
    510 TEMP=VARS$(K)
    520 VARS$(K)=VARS$(L)
    530 VARS$(L)=TEMP
    540 THIS=K+1
    550 OTHER=NVARS
    560 REPEAT
    570 TEMP$=VARS$(THIS)
    580 VARS$(THIS)=VARS$(OTHER)
    590 VARS$(OTHER)=TEMP$
    600 THIS=THIS+1
    610 OTHER=OTHER-1
    620 UNTIL THIS>=OTHER
    630 UNTIL FALSE
    640 CLOSE#X
    640
    900 REM The names of the variables go here. Each line must start with DATA.
    910 REM Make sure there are NVARS entries in total.
    920 DATA A, B, C, D, E
    930 DATA F, G, H, I, J</font>

    It's been a while since I did anything in BBC BASIC (like, two decades) and I have nothing to test the above on, so I don't guarantee it'll work. It ought to create file named FILENAME$ (line 50) with line numbers starting at the value of LINE (line 60) that has a series of IF statements that sort NVARS (line 40) variables named in the DATA statements (lines 920-930) the hard way. Note use of bubble sort within the program. :)

    Why yes, I did have some time to kill. Why do you ask?

    This is even more high-tech than that episode in Bones where the dude hacked the Jeffersonian mainframe by hand-carving fractals on a bone that he knew would be scanned in the lab.



  • @Speakerphone Dude said:

    This is even more high-tech than that episode in Bones where the dude hacked the Jeffersonian mainframe by hand-carving fractals on a bone that he knew would be scanned in the lab.

    Oh come on. All I did was use BASIC to write some BASIC. Dawg. It was a bit like chipping a flint knife using a flint, though, I must admit.



  • If my experience of entering source code from 1980's computer magazines is anything to go by all this will do when run is produce the error "Subscript out of range".



  • @zelmak said:

    We have a c-shell script [...]
    TRWTF is using the C-shell to write scripts. Whilst the TC-shell is my shell of choice, writing scripts in anything other than the Bourne shell or bash is generally not a very good idea.

     



  • @RTapeLoadingError said:

    If my experience of entering source code from 1980's computer magazines is anything to go by all this will do when run is produce the error "Subscript out of range".

     

    One of the first BASIC programs I entered was for my printer: it was basically a printer test page showing the different modes and capabilities of the printer. It involved lots of LPRINT CHR$(27) entries. Guess the typo I made? Yep, missed the dollar sign which cause that error. I do remember thinking "But the super- and subscript tests have already been printed!" then "How does BASIC know that CHR$(27)+"whatever" makes the printer go into subscript?" Then realising that subscript was the number into an array (or whatever my 12yo self thought was the terminology).

     



  • @Speakerphone Dude said:

    This is even more high-tech than that episode in Bones where the dude hacked the Jeffersonian mainframe by hand-carving fractals on a bone that he knew would be scanned in the lab.
     

    You are obliged to provide a link to video material for this because I am intensely curious how they played that one in the show.



  • @dhromed said:

    @Speakerphone Dude said:

    This is even more high-tech than that episode in Bones where the dude hacked the Jeffersonian mainframe by hand-carving fractals on a bone that he knew would be scanned in the lab.
     

    You are obliged to provide a link to video material for this because I am intensely curious how they played that one in the show.

    Can't put up a video clip (copyright and all that) but here is the relevant part of the summary on tvrage: "Back at the lab it’s discovered that a computer virus written in a bone of the victim caused a system overload when it was scanned by Angela, resulting in the lab fire; a fractal pattern in the bone was used; it turns out that the killer wanted to send out the message that he doesn’t need a computer to do his job."

    That is even worse than in X-Files when Scully calls Internet to get the phone numbers of all subscribers who used a specific website.



  • @Speakerphone Dude said:

    @dhromed said:

    @Speakerphone Dude said:

    This is even more high-tech than that episode in Bones where the dude hacked the Jeffersonian mainframe by hand-carving fractals on a bone that he knew would be scanned in the lab.
     

    You are obliged to provide a link to video material for this because I am intensely curious how they played that one in the show.

    Can't put up a video clip (copyright and all that) but here is the relevant part of the summary on tvrage: "Back at the lab it’s discovered that a computer virus written in a bone of the victim caused a system overload when it was scanned by Angela, resulting in the lab fire; a fractal pattern in the bone was used; it turns out that the killer wanted to send out the message that he doesn’t need a computer to do his job."

    That is even worse than in X-Files when Scully calls Internet to get the phone numbers of all subscribers who used a specific website.

    Reminds me of Snow Crash.



  • @Speakerphone Dude said:

    This is even more high-tech than that episode in Bones where the dude hacked the Jeffersonian mainframe by hand-carving fractals on a bone that he knew would be scanned in the lab.
    It must have been hell trying to develop shell code for handcarved fractals. That must limit the suspect pool quite a bit, down to people with the kind of access nescesary to test and develop said exploit. They would have to know all the specifics and inner workings of the forensic team, like what software they use to process the images ...



  • @Speakerphone Dude said:

    "Back at the lab it’s discovered that a computer virus written in a bone of the victim caused a system overload when it was scanned by Angela, resulting in the lab fire; a fractal pattern in the bone was used; it turns out that the killer wanted to send out the message that he doesn’t need a computer to do his job."
     

    I am so incredilized.



  • @Speakerphone Dude said:

    Can't put up a video clip (copyright and all that) but here is the relevant part of the summary on tvrage: "Back at the lab it’s discovered that a computer virus written in a bone of the victim caused a system overload when it was scanned by Angela, resulting in the lab fire; a fractal pattern in the bone was used; it turns out that the killer wanted to send out the message that he doesn’t need a computer to do his job."

    Is that the same Bones episode where the evil haxxor writes viruses on book barcodes that get scanned into the library which causes a haxxoration of the government mainframe or something? If so, I saw part of that. If not, they really ought to drop that gimmick.

    @Speakerphone Dude said:

    That is even worse than in X-Files when Scully calls Internet to get the phone numbers of all subscribers who used a specific website.

    Was that the one with the externally-digesting cannibalistic chubby chaser? That early internet reference made sense, actually, since it was a local dating website and it necessarily had subscription information.



  • @zelmak said:

    @Speakerphone Dude said:

    @dhromed said:

    @Speakerphone Dude said:

    This is even more high-tech than that episode in Bones where the dude hacked the Jeffersonian mainframe by hand-carving fractals on a bone that he knew would be scanned in the lab.
     

    You are obliged to provide a link to video material for this because I am intensely curious how they played that one in the show.

    Can't put up a video clip (copyright and all that) but here is the relevant part of the summary on tvrage: "Back at the lab it’s discovered that a computer virus written in a bone of the victim caused a system overload when it was scanned by Angela, resulting in the lab fire; a fractal pattern in the bone was used; it turns out that the killer wanted to send out the message that he doesn’t need a computer to do his job."

    That is even worse than in X-Files when Scully calls Internet to get the phone numbers of all subscribers who used a specific website.

    Reminds me of Snow Crash.

     

    Reminds me of Digital Fortress (which is a horrible piece of crap, like everything else Dan Brown has ever written).

     



  • @bullestock said:

    Reminds me of Digital Fortress (which is a horrible piece of crap, like everything else Dan Brown has ever written).

    Having not read the book, having no interest to read the book, but still curious how bad it is, I read the synopsis on Wikipedia. Wow. [url=https://en.wikipedia.org/wiki/Digital_Fortress#Synopsis]That last paragraph is a doozy.[/url]

    @Spoiler warning! Highlight to read said:


    However, Strathmore was unaware that Digital Fortress is actually a computer worm once unlocked, "eating away" at the NSA databank's security and allowing "any third-grader with a modem" to look at government secrets. When TRANSLTR overheats, Strathmore commits suicide by standing next to the machine as it explodes. The worm eventually gets into the database, but soon after Fletcher figures out the password, and is able to terminate the worm before hackers can get any significant data.



  • @Xyro said:

    @Speakerphone Dude said:
    Can't put up a video clip (copyright and all that) but here is the relevant part of the summary on tvrage: "Back at the lab it’s discovered that a computer virus written in a bone of the victim caused a system overload when it was scanned by Angela, resulting in the lab fire; a fractal pattern in the bone was used; it turns out that the killer wanted to send out the message that he doesn’t need a computer to do his job."

    Is that the same Bones episode where the evil haxxor writes viruses on book barcodes that get scanned into the library which causes a haxxoration of the government mainframe or something? If so, I saw part of that. If not, they really ought to drop that gimmick.

    Same dude; there was two episodes with him. At first they could not catch him, then a few episodes later he became such an evil master that for some reason he forged evidence (including a timestamp on the tape from a CCTV) and now everybody (INCLUDING MAYBE PEOPLE IN HER OWN TEAM!!!) think that Bones is a murderer so with the help of her father she went off the grid in the season finale, leaving Angel dude (the former sniper) alone to take care of the baby.

    Thank you now I feel like an old woman calling her friend Marge over at the beauty salon to tell her what just happened in Santa Barbara.



  • @zelmak said:

    Reminds me of Snow Crash.
     

    Reminds me of The Killing Star. Aliens do the same thing with rigged signals to SETI, and which take out the global defense network so their incoming (near light-speed) missles aren't detected.

     The rest of the book doesn't turn out well for the humans.



  • @Severity One said:

    Whilst the TC-shell is my shell of choice, writing scripts in anything other than the Bourne shell or bash is generally not a very good idea.
     

    Dunno if C-shell scripting is necessarily a bad idea (right tool for the right job and all that) but it's increasingly uncommon these days.

    The last group of customers that wanted C-Shell scripting soon realised how other shells were nicer/easier to use, and learned enough to translate the scripts into a saner language. I've not come across anyone else using C-Shell.



  • @Cassidy said:

    Dunno if C-shell scripting is necessarily a bad idea (right tool for the right job and all that) but it's increasingly uncommon these days.

    I don't think much has changed since [url=http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/#b]Csh Programming Considered Harmful[/url] was written, so it's fair to say it's always been a bad idea.



  • Wow this spoiler alert style of posts is new to me.

    WHAT A SILLY WASTE OF BOOK.


  • Discourse touched me in a no-no place

    @Nagesh said:

    Wow this spoiler alert style of posts is new to me.

    <font color="white">You've never been on forums that require spoilers?</font>


  • @Vanders said:

    Csh Programming Considered Harmful
     

    Ohhh... that's a new one on me.

    I mean, I've always suspected... just never seen it formally documented.



  • @Speakerphone Dude said:

    That is even worse than in X-Files when Scully calls Internet to get the phone numbers of all subscribers who used a specific website.
    You mean, like Facebook? I'm pretty sure that Facebook could give you that information.

     



  • @Severity One said:

    @Speakerphone Dude said:

    That is even worse than in X-Files when Scully calls Internet to get the phone numbers of all subscribers who used a specific website.
    You mean, like Facebook? I'm pretty sure that Facebook could give you that information.

     

    Yeah it worked! They sent me your browsing history, now I wonder why you spend so much time on such a creepy website (you know you want to click on this link without checking the url first, don't fight it, resistance is futile!)



  •  I'm at work, and internet usage is monitored. Even though I'm a developer, I'm not entirely stupid.

     



  • @Severity One said:

     I'm at work, and internet usage is monitored. Even though I'm a developer, I'm not entirely stupid.

     

    If you live in the US, you can click, no worries... the commies from ACLU have your back.


Log in to reply
 

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