0

E.g., I wish to have a shell script that, with a key combination (for example CTRL S, or maybe with a signal) allow me to switch between 2 or more options in runtime, like:

  • print the number of process on system every 5 seconds
  • CTRL S
  • print the current time
  • CTRL S
  • again print the number of process on system every 5 seconds
  • CTRL S
  • print the current time

etc...

I use this construct, maybe someone could suggest another, more elegant, form:

while true; do
    echo a
    read -t5 -n1
done
while true; do
    echo b
    read -t5 -n1
done
source ./myscript.sh
4
  • 2
    Do you actually have a mechanism to do one of these two things in place already and just need to know how to toggle between them based on a signal or keypress, or are you also asking someone to tell you how to set up a periodic update (to the prompt or whatnot)? Note that a good SO question is narrowly scoped -- asking how to trap a signal and toggle a variable is a better question than including the things you want to do based on that variable's value in-scope. Which is to say, a larger number of narrow questions is better than a small number of broad ones... Commented Jun 7, 2018 at 15:45
  • ...in part because the narrow questions are likely to have already been asked and answered, so when you break your question down into slices, you can only ask about the specific aspects that nobody has already tried to do (and gotten assistance/instruction on already in our knowledge base) in the past. Commented Jun 7, 2018 at 15:47
  • screen? tmux? Run two terminals on a terminal multiplexer, one with while sleep 5; do ps aux | wc -l; done the second with while sleep 1; do date; done and you can switch between them using (CTRL+B)+0 or (CTRL+B)+1 in tmux? Commented Jun 7, 2018 at 15:54
  • No, i just need to know how to toggle between 2 or more commands (like a wheel, so that scripts not exit on last command but restart from first command), not that specifics commands in question Commented Jun 7, 2018 at 15:55

1 Answer 1

3

This doesn't do quite what you're asking for: it spits out the number of processes every 5 seconds, and responds at any time to CtrlT to emit the current time.

#!/usr/bin/expect
puts "hit ctrl+T for the current time"
spawn -noecho sh -c {while :; do ps a | wc -l; sleep 5; done}
interact \024 {puts [timestamp -format "%Y-%m-%d %H:%M:%S"]}

I chose Ctrl+T to avoid clashing with Ctrl+S that pauses the terminal.


A command switcher that responds to CtrlC

#!/bin/bash

trap switch_command INT

switch_command() { 
    (( idx = (idx + 1) % ${#commands[@]} ))
    echo "switch to: ${commands[idx]}"
}

commands=(count_procs show_time)
idx=0

count_procs() { ps a | wc -l; }
show_time() { date; }

echo "Ctrl+C to switch commands"
echo "'kill $$' to stop"
while :; do
    "${commands[idx]}"
    sleep 5
done
Sign up to request clarification or add additional context in comments.

4 Comments

thanks glenn, but it's not what i've asked: i use that 2 commands just for example. I need a way to switch between 2 commands in a script with a key combination
I solved with a switcher that responds to Ctrl-C, but mystery about the behavior of trap command SIGQUIT remains
Well, there are some signal that cannot be trapped, and apparently there are some signals that cannot be completely trapped.
@glennjackman Nice script. Any chance you can explain how it works. I need a wallpaper switcher and this looks like it's ideal, but I don't know how it works, or how I would get it into .bashrc the way it's formatted. How does the switch_command() function get called? It's not obvious to me. I'm trying to set up a script to toggle between any of 4 specific wallpapers on a key combination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.