Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From a bash script in OS X, is it possible to prompt for Administrator permissions?

I have a shell script that adds a line to /etc/hosts. If I run it manually with sudo, it works correctly. I'd like to distribute this file to my coworkers who use Macs. However, when the shell script runs normally, I get a:

sed: /etc/hosts: Permission denied

Is there a way to force a bash script to prompt for a password, as compiled ObjC code can do? If Bash can't do this, can Applescript do so?

like image 279
John O Avatar asked Dec 27 '22 06:12

John O


1 Answers

Adayzdone's answer is a correct way to use "with administrator privileges"

But you do not need to hard code password or create your own dialog.

If you do not include the user name and password you will be prompted with the standard username and password dialogue for an admin.

do shell script "echo hi" with administrator privileges
like image 159
markhunte Avatar answered Dec 29 '22 13:12

markhunte