Find the answer to your Linux question:
Results 1 to 4 of 4
hi I am trying to run head or tail command from externa argument as below. for example test.sh and run ./test.sh 1000 ------------------ #/bin/ksh print "fred: $*" print "$0: $1 ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    1

    ksh head/tail argument variable

    hi
    I am trying to run head or tail command from externa argument as below.
    for example test.sh
    and run ./test.sh 1000

    ------------------
    #/bin/ksh
    print "fred: $*"
    print "$0: $1 and $2"
    print "$# arguments"
    head -$1 /u01/scripts/dump*|grep sessio|awk '{ print $1`}> /u01/out.txt
    -------------------------------------------------

    whenever I run it gives an empty out file.
    I tried to several times but no result. I alsoedit script as
    head -1000 /u01/scripts/dump*|grep sessio|awk '{ print $1`}> /u01/out.txt
    to see if there another reason, but in this way I can get positive result.

    Regards

    Sinano Deregole

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Did you get an error message when you ran the script? When I did I got 'no closing quote'. Looks like your awk command is wrong. It's

    head -$1 /u01/scripts/dump*|grep sessio|awk '{ print $1`}> /u01/out.txt

    when it should be:

    head -$1 /u01/scripts/dump*|grep sessio|awk '{ print $1}'> /u01/out.txt

    Also change the first line to:

    #!/bin/ksh -vx

    The '-vx' will run the script in verify mode, you'll see the commands as they execute. Also note that your first line didn't have an exclamation point after the pound character.

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    35
    Hey there,

    The above reply is absolutely correct about the format of the awk command and the pound-bang line. Just one extra thing, if you need to figure out what's wrong in your pipe chain.

    If you can, run the series of piped commands using bash and then echo the value of the PIPESTATUS variable. It will tell you which command in your pipe chain is giving the error.

    Since you can get a good result when running this a separate way, this might not have anything to do with I/O redirection, but you can always try to tie stderr to stdout at each point in the chain, too

    , Mike

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i see you have a little bit of awk code there in your script. I suggest you just use awk.
    Code:
    #!/bin/ksh
    number=$1
    awk -v n=$number 'NR < n && /session/{ print $1}' /u01/scripts/dump* > /u01/out.txt

Posting Permissions

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