Find the answer to your Linux question:
Results 1 to 4 of 4
How can I get to know what command/script/function, I just called/ran within a script. Code: [stpuser@stpbuild]$ echo aaa aaa [stpuser@stpbuild]$ echo bbb bbb [stpuser@stpbuild]$ echo ccc ccc [stpuser@stpbuild]$ history|tail -1 ...
  1. #1
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Question Find Last command name within a shell script

    How can I get to know what command/script/function, I just called/ran within a script.

    Code:
    [stpuser@stpbuild]$ echo aaa
    aaa
    [stpuser@stpbuild]$ echo bbb
    bbb
    [stpuser@stpbuild]$ echo ccc
    ccc
    [stpuser@stpbuild]$ history|tail -1
    34      echo ccc
    
    
    [stpuser@stpbuild]$ cat a.sh
    echo aaa
    echo bbb
    echo ccc
    history |tail -1
    [stpuser@stpbuild]$ ./a.sh
    aaa
    bbb
    ccc
    [stpuser@stpbuild]$ . ./a.sh
    aaa
    bbb
    ccc
    37      ./a.sh
    [stpuser@stpbuild]$
    What I want is that, a.sh should display "echo ccc" as it is the last command that was executed before the last command in a.sh script.

    Any helps!
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  2. #2
    Just Joined!
    Join Date
    Jun 2008
    Posts
    39
    This does not exactly answer your question, but if you use "history 2" it will give you something close.
    history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]

  3. #3
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    $LINENO can be useful (if we embed that in our script line) provided, there should not be multiple commands (separated by ";" semi-colon) in one line.
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  4. #4
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    $LINENO can be useful (if we embed that in our script line) provided, there should not be multiple commands (separated by ";" semi-colon) in one line.
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

Posting Permissions

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