Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
Hi All, I have a small application (software), i am not sure exact type of it but it's executable and i can run it like this; ./nameOfApplication when its running, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    8

    How to run and get output from an executable file in shall scripting??


    Hi All,

    I have a small application (software), i am not sure exact type of it but it's executable and i can run it like this;
    ./nameOfApplication
    when its running, i can view/add/update/delete products by passing integer keys/values (1,2,3,4) respectively.

    Now i want to make a shall script inside which i can run the above application, pass an integer to view the products and save the result into a txt file. I tried the following but it's not working.
    Code:
    #!/bin/bash
    ./nameOfApplication \
    4
    > tmp.txt
    Anybody have an idea how to achieve this?

    Thanks and regards,
    Malik

  2. #2
    Linux Newbie
    Join Date
    Apr 2005
    Location
    Clinton Township, MI
    Posts
    104

    Make the script executable, then pass it values

    You have to do a few things:

    1. Make the script executable.
    To do this, run the command:
    chmod +x nameOfScript

    2. Either set the value you want in the script, perhaps setting one default value, then pass in other values, either required, or optional, through command arguments.

    #!/bin/bash
    ./nameOfApplication $1

    where $1 contains the contents of the information you want to pass to your application. You can test for whether or not a value is passed to the application, and if not, use a default value instead. I'll leave that up to you to learn.

    The man command for bash has quite a bit of information. For example:

    ARGUMENTS
    If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0 is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads and executes commands from this file, then exits. Bash's exit status is the exit status of the last command executed in the script. If no commands are executed, the exit status is 0. An attempt is first made to open the file in the current directory, and, if no file is found, then the shell searches the directories in PATH for the script.

    Positional Parameters
    A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameters may not be assigned to with assignment statements. The positional parameters are temporarily replaced when a shell function is executed (see FUNCTIONS below).

    When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces (see EXPANSION below).

    Special Parameters
    The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
    * Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
    @ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
    # Expands to the number of positional parameters in decimal.
    ? Expands to the exit status of the most recently executed foreground pipeline.
    - Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).
    $ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
    ! Expands to the process ID of the most recently executed background (asynchronous) command.
    0 Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.
    _ At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file currently being checked.
    Brian Masinick
    masinick AT yahoo DOT com

  3. #3
    Just Joined!
    Join Date
    Sep 2011
    Posts
    8
    Thanks masinick.
    But it's not what i want.
    I want "nameOfApplication" to run first and while it's running i pass an integer parameter and get the results back. I can pass default/static value its not a problem but its just not right. For example i tried this:
    Code:
    ./nameOfApplication 4
    its wrong! With this i am passing an argument (4) to the application. Application will run with the argument (4) and then terminate. You can achive in Java what i want something like this;
    Code:
    java -Dsun.net.inetaddr.ttl=0 -cp $CLASSPATH:./xercesImpl.jar:./xmlParserAPIs.jar CSPxmlParser \
    -XMLFile $1 \
    -DBPath $DBURL \
    -DBUser username1 \
    -DBPassword password1

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    You could use signals to do this, I guess. The SIGRTMIN/SIGRTMAX signals are reserved for user use, and there's more than enough of them. For example:

    Code:
    #!/bin/bash
    trap_func_1() {
      echo Doing stuff for selection 1...
    }
    trap_func_2() {
      echo Doing stuff for selection 2...
    }
    trap_func_3() {
      echo Doing stuff for selection 3...
    }
    trap_func_4() {
      echo Doing stuff for selection 4...
    }
    
    trap 'trap_func_1' 35
    trap 'trap_func_2' 36
    trap 'trap_func_3' 37
    trap 'trap_func_4' 38
    
    while :; do
      printf .
      sleep 1
    done
    The above script would run in a forever loop, awaiting your input. Then in another terminal, send the pid of the script one of the four signals defined in the script ( 35 - 38 ), e.g.:

    Code:
    kill -35 $script_pid
    To see all the signals available on your system, do:
    Code:
    kill -l
    and 'man kill'.

  6. #5
    Just Joined!
    Join Date
    Sep 2011
    Posts
    8
    Honestly i really don't know what "SIGRTMIN/SIGRTMAX" signals are and how i can use them
    I am not very experiened with bash/Linux.

    Though i do remember i have seen a bash script more or less same as i mentioned above in Java but unfortunately i can not find it anymore (

  7. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    SIGRTMIN and SIGRTMAX are just signals you pass to any program (or script) with the 'kill' command. The bash example I included gives you an example of how to use those very signals. Just incorporate it into the bash script you already have - you'll need to equate each signal with an integer that you want (i.e., signal 35 = 1, signal 36 = 2, etc.). If you have a zillion integers that you'll want to pass to your script, then this approach won't work, but if its only a handful, it should work for you.

  8. #7
    Just Joined!
    Join Date
    Sep 2011
    Posts
    8
    I can not call kill as it requires pid while i am calling the application inside a bash scipt. Please see below code:

    Code:
    #!/bin/bash
    signal 35 = 3
    kill -35 ./tcap
    ./tcap is an application which requires input '3' to do a specific action while it's running

  9. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Is tcap something you've written? If so, have you already put code into it that converts the signal 35 to mean 3, internally?

    As to your code above, what is the 'signal 35 = 3' line doing? Is it just a comment for reference, b/c syntactically, it is not correct (in bash anyway).

    To get the pid of your command, do something like:
    Code:
    pid=`ps -eo pid,cmd|awk '$2 !~ /awk/ && /tcap/{print $1}' `
    echo pid of tcap is $pid
    kill -35 $pid

  10. #9
    Just Joined!
    Join Date
    Sep 2011
    Posts
    8
    "tcap" is a 3rd part software which we are using it to get the entitlements etc...
    As i mentioned in my very first post i don't know what type of application it is so i can't edit it either.
    "tcap" is not a by default running process. You have to run it manually to get entitlements, user info etc... back.

    If you run it like this "./tcap" it runs a small software where you can pass input in form of integers (1,2,3,4 etc...) to perform a specific job.

    I hope i explained better now?

  11. #10
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Oh, okay - that changes things. So how exactly do you pass integers to the "tcap" program normally - can you show an example of how you run it and how you input integers?

Page 1 of 2 1 2 LastLast

Posting Permissions

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