Re: shell scripting while if string length != 0

From: Rob (listone_at_deathbeforedecaf.net)
Date: 08/01/03

  • Next message: Alexander Farber: "Compiling kernel and making world as non-root"
    To: "Dave [Hawk-Systems]" <dave@hawk-systems.com>, "FreeBSD Questions" <freebsd-questions@FreeBSD.ORG>
    Date: Fri, 1 Aug 2003 19:01:00 +0930
    
    

    I'm not a shell guru, but pipelines don't necessarily run in sequence.
    In line 5 of your script, the part that says

      sed '1d' > /path/to/file_o_commands

    will destroy all contents of the original file. This may or may not
    happen before

      cat /path/to/file_o_commands

    has finished reading it.

    If you just want to execute the lines of a file in order, use something
    like

      cat file_o_commands | while read CMD ; do
        eval $CMD
      done

    On the other hand, if you want the script to hang around at the end of
    the file and wait for new commands, you may need a named pipe (FIFO).
    This is a file that one process writes to and another one reads from,
    not necessarily at the same time.

    See http://www.linuxjournal.com/article.php?sid=2156 and
    http://tldp.org/LDP/lpg/node15.html for some info on these.

    ----- Original Message -----
    From: "Dave [Hawk-Systems]" <dave@hawk-systems.com>
    Subject: shell scripting while if string length != 0

    > for reasons best left unsaid, we need to pull in a file full of
    partial
    > commands, and run them via a shell script on occasion, removing each
    command as
    > we run it. Have managed to hack togetherthe following shell script,
    but and
    > stumped on something simple because of my lack of shell knowledge;
    >
    > the file that holds out commands
    > <file_o_commands>
    > Server1 df -k
    > Server2 df -k
    > Server3 top | grep myprog
    > Server4 who
    >
    > add new commands to the end of the file with
    > echo "Server2 who" >> /path/to/file_o_commands
    >
    > then when we need to, run through the commands
    > <file_to_run_stuff>
    > #!/bin/sh
    > # get top command
    > DOCOMMAND=`head -n 1 /path/to/file_o_commands`
    > # remove that command
    > cat /path/to/file_o_commands | sed '1d' > /path/to/file_o_commands
    > # run that command
    > ssh ${DOCOMMAND}
    >
    > this works as intended with 1 exception, we need to add a while in
    there to loop
    > through the file and stop processing an exit when `head -n 1
    > /path/to/file_o_commands` does not return a line.
    >
    > I almost want to borrow -n from if
    >
    > while [ -n (DOCOMMAND=`head -n 1 /path/to/file_o_commands`) ] do
    > ...rest of script...
    > done
    >
    > Anyone care to enlighten me a bit?
    >
    > Dave
    >
    >
    > _______________________________________________
    > freebsd-questions@freebsd.org mailing list
    > http://lists.freebsd.org/mailman/listinfo/freebsd-questions
    > To unsubscribe, send any mail to
    "freebsd-questions-unsubscribe@freebsd.org"
    >

    _______________________________________________
    freebsd-questions@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-questions
    To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"


  • Next message: Alexander Farber: "Compiling kernel and making world as non-root"

    Relevant Pages

    • Re: Affecting Institutional Change (Yeah Right)
      ... provides labs in most non-dorm buildings replete with the wonderful ... be run from a shell, no idea what an executable was, and, in fact, ... IDE program, go through a ton of menus to pick the right script, go ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
      (Debian-User)
    • Checking for existence of command prior to execution
      ... alone in an interactive shell it works though. ... it will fail if I add ssh to the list of commands which is ... aliased by the tcsh that is calling the above script. ...
      (comp.os.linux.misc)
    • Re: unzip | touch | re-zip
      ... Put these commands in a function and all is solved. ... I know next to nothing about Unix shell commands. ... get a zipfile download preparation script in PHP to work on my website. ...
      (comp.unix.shell)
    • Re: Shell Script
      ... Another valuable document to look at it the Advanced Bash Scripting Guide ... Within the script, it will change the directory to /opt, and then exit. ... THIS HAS NO EFFECT ON THE SHELL YOU RUN THIS COMMAND IN. ... Read and execute commands from FILENAME and return. ...
      (linux.redhat)
    • Re: Shell Script
      ... Within the script, it will change the directory to /opt, and then exit. ... THIS HAS NO EFFECT ON THE SHELL YOU RUN THIS COMMAND IN. ... Read and execute commands from FILENAME and return. ...
      (linux.redhat)