开发者

terminal sudo command

开发者 https://www.devze.com 2022-12-10 13:34 出处:网络
I\'m a complete novice at this (terminal in Mac leopard) and hoping to get an lifeline from the web as I\'ve certainly hit a wall.

I'm a complete novice at this (terminal in Mac leopard) and hoping to get an lifeline from the web as I've certainly hit a wall.

I want to run a script as a root in terminal. The script is saved 开发者_开发技巧as a text file with a .rtf extension. I've inserted into terminal:

sudo filename.rtf

then I get asked a password which I've typed in (admin password) and pressed enter; then it shows a prompt: sudo: Mac: command not found

I've tried it with the extension hidden but got the same result. Any pointers on what I'm doing wrong?

Thanks in advance


You need to first get the script out of the .rtf file and into a plain text file (open it up in TextEdit and select "Make Plain Text" from the format menu, then save it again as myscript.sh).

Now you can type

sudo sh myscript.sh

The "magic" sh letters there are because as another responder says, sudo will temporarily elevate you to superuser and run a program. In *nix environments, that would be anything with the executable bit set, meaning that someone's explicitly told the operating system that it's safe to run a file. In your case, your myscript.sh has not been "blessed" in this way, so to run it you need to feed it into a program that knows how to understand it. That program is sh, and it does have the executable bit set. Thinking of it as sudo (sh myscript.sh) might make it a bit clearer.

If you plan on running this script a lot, you might want to actually make it executable on its own. This amounts to putting special instructions inside the file that tell the operating system how the file should be run. If you stick #!/bin/sh (this is called a shebang line and tells the OS what to do with your file) on the first line of your script, and then type chmod u+x myscript.sh (this tells the OS that you, and only you, are allowed to execute your file), you'll be able to run the file directly with sudo myscript.sh.


sudo is used to execute commands as the root user of the machine.

when you type

sudo [somthing]

the shell grants temporary root privilges and then executes the given "somthing"

assume your script is in bash, you should have done

sudo sh filename.rtf

Also, it's better to save script as plain txt, with an sh extension, so you would execute

sudo sh myscript.sh


first set the script as executable:

chmod +x filename.rtf

Then you can run it like so:

sudo ./filename.rtf
0

精彩评论

暂无评论...
验证码 换一张
取 消