🖥Scripting

The basics of shell script programming

Script output (such as "Hello World" below) is displayed at the start of the line.

$ echo '#!/bin/sh' > my-script.sh          # Creat and write in file
$ echo 'echo Hello World' >> my-script.sh  # update file contcont 
$ chmod 755 my-script.sh                   # Set file Permission 
$ ./my-script.sh
Hello World
$

# File permission 

# 4 stands for "read",
# 2 stands for "write",
# 1 stands for "execute", and
# 0 stands for "no permission."

# 7 = 4 + 2 + 1 --> rwx
# 5 = 4 + 1     --> r-x

# owner-group-others

# 10 character permission

# first character is object type
# a hyphen represents a plain file
# d -> directory file
# l -> symlink file
# s -> socket file
# p -> pipe (FIFO, Read and Write)


# 2-4 character is owner perminssion
# 5-7 character is group permission
# 8-10 character is other permission

# permission are
# r->Read, w->write, x-> excute, s->Stiky bit)

Chmod Terminology

Permission

rwx

Binary

7

read, write and execute rwx

111

6

read and write rw-

110

5

read and execute r-x

101

4

read only r--

100

3

write and execute -wx

011

2

write only -w-

010

1

execute only --x

001

0

none ---

000

Reference

Class

Description

u

user

file owner

g

group

members of the file's group

o

others

users who are neither the file's owner nor members of the file's group

a

all

all three of the above, same as ugo

Operator

Description

+

adds the specified modes to the specified classes

-

removes the specified modes from the specified classes

=

the modes specified are to be made the exact modes for the specified classes

Note that to make a file executable, you must set the eXecutable bit, and for a shell script, the Readable bit must also be set:

Note:

One weakness in many shell scripts is lines such as:

which would run much faster as:

Last updated