Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Quick Bash Question

    I've a quick question:

    I'm running a bash script that runs many commands and redirects the output to a file. All the commands function correctly but one. This particular line is as follows:

    #snort -V
    echo -e "\nSnort Version:" >> $SENSORSTAT/MITRE.txt
    /usr/local/bin/snort -V >> $SENSORSTAT/MITRE.txt

    Unlike all the other commands, the command in red doesn't redirect/append to MITRE.txt but outputs to the screen instead, if run manually. I've no idea if it will run correctly as a cronjob (yet).

    I've 3 different scripting books and cannot (so far) find how to correct this.


  2. #2
    I don't see any command in red but I will assume it's snort.Try
    /usr/local/bin/snort -V >> $SENSORSTAT/MITRE.txt 2>&1

  3. #3
    It worked! Thanks a ton!

    I referenced the '2>&1' and actually got a better understanding of standard output and redirecting.

    Again, thanks!

  4. $spacer_open
  5. #4
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Yeah, with the first iteration only stdout is being redirected.

    is implicitly the same as

    Santa's Little Helper's addition points descriptor 2 (stderr) to the same place as descriptor 1 (stdout).

  6. #5
    Linux Guru
    Join Date
    Oct 2001
    Täby, Sweden
    For a shorter version, though:
    echo "Snort version: $(snort -V 2>&1)" >>/path/to/file
    Not necessarily what you want, just a tip.

    Also, if you to redirect an entire sequence of commands to the same file, try this:
    (echo "Snort version:"
    snort -V
    possibly other commands
    ) >/path/to/file 2>&1
    The parentheses run all the enclosed commands in a subshell. Since it is the subshell's file descriptors that are being redirected, all commands invoked by the subshell will inheret those redirections. If you fancy it, here's another alternative:
    exec >/path/to/file 2>&1
    echo "Snort version:"
    snort -V
    The exec command without any arguments will redirect any file descriptors of the currently running shell. You can even save the old ones and restore them later, like this:
    exec 3>&1 4>&2 1>/path/to/file 2>&1
    echo "Snort version:"
    snort -V
    exec 1>&3 2>&4 3>&- 4>&-
    The ">&-" part means to close the given file descriptor.

    Much more info on these things can be fetched from the bash manpage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts