19

I have several Terminal commands I use for troubleshooting purposes, by typing them in one by one into the terminal window.

These commands require the user's password to be entered.

I'm looking for a way to create a universal (script) file with these commands. I want to be able to run the file on a Mac just by double-clicking on it or dragging the file over the Terminal icon.

I have never done anything like this before. Is there any way to create such a script? Please advise.

  • Do you mean to ask that you want to run a script without having the user enter their password? – Allan Mar 21 '17 at 19:38
  • @Allan I want to run one file instead of multiple commands. Running it without having the user enter their password at least once would be good, but most likely impossible if I'm not mistaken. – 0lesya Mar 21 '17 at 19:51
  • That's definitely possible unless you need to do something that requires admin -- see my answer below – Cullub Mar 21 '17 at 20:52
29

Short answer

A .command script should do the trick


Step-by-step

  1. Open TextEdit and create a new file
  2. Convert it to plain text by clicking Format > Make Plain Text
  3. Add your commands, one per line. For example, you could do:
    #! /bin/bash cd ~/Desktop mkdir myCoolFolder cd myCoolFolder

  4. Run chmod u+x ~/Desktop/myCommandScript.command in your terminal, where ~/Desktop/myCommandScript.command is the path to your script. This will give the terminal permission to run the file.

  5. You're done! Double-click the file to run. Dragging over the terminal icon will also work.

Notes:

  • If you need to do something that requires root (admin) access, you can prefix your command with sudo. When the script runs, you'll have to enter your password (and be an administrator)
  • If the end user isn't an administrator, but you need to do something that required root access, you can use su someAdminName, which will perform the command as someAdminName (you'll need his password).

You can also see here on Stack Overflow for a bit more information.

  • Is there a way to make a command file not to log out? I want to have a command file which opens the terminal with predefined path variable. – Royi Sep 1 '18 at 15:53
6

A script is just a series of commands, so you could put it into a bash script.

#!/bin/bash

command 1
command 2
command 3

Now, this all depends on the they types of command and whether or not they require user intervention.

However, if you are getting diagnostic info, for example, you can have a script that does

#!/bin/bash

# Get SMART status of main drive
diskutil info disk0 | grep -i smart

# Get the model of the machine in question
system_profiler SPHardwareDataType | grep -i Identifier

Those are just to examples. Keep in mind that any output generated, will get sent to the console (screen). However, you can redirect the output to a file by appending >> /path/to/outputfile.txt at the end of each command so you have a file which you can parse once the file has finished running.

You can name the file diagnostics.sh and place it anywhere you like. Just make sure you make it executable by issuing the command chmod +x diagnostics.sh and use ./diagnostics.sh to execute it or double click on it to run it if it's saved to your desktop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .