Skip to content
KALANBLON
Show contents

Chapter 5 of 7 · 20 min

Permissions and first scripts

By the end of this chapter, you'll be able to

  • Read a file's permissions in the output of ls -l
  • Change permissions with chmod, in symbolic and octal notation
  • Write a bash script and make it executable
  • Use variables, arguments and a command's exit status

Reading permissions

ls -l starts each line with ten characters, for example -rwxr-x---. The first one gives the type (- file, d directory). The next nine form three groups of three:

  • for the owner (user, u);
  • for the file's group (group, g), users who share permissions;
  • for others (others, o).

In each group: r (read) allows reading, w (write) changing, x (execute) running as a program; a - means the permission is missing. For a folder, x allows entering it.

Changing permissions with chmod

In octal notation, each group is one digit: r is worth 4, w is worth 2, x is worth 1, and you add them up. 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--, 0 = no permission.

echo "Quarterly report" > report.txt
chmod 640 report.txt
ls -l report.txt | cut -c 1-10
-rw-r-----

640: the owner reads and writes (6), the group reads (4), others get nothing (0). Symbolic notation changes one permission without touching the others: chmod u+x adds execution for the owner, chmod o-r removes reading for others, chmod a+r gives reading to everyone (all).

chmod g+w report.txt
ls -l report.txt | cut -c 1-10
-rw-rw----

Your first script

A script is a text file containing commands. Its first line, the shebang #!/bin/bash, says which program must interpret it. On a real system, you would write it with an editor (nano hello.sh); here, we write it line by line with echo:

echo '#!/bin/bash' > hello.sh
echo 'echo "Hello $1, welcome to the club!"' >> hello.sh
chmod 755 hello.sh
ls -l hello.sh | cut -c 1-10
./hello.sh Kofi
-rwxr-xr-x
Hello Kofi, welcome to the club!
  • single quotes stop the shell from replacing $1 when running echo: the text is written into the file as is;
  • chmod 755 makes the script executable;
  • ./hello.sh runs the script from the current folder. The ./ is required: for safety, the shell does not look for programs in the current folder;
  • $1 is the first argument given to the script (Kofi), $2 the second, and $# their number.

The script ends with the status of its last command: 0 means everything went well.

Step 5 of 5
Text description of the animation
  1. The shell separates the program name from its arguments: $1 will be Kofi.
  2. Without the x permission for you, everything stops here: Permission denied.
  3. The first line names the interpreter: bash reads the rest of the file.
  4. Each command runs in order, with $1 replaced by Kofi.
  5. The script ends with the status of its last command: 0 means everything went well.

Variables and exit statuses

A variable is created with NAME=value, without spaces around the =, and read with $NAME. Inside double quotes, variables are replaced; inside single quotes, they are not.

CITY=Bamako
echo "Delivery planned in $CITY"
echo 'Delivery planned in $CITY'
Delivery planned in Bamako
Delivery planned in $CITY

Every command returns an exit status: 0 on success, another number on failure. $? holds that of the last command. The && and || operators use it: a && b runs b only if a succeeded, a || b only if a failed.

printf "rice\noil\n" > stock.txt
grep -q rice stock.txt && echo "rice in stock"
grep -q sugar stock.txt || echo "sugar missing (status $?)"
rice in stock
sugar missing (status 1)

grep -q (quiet) displays nothing: only its exit status matters, 0 if it found something, 1 otherwise.

Your turn

Mini-lab: a script that welcomes everyoneTerminal simulator

Terminal

Click “Run”: the commands run in a fresh terminal, in your home folder /home/awa. Type “help” for the list of commands.

Teaching simulator: it reproduces the essential Linux commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

The first run fails: that is on purpose. Change the arguments, remove the permission with chmod u-x, try bash welcome.sh Ama.

Worked exercise

Worked exercise: a backup script

Write a copy.sh script that copies the file given as first argument into a backups folder (created if it does not exist), then prints “Copy of NAME complete”. Make it executable for you only (read, write and execute for the owner, no permission for others), then run it on stock.txt.

Hint 1
mkdir -p does not complain if the folder already exists: perfect in a script run several times.
Hint 2
rwx for the owner and nothing for the group and others: 700.
Show the solution and common mistakes
echo '#!/bin/bash' > copy.sh
echo 'mkdir -p backups' >> copy.sh
echo 'cp "$1" backups/ && echo "Copy of $1 complete"' >> copy.sh
chmod 700 copy.sh
./copy.sh stock.txt
ls -l copy.sh | cut -c 1-10
Copy of stock.txt complete
-rwx------

The && only displays the message if the copy succeeded: an honest script does not say “complete” when it has failed.

Common mistakes:

  • Writing the line with double quotes on the outside: $1 would be replaced (by nothing) when running echo, and the script would never receive the argument.
  • Forgetting the quotes around "$1" in the script: a file name with a space would be split into two arguments.

Summary

  1. 1The r, w and x permissions apply to the owner (u), the group (g) and others (o).
  2. 2chmod 640 sets all permissions in octal; chmod u+x adds a single one.
  3. 3A script starts with #!/bin/bash, must be executable, and is run with ./script.sh.
  4. 4$1, $2… are the arguments, $# their number, $? the exit status of the last command.
  5. 5&& chains on success, || on failure; always avoid chmod 777.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.