🖥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
chmod [options] mode[,mode] file1 [file2 ...]
-R Recursive, i.e. include objects in subdirectories.
-v verbose, show objects changed (unchanged objects are not shown).
$ ls -l findPhoneNumbers.sh
-rwxr-xr-- 1 dgerman staff 823 Dec 16 15:03 findPhoneNumbers.sh
$ stat -c %a findPhoneNumbers.sh
754
The r, w, and x specify the read, write, and execute access.
# 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
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
#!/bin/sh
# This is a comment!
echo Hello World # This is a comment, too!
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:
$ chmod a+rx my-script.sh
$ ./my-script.sh
Note:
One weakness in many shell scripts is lines such as:
cat /home/username/somefile | grep "string_to_find"
which would run much faster as:
grep "string_to_find" /home/username/somefile
Last updated
Was this helpful?