We are going to write a program to execute the commands we have learned. There are three things need to understand beforehand, authority, vim and shell script.
Authority
The most cardinal feature in Linux is the authority. We can use ls -l
or ll
to list the file and its permission in the current working directory. The owner and the group owner are shown at the third and the fourth column.
digit | possibility | meaning |
---|---|---|
1 | d, - | whether it is a directory |
2-4 | r,w,x | the permission of “owner” |
5-7 | r,w,x | the permission of “group owner” |
8-10 | r,w,x | the permission of “others” |
The 2 to 10 digits in first column are all in r
, w
or x
.
permission | meaning | 2-based |
---|---|---|
r | readable | r– = 100 = 4_2 |
w | writable | -w- = 010 = 2_2 |
x | executable | –x = 001 = 1_2 |
Since the digit are all in boolean type, it can be presented as three decimal number with each digit (0-7) means the permission of owner, group and others. The command chmod
change the permission of a file. For example, chmod 664 a.txt
changes the authority of a.txt. Let both owner and group owner can both read and write, and the others can read only.
The command chown
change the owner of a file. For example, chown root a.txt
changes the owner from bobo to root user.
Apt and Vim
Vim is an very useful text editor. However, it has to be installed from the internet. The apt tool is most recommended for installing package in Ubuntu. It helps you to download the package with its dependency from the Internet and install automatically.
1 | sudo apt install vim |
You will see a text editor in terminal. There are two modes in vim, normal mode (default) and insertion mode (only this mode can be edited). Press a
or i
to switch from normal mode to insertion mode and press Esc
to switch back.
The :w
in normal mode means saving the file, and the :q
in normal mode means quit. They can be combined as :wq
as well. If you want to discard the changes without saving, just type :q!
to quit mandatory.
There are several hot key in Vim, such as dd
(cut in normal mode) and p
(paste in normal mode).
Shell script
Now its time for writing a script to execute the Linux command. Use touch
command to create a empty text file and change it to executable file.
1 | touch test.sh |
Open the shell script by a text editor (i.e. vim test.sh
) and type the following (You need to press a
or i
to switch to the editing mode). The first line of a shell script specified the launcher. It is usually written as !#/bin/bash
.
Next, press :wq
to save and quit. We can use cat test.sh
to verify the edition. Then execute the shell script by ./test.sh
. The ./
is necessery because the test.sh isn’t the system command so we have to specify the path of the shell script.
Comments and variables
We use the bash to execute the shell script. It also supports the condition and loop similar to the general purpose languages.
There are several differents bewteen the general purpose language and bash commands.
- The equal sign cannot allow spaces on both sides while the declaration. i.e.
a=1
is valid buta = 1
not. - The dollar sign is necessery to be used while using declared variable. Furthermore, all the variables are in string type in default, and number variables must be used with $(()).
- The text in ‘’ are pure string. i.e. ‘$a’ is pure string, but “$a” is calling the variable, so does $a.
The linux commands we have learned before are all valid in shell script, and there are further commands that is useful.
Commands | Description |
# | One line comment. (Exclude the #!/bin/bash in the first line) |
:<<!text! | Multiline comment |
input and output
Note that the second argument of the command read -p
is the variable stores the user’s input, so we have to use ‘’ to let the first argument ‘who are you?’ be a pure string.
arithmetic
Remember that the default type we called is string, so we have to use $(()) to convert it to number. Shell script also supports +
, -
, *
, /
(integer division).
if else
Here is an example. Note that the operands, the operators and the brackets in the condition must remain space for each other. It is different from variable declaration.
More conditions are in the following table.
Conditions | Meaning |
---|---|
-eq, == | equal |
-ne, != | not equal |
-gt | greater than |
-ge | greater or equal than |
-lt | less than |
-le | less or equal than |
for
while
function
The argument of a function doesn’t need to specify in the (). Use ${num} to call the input. However, ${0} is the command of calling the script.
Outer arguments
The outer arguments are frequently used in software development. See the following example.