Find the answer to your Linux question:
Results 1 to 5 of 5
Hi Im starting to write BASH scripts to automate some of my analysis. Ive been trying to find out how to do something but I cannot find it on the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2004
    Posts
    42

    BASH scripting problem


    Hi

    Im starting to write BASH scripts to automate some of my analysis. Ive been trying to find out how to do something but I cannot find it on the web or in books so could any of you pros please help me...

    Basically, in my script I execute an analysis program which opens up its own command line within which I type analysis commands, then I exit to return back to the bash prompt, like so:

    ~> program #start program

    program> command1 #Enter program commands
    program> command2
    program> exit #Exit program, back to bash shell

    ~>

    The problem is that I dont how to script all this in bash, i.e. enter all the external commands for my analysis program and exit the program subshell. Can any of you help.

    I came across something that apparently works in tcsh, the 'comm' parameter is used to supply external commands, it would go like this:

    ~> program comm="command1 & command2 & exit"

    Could any of you please help, and give me advice on how to do it in bash?

    Thanks

  2. #2
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    This piece of code should do something similar to what I believe you are asking:
    Code:
    #!/bin/bash
    
    CMDLN_ARGS="$@" # Command line arguments for this script
    export CMDLN_ARGS
    
    echo "Please enter password for root: "
    exec su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
    exit ${?}
    It halts all processing until the user supplies some input, in this case the root's password.

    Hope this helps

    tech

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I don't see why root enters into this at all...in fact, that method won't work at all, as the program reads from STDIN, not its commandline arguments. I recommend doing it like this:

    Code:
    #!/bin/bash
    
    program < $@;
    
    exit $?
    So basically, you could call:
    Code:
    ./script ooga booga
    And 'ooga booga' would be passed as standard input into the program. The only limitation to this is that each answer to a prompt in program needs to be one word. If you want to use multiple words, you'll need to hardcode them in:

    Code:
    #!/bin/bash
    
    program <<EOI
    option 1
    option 2
    option 3
    EOI
    
    exit $?

  4. #4
    Just Joined!
    Join Date
    Aug 2004
    Posts
    42
    Thanks for your help guys but I managed to find a solution and its quite simple. From within the script I call 'program' and it requires two arguments, name of an input file and the name of the output file it will produce, it then proceeds to its command line interface where I enter my analysis commands, cmd1, cmd2 and then exit:

    Code:
     program infile outfile comm="cmd1 && cmd2 && exit"
    Once the program is called and its command line interface is opened, 'cmd1', 'cmd2' and then 'exit' are entered sequentially.

    I actually thought that the 'comm' parameter, used above, was a bash builtin function or something but it is not, because the following also works:

    Code:
     program infile outfile "cmd1 && cmd2 && exit"
    So now, Im really confused. I dont understand why the declaration 'comm=...' works as input to the program interface, or why just giving the quoted string with logical && operators works? Can any you explain the semantics of this and what exactly bash is "thinking" when it does it?

    Thanks all

  5. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Alrighty. 'comm' is NOT a universal thing, rather it seems that this has been builtin for this program.

    Now then: programs usually take input from STDIN (standard input). This is your shell if running the program from a terminal (it will give a prompt and wait for you to enter something), or maybe a file or preselected words (using the STDIN redirection operator '<'). Now then, the program that you're using probably does something like this (pseudocode):
    Code:
    input_file=param1
    output_file=param2
    if other parameters exist
        use them as responses
    else
        read in responses from STDIN
    So when you call 'program' in the Bash script, Bash just passes stuff along. This is not any sort of Bash thing, but rather the way the program was written.


    Does that help?

Posting Permissions

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