Results 1 to 10 of 10

Thread: a script to give me the last argument !!!

  1. #1

    a script to give me the last argument !!!

    Hi

    I just want a shell script which gives me the last argument passed.

    For example,
    if the script script.sh is executed

    >script.sh one two three

    it must give me "three"

    Also,

    >script.sh one two "three four"

    must give me "three four"

    Could u geeks help me ??????

    thanks in advance,
    rameshvl


  2. #2
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re: a script to give me the last argument !!!

    Only if you send me a script that makes my PC brew good beer and shoot it out the useless floppy drive into my mug ;D

  3. #3

    Re: a script to give me the last argument !!!

    [quote author=rameshvl link=board=9;threadid=6893;start=0#64253 date=1051249815]
    Hi

    I just want a shell script which gives me the last argument passed.

    For example,
    if the script script.sh is executed

    >script.sh one two three

    it must give me "three"

    Also,

    >script.sh one two "three four"

    must give me "three four"

    Could u geeks help me ??????

    thanks in advance,
    rameshvl


    [/quote]

    You know, if you are looking for help, you might want to be more courteous... Calling people "geeks" is kind of rude.

  4. #4

    Re: a script to give me the last argument !!!

    Did you even look or try anything?

    Put this in a file and run it.

    echo $3

  5. #5

    Re: a script to give me the last argument !!!

    Code:
    #!/bin/bash
    ARGV=$*
    echo $ARGV |awk '  {  printf(" \n %d and arg-1 %d ", $NF , $(NF-1) ); } '
    $* is all arguments provided
    there you go, NF is the number of records you can play with that.

  6. #6
    Guest

    Re: a script to give me the last argument !!!

    bash scripting supports printf() - kewl, you learn something every day.


  7. #7

    Re: a script to give me the last argument !!!

    yep, oh forgot to say if you ain't to good at coding %d = interget(number), %s (string).
    you can also have if statements within awk so it's farily straightforward.

  8. #8
    Guest

    Re: a script to give me the last argument !!!

    [quote author=trickster link=board=9;threadid=6893;start=0#64269 date=1051273494]

    You know, if you are looking for help, you might want to be more courteous... Calling people "geeks" is kind of rude.
    [/quote]

    He meant geeks as in computer geeks and most people here I bet will call themselves geeks all the time and dont mind being called geeks, it's a badge of honor

  9. #9

    Re: a script to give me the last argument !!!

    exactly, thanks a lot, Mr. vlad902

    Anyway, I found a simpler solution for my problem, let me share it with u all,

    shift $(($# - 1))

    will do the trick,
    I can get the last argument with $1 after the above line

    Thanks a lot to everyone who replied,

    regards,
    rameshvl

  10. #10
    Senior Member
    Join Date
    Sep 2002
    Posts
    421

    Re: a script to give me the last argument !!!

    The simplest would be to use the special variable !$ which always contains the last argument to the last command

    This tip was brought to you by www.unixtips.org

Similar Threads

  1. Passing Argument from Asterisk to Perl
    By padmaja3 in forum Linux - Software, Applications & Programming
    Replies: 0
    Last Post: 09-26-2007, 02:21 PM
  2. ifconfig eth0 returns SIOCSIFFLAGS: invalid argument
    By gvazqu1cs in forum Linux - Hardware, Networking & Security
    Replies: 1
    Last Post: 03-28-2005, 07:11 PM
  3. Give it a try
    By mugs in forum Linux Distros
    Replies: 4
    Last Post: 11-18-2003, 03:56 PM
  4. ready to give up
    By krinkstar in forum Linux - General Topics
    Replies: 20
    Last Post: 04-27-2002, 07:35 PM
  5. Replies: 5
    Last Post: 04-18-2002, 10:56 AM

Bookmarks

Posting Permissions

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