Shell Script to Toggle Between Two Commands
One good way of accomplishing this is for the script to create a blank "configuration file":
- The 1st time the script runs, it sees the file doesn't exist, creates it, and runs
command1
. - The 2nd time the script runs, it sees the file does exist, deletes it, and runs
command2
. - The 3rd time the script runs, it sees the file doesn't exist, creates it, and runs
command1
. - The 4th time the script runs, it sees the file does exist, deletes it, and runs
command2
.
And so forth.
Here's a script that does that:
#!/bin/sh
# This shell script is PUBLIC DOMAIN. You may do whatever you want with it.
TOGGLE=$HOME/.toggle
if [ ! -e $TOGGLE ]; then
touch $TOGGLE
command1
else
rm $TOGGLE
command2
fi