Find the answer to your Linux question:
Results 1 to 6 of 6
I have a number of bash shell scripts that do various things like dumping databases. I would like to configure these scripts to output the command being executed to the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2005
    Posts
    4

    How to output current command in shell scripts


    I have a number of bash shell scripts that do various things like dumping databases. I would like to configure these scripts to output the command being executed to the standard output. I don't mean direct the output of the command, but rather the command itself, before executing it. So, for example, if you had a script that said

    #!/bin/bash
    date

    then I would want the output to be:

    date
    Sun Jul 3 09:31:32 PDT 2005

    Currently I achieve this by putting an "echo" just before each command in the script, but this gets old really quickly and seems very clunky. I've looked in the bash documentation but couldn't find anything, maybe I'm missing something really obvious here? This would be very useful because the scripts have a number of commands, each of which can take a while, and it's very helpful to be able to see which command it is currently doing (without having to do a ps).

    So, is there any special way of invoking bash so that it outputs each command in a script before it does it?

    Thanks,

    -Neil

  2. #2
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    If you want to echo a lot of commands in a script try this:

    Code:
    cat <<- _EOF_
    $PATH
    $USER
    
    _EOF_
    and so forth.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  3. #3
    Just Joined!
    Join Date
    Jul 2005
    Posts
    4
    Thanks very much, that seems to work.

  4. #4
    Just Joined!
    Join Date
    Jul 2005
    Posts
    4
    Actually, I spoke too soon - this seemed to work on the simple script I tried it in, but when I put it into the dump_databases script, it just seems to output the whole script, without actually doing any of the commands. That isn't the effect I wanted... it should do the commands as usual, just outputting the commands as it does them. In any case, thanks again for the rapid reply... any other ideas much appreciated!
    -Neil

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Your script should begin with set -v

  6. #6
    Just Joined!
    Join Date
    Jul 2005
    Posts
    4
    Great! I think that's it. Actually, I think you can do it all in the first line of the script:

    #!/bin/bash -v

    This appears to work, outputting all the commands as it does them. I don't know why I didn't think of this myself, the "verbose" option is a rather obvious thing to try... I think I was distracted by the sheer amount of "stuff" in the man page for bash. Thanks!

    -Neil

Posting Permissions

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