Find the answer to your Linux question:
Results 1 to 4 of 4
I am having a problem scripting in ksh. I want to have a general subroutine which can query the version of a program. Since there are so many different ways ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    2

    ksh scripting problem

    I am having a problem scripting in ksh. I want to have a general subroutine which can query the version of a program. Since there are so many different ways of querying an application, I want to have the actual command passed as a string, and then used as part of a command.

    I have a subroutine which has
    $4 = pathname terminated with /
    $5 = binary name
    $6 = cmd string

    Code:
    Get_Version ()
    {
    # does not work
    QUERY="`$4$5 $6`"
    
    # works
    QUERY1="`$4$5 -version | cut -f1 -d, | cut -f3 -d' '`"
    
    echo "${QUERY} does not equal ${QUERY1}.  Why not?"
    }
    
    FX_CMD="-version | cut -f1 -d, | cut -f3 -d' '"
    FX_PATH="/opt/sfw/lib/firefox"
    FX_BIN="firefox-bin"
    
    # nulls for example only
    Get_Version null null null $FX_PATH $FX_BIN "$FX_CMD"
    I can't figure out why the pipes and cuts don't work when I try and use a variable.

    perplexed <at> paiged <dot> fea <dot> st

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    First, some utilities display the version on stderr and pipe only pipes stdout. So you have to redirect stderr to stdout (2>&1).

    Second, if you're going to pass the command as a parameter use the eval command to execute it.

    You also forgot the trailing '/' on FX_PATH.

    Code:
    Get_Version ()
    {
    # does not work
    QUERY="`eval $4$5 $6`"
    
    # works
    QUERY1="`$4$5 -version 2>&1 | cut -f1 -d, | cut -f3 -d' '`"
    
    echo "${QUERY} does not equal ${QUERY1}.  Why not?"
    }
    
    FX_CMD="-version 2>&1 | cut -f1 -d, | cut -f3 -d' '"
    FX_PATH="/opt/sfw/lib/firefox/"
    FX_BIN="firefox-bin"
    
    # nulls for example only
    Get_Version null null null $FX_PATH $FX_BIN "$FX_CMD"

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    You might find the ksh (and bash) builtin command command to be useful. See the man page for each for details.

    If you are intending to use your function to display the version of system programs, note that many (but not all) of the GNU programs use "--version" (2 minus signs) to elicit the version of the program.

    Good luck ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Just Joined!
    Join Date
    Jul 2009
    Posts
    2

    Thanks

    drl, the inconsistencies between --version and -version (or even -V) is the reason for placing the command string into a variable. I can then make the subroutine generic, generally (without) concern about how to retrieve the version information.

    lomcevak, I'll try your suggestions, and see if they 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
  •  
...