tags:

views:

377

answers:

3

Hello, I'm a complte 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 and .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

+5  A: 

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.

pumpkin
Maybe explain what the magic letters 'sh' do, the asker seems pretty new and might not understand why they are needed.
davr
Thanks for the suggestion :) I edited it to be a bit clearer on that point.
pumpkin
A: 

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
Am
A: 

first set the script as executable:

chmod +x filename.rtf

Then you can run it like so:

sudo ./filename.rtf
Brian Gianforcaro
-1 because rtf contains random formatting chars...99% chance executing it like this wont work. See pumpkin's answer
davr
@davr, I was assuming he just added the file extension .rtf and that it wasn't a rich text formatted file. Thanks for the down vote, I appreciate it.
Brian Gianforcaro
+1 because although it will not work in this particular situation, this does indeed answer the OP's question and will be useful for him in the future. Just not for this particular fle.
Rayne