Find the answer to your Linux question:
Results 1 to 7 of 7
Hi Here's an interesting one - and (hopefully!) simply answered! I have a number of servers of different types (Linux, Solaris, AIX etc), each of which is running ksh (yes ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    4

    Scripting with parameters

    Hi

    Here's an interesting one - and (hopefully!) simply answered!

    I have a number of servers of different types (Linux, Solaris, AIX etc), each of which is running ksh (yes I *know* its better to use bash, however, this decision wasn't mine )

    Anyway, I've a set of scripts, that ideally I'd like to keep the same on all servers, however, there are subtle differences between each flavour of unix. So my idea is to create a device specific 'base' API which gives the basic functions required by scripts, as such, the scripts can be kept the same....

    All has worked well so far, until I came across a problem with AWK...

    I thought I should be able to create a function:

    API_awk()
    {

    awk $*

    }

    As the $* passes across all the parameters from the calling command line... however, I'm getting syntax errors etc...

    e.g.
    echo hello | awk '{printf $1}'
    hello
    - works ok...

    echo hello | API_awk '{printf $1}'
    Param: {printf $1}
    Execute command: awk {printf $1}
    awk: syntax error near line 1
    awk: illegal statement near line 1

    (I put some extra echos in to say what the script is doing...)
    - As you can see it isn't working...

    So.... my question is, a) what's going wrong? - I'm thinking this has something to do with substitution, and that the input is being piped in...

    and b) how can this be made to work, in as similar way to a normal statement as possible?

    Many thanks in advance...

    Carl.

  2. #2
    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.

    A quoting issue. This version seems to work:
    Code:
    #!/usr/bin/env ksh
    
    # @(#) s0	Demonstrate avoiding quoting issues with awk.
    
    # Section 1, setup, pre-solution.
    # Infrastructure details, environment, commands for forum posts. 
    # Uncomment export command to test script as external user.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    set +o nounset
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    C=$HOME/bin/context && [ -f $C ] && . $C 
    set -o nounset
    
    API_awk()
    {
    set +o nounset
    
    pe " debug API_awk, parameters :" "$*" ":"
    awk "$*"
    
    }
    
    # Section 3, solution.
    pl " Results:"
    echo Hello world |
    API_awk '
    {print "Greetings from inside API_awk" }
    {printf "%s,%s\n",$2,$1}
    '
    
    exit 0
    producing:
    Code:
    % ./s0
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0 (lenny) 
    ksh 93s+
    
    -----
     Results:
     debug API_awk, parameters :
    {print "Greetings from inside API_awk" }
    {printf "%s,%s\n",$2,$1}
    :
    Greetings from inside API_awk
    world,Hello
    Best wishes ... 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 )

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    4
    Hi,

    Many thanks for the reply - this works, however, when I try to use the following statements I get an error - can you let me know why?

    ps -eaf|grep -v grep|grep pmon|API_awk '{print $NF}'

    Outputs:
    instance1
    instance2
    ...

    which is right, however:

    ps -eaf|grep -v grep|grep pmon|API_awk '{print $NF}' | API_awk -F_ '{print $3}'

    gives:
    awk: syntax error near line 2
    awk: bailing out near line 2

    My definition of API_awk is:


    API_awk()
    {

    awk "$*"

    }


    - it seems something to do with the -F parameter?

    Thanks

    Carl

  4. #4
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52

    Perhaps:

    Code:
    API_awk()
    {
      awk "$@"
    }

  5. #5
    Just Joined!
    Join Date
    Dec 2010
    Posts
    4
    Ok - this works!

    - I've not met $@... whats the difference between $* and $@?

    Thanks

    Carl

  6. #6
    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.
    Quote Originally Posted by carlbeech View Post
    ...I've not met $@... whats the difference between $* and $@? ...
    Code:
    The  meaning  of $* and $@ is identical when not quoted or when
           used as a variable assignment value or as a file name.   However,  when
           used  as a command argument, "$*" is equivalent to "$1d$2d...", where d
           is the first character of the IFS variable, whereas "$@" is  equivalent
           to  "$1" "$2" ....
    
    excerpt from man ksh, q.v.
    Best wishes ... 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 )

  7. #7
    Just Joined!
    Join Date
    Dec 2010
    Posts
    4
    Many thanks for the assistance - greatly appreciated!

    Carl.

Posting Permissions

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