Find the answer to your Linux question:
Results 1 to 5 of 5
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    3

    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.

    TIA

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    I don't see any command in red but I will assume it's snort.Try
    Code:
    /usr/local/bin/snort -V >> $SENSORSTAT/MITRE.txt 2>&1

  3. #3
    Just Joined!
    Join Date
    May 2005
    Posts
    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
    $spacer_close
  5. #4
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Yeah, with the first iteration only stdout is being redirected.

    Code:
     >>
    is implicitly the same as

    Code:
     1>>
    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
    Location
    Täby, Sweden
    Posts
    7,578
    For a shorter version, though:
    Code:
    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:
    Code:
    (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:
    Code:
    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:
    Code:
    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
  •