I want to be able to run a command, it fails cause it doesn't have proper permissions. Then I can write "please" to sudo the command I just ran.
Here's what I want to do in the terminal:
$ run command
"you don't have access to do that"
$ please
"ran successfully"
I saw that !!
will grab the previous command, so I thought I could use that, but I can't get it to work.
my please.sh shell script looks like this, but I can't get any of these to work. It just says "command not found !!" and prints out the sudo usage.
#!/bin/zsh
#sudo !!
#sudo `!!`
sudo $(!!)
Best Answer
You cannot use
!!
in a shell script, as you cannot access the parent shell in a child shell. Though I recommend usingsudo !!
, if you really want to make a BASH script, you would have to use .bash_history, like so:It is definitely NOT a perfect solution, but it should do the trick.
If you are using ZSH, this will not work, as ZSH does not output to .bash_history (of my knowledge).UPDATE: Here is a version that should work with ZSH:Hope this helps!
If you don't understand the script, it simply runs the last command entered in BASH with sudo.