3

Sometimes, after I run some commands (for example git commands), I discover that I forgot some configurations or forgot to put some requirements... . So I thought about displaying a message, to remind me or warn me, (like the one displayed when we run sudo apt upgrade:

The following packages will be upgraded ... Do you want to continue [Y/n]?)

I want to get a similar message when I run a specific command ("Did you check the conf files, did you update the requirements file?) When I type Y, the command run and when I type n, the command stops.
How to do this in Ubuntu?

4

Typically, when there are things that have to be done before an actual command runs - such as providing and setting up environment variables, adding extra positional parameters, etc - the command is placed into a wrapper script. How it should be implemented - that depends on the specific command you want and things you want to check before actual command runs. In the very simple case such as git, you could do something along the following lines:

#!/bin/sh

printf "%s\n" "Did you check the conf files, did you update the requirements file?[y/n]"
read answer

case "$answer" in
    [Yy])  exec git commit -a ;;
    [Nn]) printf "%s\n" "Please check. Exiting"; exit 1 ;;
esac

Of course the next question is the naming of the command. This script could be named git-commit or something similar. I would advice against naming wrapper scripts same as the original application.

See also:

  • 1
    +1 nice touch adding references to sister-site. I would note best to use "Y" to proceed, any other key to exit. Currently if user presses "U" neither "Y" nor "N" is executed. – WinEunuuchs2Unix Dec 19 '18 at 11:27
  • 1
    @WinEunuuchs2Unix Agreed, that's a better approach. I'll try to remember that for the future – Sergiy Kolodyazhnyy Dec 19 '18 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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