Retrieving previous command in a script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retrieving previous command in a script
# 1  
Retrieving previous command in a script

i know from the command line, the symbol $_ is used to get the last command that was run.

however, id like to replicate this within a script.

meaning, how do i do something like this:

Code:
#!/bin/sh
ps -ef | egrep talling
StoreThisLastCommandA=$_
awk '/error/ {print $3}' /tmp/test
StoreThisLastCommandB=$_

My goal is to have the variable StoreThisLastCommandA contain
Code:
 ps -ef | egrep talling

since that is the last command ran at that point in the script.

Likewise, I want the variable StoreThisLastCommandB to contain
Code:
awk '/error/ {print $3}' /tmp/test

because, again, that is the last command kicked off at that point.
# 2  
And the question is ...? You may want to refer to your shell's man page for an explanation of that special parameter.


Please become accustomed to provide decent context info of your problem.
It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.
# 3  
The OS is any Unix system and I intend to use it across all platforms.

But if you need me to narrow it down:

Code:
OS:
RedHat
CentOS
Fedora
AIX
Raspian
Ubuntu

Shell: 
/bin/sh

# 4  
I'm afraid that with /bin/sh you're out of luck: It doesn't offer the $_ special parameter (according to its man page) nor the history command that e.g. bash provides.
# 5  
Quote:
Originally Posted by SkySmart
The OS is any Unix system and I intend to use it across all platforms.
But if you need me to narrow it down:
Code:
OS:
RedHat
CentOS
Fedora
AIX
Raspian
Ubuntu
Shell: 
/bin/sh

Hello SkySmart,

Could you please try the following script and let me know if this helps you.
Code:
cat script.ksh
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
ps -ef | egrep talling
StoreThisLastCommandA=$previous_command
awk '/error/ {print $3}' /tmp/test
StoreThisLastCommandB=$previous_command
##Printing the variables values here.
echo "Value of variable StoreThisLastCommandA is: " $StoreThisLastCommandA
echo "Value of variable StoreThisLastCommandB is:" $StoreThisLastCommandB

Since I don't have sh with me so I haven't tested it though I believe it should work properly, kindly do let me know on same.

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 6  
Another technique:
Store the command first, put it into 'ticks'. A tick within ticks is escaped '\''.
When running the command "eval" lets pipes semicolons etc. work.
Code:
#!/bin/sh
CommandA='ps -ef | egrep talling'
eval $CommandA
CommandB='awk '\''/error/ {print $3}'\'' /tmp/test'
eval $CommandB

Or, store the commands in a separate file or have them in a here document,
and read/run them line by line
Code:
#!/bin/sh
while read cmdline
do
  eval $cmdline
done <<'_EOF'
ps -ef | egrep talling
awk '/error/ {print $3}' /tmp/test
_EOF

This User Gave Thanks to MadeInGermany For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #494
Difficulty: Easy
A block of code is a section of code that has been logically grouped together.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Retrieving values from tab-delimited file in unix script

Hi I am trying to retrieve values from a tab-delimited file.I am using while read record value=`echo $record | cut -f12` done Where 12 is the column no i want retieve and record is one line of the file. But it is returning the full record. Plz help (4 Replies)
Discussion started by: akashtcs
4 Replies

2. UNIX for Advanced & Expert Users

Retrieving command line arguments of a particular PID

hi The "ps" command shows the command line arguments of running processes: $ /bin/ps -o pid,args -e .... 26031 pico /tmp/crontab2KaG1Y 596 /usr/lib/sendmail -bd -q15m 9955 xterm -n 1 -sb -sl 800 -g 80+70+70 2627 /usr/sbin/snmpd -Lsd -Lf /dev/null -p /var/run/snmpd -a 1691 .... I... (2 Replies)
Discussion started by: Andrewkl
2 Replies

3. HP-UX

Previous command

Hi, i would like to retrieve (only retrieve, not execute) a previous command which i executed on the command prompt. I tried the 'up arrow' key, but its not working. Instead,it showed me some funny characters. I searched around and i found the way to do it was using the way below. Several... (5 Replies)
Discussion started by: new2ss
5 Replies

4. HP-UX

Retrieving the full command

Hi All, I would like to retrieve the whole command that got executed for a running process rather than a truncated string, as a result of running ps -ef on HP-UX. Please help. TIA (1 Reply)
Discussion started by: trocadero
1 Replies

5. UNIX for Dummies Questions & Answers

Previous Command

We have a Solaris Server and I am acessing it through telnet to run various commands for my data validation like GREP, SED, AWK etc. My requirement is how do i use the previous command that was executed. I tried the option 'ESC' and then 'k'. It displays the characters like '^[[A' etc. How do I... (2 Replies)
Discussion started by: vskr72
2 Replies

6. Shell Programming and Scripting

Previous Command

We have a Solaris Server and I am acessing it through telnet to run various commands for my data validation like GREP, SED, AWK etc. My requirement is how do i use the previous command that was executed. I tried the option 'ESC' and then 'k'. It displays the characters like '^[[A' etc. How do I... (1 Reply)
Discussion started by: vskr72
1 Replies

7. UNIX for Advanced & Expert Users

Previous Command

We have a Solaris Server and I am acessing it through telnet to run various commands for my data validation like GREP, SED, AWK etc. My requirement is how do i use the previous command that was executed. I tried the option 'ESC' and then 'k'. It displays the characters like '^[[A' etc. How do I... (0 Replies)
Discussion started by: vskr72
0 Replies

8. UNIX for Dummies Questions & Answers

Seeing previous command

Guys, In work we have just gotten a new HP 9000 server and I'm trying to set it up so that if I press <Up arrow> key I can see the command I typed in last(and press enter to run it again). Can anyone advise how to set this up. One friend suggested I touch a file called .toucheditXXXX (I cant... (4 Replies)
Discussion started by: JackieChan
4 Replies

9. UNIX for Dummies Questions & Answers

Retrieving the execution time of a completed command

Hello, I am new to this forum and relatively new to unix, but hope to become an expert soon! My question is: How can I find out the execution time of a command that has already completed execution? More specifically, I launched a Python script to populate a PostgreSQL database on Suse... (2 Replies)
Discussion started by: marina
2 Replies

10. UNIX for Dummies Questions & Answers

getting the previous command

Hello all I'm on SunOS5.9 and I'm trying to make the arrow keys work as in DOS ..get the previous command , etc .. I searched this site and got an answer. I pasted the following code to my .kshrc file in my home directory...It still does not seem to work ...Am I missing anything here ? I'm... (5 Replies)
Discussion started by: luft
5 Replies

Featured Tech Videos