1

I am looking for a workaround for processes with a long duration. There is the special parameter $_ containing the last parameter of the last command. Well I am asking you for something vice versa.

For example:

/etc/init.d/service stop; /etc/init.d/service start

.. could be easier if there is a parameter/variable containing the last binary/script called. Let's define it as $. and we get this:

/etc/init.d/service stop; $. start

Do you have any Idea how to get this? I found this Thread on SO But I only get output like this: printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"

But the var $BASH_COMMAND is working well:

# echo $BASH_COMMAND
echo $BASH_COMMAND

# echo $BASH_VERSION
4.1.2(1)-release

Any help is very appreciated! Thank you, Florian

3
  • Check this: stackoverflow.com/a/4009451/1096140
    – hesham_EE
    Commented Sep 29, 2014 at 14:06
  • Sorry, but that doesn't fit my needs, because the history expansion is going to expand too early. And the !:1 meets the last used parameter not command. Here is my try: {[email protected]} /root/tmp # ls -l insgesamt 0 {[email protected]} /root/tmp # /etc/init.d/httpd stop; !:1 /etc/init.d/httpd stop; -l httpd beenden: [FEHLGESCHLAGEN] -bash: -l: command not found edit:/ how do I get a \n here?
    – Fet32
    Commented Sep 29, 2014 at 14:11
  • Oh! I didn't notice you want the 2 commands in the same line! As you said, this won't work.
    – hesham_EE
    Commented Sep 29, 2014 at 14:21

2 Answers 2

1

You can re-execute the last command by using:

!!

however, this won't help with what you want to do, so you could try using the "search and replace on last command" shortcut:

^<text to search for>^<text to replace with>^

so your problem could be solved using:

/etc/init.d/service stop; ^stop^start^

NOTE: This will only replace the first instance of the search text.

Also, see the comments below by more experienced peeps, for other examples and useful sources.

3
  • +1 Note that ^stop^start is a shortcut for !!:s/stop/start/; there is a wealth of operators you can apply to history expansions, of which !! is just one example.
    – chepner
    Commented Sep 29, 2014 at 14:16
  • @chepner - yup, absolutely. Also, by that token ^foo^bar^ will only replace the first occurrence of the search term, but it's a nice and short, err, shortcut =)
    – Raad
    Commented Sep 29, 2014 at 14:19
  • !!:0 (or !:0) gives you the command word from the previous command -- see gnu.org/software/bash/manual/bashref.html#Word-Designators Commented Sep 29, 2014 at 14:26
0

If the primary problem is the duration of the first process, and you know what the next process will be, you can simply issue a wait command against the first process and follow it with the second.

Example with backgrounded process:

./longprocess &
wait ${!}; ./nextprocess # ${!} simply pulls the PID of the last bg process

Example with manual PID entry:

./longprocess
# determine PID of longprocess
wait [PID]; ./nextprocess 

Or, if it is always start|stop of init scripts, could make a custom script like below.

#/bin/bash
#wrapperscript.sh
BASESCRIPT=${1}
./$BASESCRIPT stop
./$BASESCRIPT start

Since the commands are wrapped in a shellscript, the default behavior will be for the shell to wait for each command to complete before moving on to the next. So, execution would look like:

./wrapperscript.sh /etc/init.d/service

Not the answer you're looking for? Browse other questions tagged or ask your own question.