Josh
Josh

Reputation: 778

In bash, is there a way to suspend a script, let a user enter some commands, and then resume the script once they are done?

I might be approaching this the wrong way, but I am currently writing a script that will deploy some files between environments. The script will be pulling these files from a dev server, and manual edits will need to be made so they work properly once deployed on a test cluster.

I want the user to be able to use vim in the middle of this deploy script to make these edits before the file is then copied to other servers in the test cluster. Then, they could signify they've finished and the script will pick up where it left off. Is this possible?

Upvotes: 0

Views: 160

Answers (1)

that other guy
that other guy

Reputation: 123570

If the script is running interactively, simply invoke an editor on the file:

#!/bin/bash
echo "hello world" > myfile.txt
${EDITOR:-nano} myfile.txt

echo "Here's what you ended up saving:"
cat myfile.txt

This opens the user's preferred editor (with fallback on nano), and will not continue until the user quits the editor. This is similar to how an editor opens when you use git commit.

You can similarly give the user an interactive shell:

#!/bin/bash
echo "Please run any commands you want, and 'exit' when done"
bash
echo "Ok, continuing"

In this case, a shell starts, the user can run whichever commands they want, and when they exit the shell using exit or Ctrl+D, the script continues.

Upvotes: 1

Related Questions