11

I would like to have the following command structure:

command && check-status | less && followup-command

This would pause the execution while the user interacted with less. How can the user force less to exit with a non-zero status to prevent followup-command from running?

I am currently using less 458 (POSIX regular expressions)

1 Answer 1

15

In the simple case, you can ask less not to handle SIGINT, then Control-C will kill it and the exit code will be non-zero. Do this with option -K.

command && check-status | less -K && followup-command

As a workaround for older versions of less, you can do something like

command && bash -c 'trap "exit 1" int;check-status | less -K;' && followup-command

If you don't have -K, omit it, but you'll have to signal and then type quit too.

6
  • less -K still returns a 0 exit code on ctrl-c for me. v418 on mac and v436 on linux. less -K test; echo $? => 0
    – Matt
    Commented Jun 6, 2016 at 4:21
  • that's sad. it's ok on a v458 and v481 for me.The man page for -K explicitly says exit ...(with status 2). Does yours?
    – meuh
    Commented Jun 6, 2016 at 5:01
  • man only says Causes less to exit immediately > greenwoodsoftware.com/less/news.443.html
    – Matt
    Commented Jun 6, 2016 at 7:06
  • I've added a workaround for an older less.
    – meuh
    Commented Jun 6, 2016 at 7:31
  • 2
    Note that the ^C will also kill check-status if it's not finished already. Commented Jun 6, 2016 at 8:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .