0

I'm creating a shell script that will always read the user's current commands from the shell.

I'm currently using the read command which I'm not satisfied with since it is used for prompting questions. Example:

root@hostname: ./script.sh
Question here?
answer - `read` command 

I want the my script to be invoked when the user directly inputs a command on the command line (the script is already running through /etc/profile.d/myapp.sh once logged in).

root@hostname: read the command here
result will happen

My example script myapp.sh:

#!/bin/bash
if [ "input" = "value"]
then
    do some actions
fi
Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
BLNK
  • 123
  • 1
  • 12

1 Answers1

0

If you are explicitly targeting Bash, I would recommend setting up Bash aliases for those commands.

At the start of the user's session, your program can run the following commands to create bash aliases:

alias command1="/your/path/script.sh command1"
alias command2="/your/path/script.sh command2"

Then for instance when the user enters command1, it will expand and run /your/path/script.sh command1 instead.

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
  • i change command1 into `sudo -s` i receive `-bash: alias: sudo not found`. i want is when a user type a sudo -s command a follow up question will proceed – BLNK Sep 02 '15 at 06:38
  • @BLNK I wouldn't recommend doing the following, but it might be a starting off point: You could change `command1` into `sudo` instead of `sudo -s`. So for instance: `alias sudo="/your/path/script.sh"`. Then you could catch the `-s` argument in your program. It seems like a very bad idea to override the system `sudo` command though - it will probably end up breaking stuff at best, and compromising system security at the worst - so why not choose a custom command for your program, sort of like `mysudo`? – Maximillian Laumeister Sep 03 '15 at 19:39
  • I was thinking if it is possible then since it is an open source software otherwise you mentioned is best in practice since it is a built-in security command. Thanks! – BLNK Sep 04 '15 at 03:19
  • It is already working. Thanks! Normal users can't use the `sudo` command easily :) – BLNK Sep 04 '15 at 03:40
  • follow up question if it is ok.since i ran the program and whatever command i do with the `sudo` it will ran the pointed sh file. But how can it catch if it has the `-s` as you also mentioned? – BLNK Sep 04 '15 at 04:37
  • @BLNK This post may help as far as parsing command line arguments such as `-s`: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash – Maximillian Laumeister Sep 04 '15 at 17:01