Scripts and Permissions
Manually entering commands is great, but sometimes you want to do the same thing again and again. Rather than having to type the commands every time, you can save your commands in a file and run that.
Creating a shell script
A shell script is just a text file that contains shell commands. You can create a shell script in any text editor, but for now we will create and edit them in the terminal and with `nano“.
Enter the following code in the script file, and save it before exiting.
The shebang (#!
, a.k.a. hashbang) must be the first line of the shell script. When executing the script your shell will read this, and use the indicated path as the shell to run the script. This allows you to specify the shell you are targeting when you write the script.
The remainder of the file contains the commands you want to run, in the order you want them to run.
Permissions
To be able to run the script you need to tell the operating system that this file is executable. This is handled through the permissions system, which we can interact with using shell commands.
Action | Command | Description |
---|---|---|
Change permissions | chmod | Adjust the permissions for a file or folder. |
Unix provides permissions to read, write, and execute files and folders.
Permissions can be associated with individual users or groups of users.
You can see the permissions on files and folders when you use ls -l
, which shows them at the start of each line as three sets of “rwx” values.
These values begin with a single character indicating the type of file, which is most commonly a regular file (”-”) or a directory (“D”).
When using ls -l
the presence of a permission is shown with the relevant character (i.e., “r”, “w”, or “x”), and the absence of that permission is shown with a ”-“.
The first set of rwx values indicates the permissions for the current user.
Next the permissions for the current user’s group are shown.
Then, the last set of rwx values indicate the permissions for any other users.
For example, -rwxrw-r--
indicates that the current user can read, write, and execute the file, other members of the same group and read and write to the file, but all other users can only read the file.
The chmod
program lets you change permissions.
Permissions are also known as file modifiers, hence the name of the command. The easiest way to work with chmod
is to use the format <person>[+/-]<permission>
for arguments, where person
is one of u user, g group, o other, or a all, and permission is one of r read, w write, or x execute. So, you can give all users execute permissions on something using chmod a+x
, or remove read for other users with chmod o-r
.
The following script demonstrates the use of chmod
, and makes our script executable only by you.
For this example we assume that the current user is “multitool”, and the current group is “devs”.
Note that when you run these commands the permissions should match our output, but the dates and times will most likely be different!