Shell Script Series Tutorial Two: Start Shell programming
2.1 How do I write a shell script?
(1) The most common use of VI or mcedit to write shell scripts, but you can also use any of your favorite editor;
(2) After the script is written, to set executable permissions for the script: syntax: chmod [option] Mode script-name
chmod +x script-name chmod775 Script-name
here, 775 of these 3 numbers indicate this file for user (u), group (G), other user (o) permission settings, for example: the first 7, The binary is 111, which indicates that the user is given the read, write, and Execute permissions of this file respectively;
who: U (user), G (group), O (Other), a (user, default); opcode: + (add permission),-(delete permission), = (reassign permission); Permission: R (Read permission), W (Write permission), X (Execute permission), s (set User ID number), t (Sticky bit)
1$chmodU+xfile# Add Execute permissions to the user of this file2$chmod 751# Give this file the user read and write execution permissions, the file group read, execute permissions, other users execute permissions3$chmodU=rwx,g=rx,o=xfile# and the previous statement mean the same4$chmod=rfile# Assign Read permissions to all users (the default user is a, this is the default)5$chmod 444 file# exactly the same as the previous statement6$chmodA-wx,a+rfile# The same as the previous clause,-represents removal Permissions7$chmod+r U+r Directory # recursively assigns read permissions to all files and subdirectories in the directory directory
(3) Use the following command to execute the script:
$ bash script-name or $ sh script-name or $./script-name (your terminal's current path is the path of the script, but you still need to add "./" To find the file from the current folder); (dot) indicates the execution of a given file within the current shell: The syntax is: . Command-name (see [2]).
Below, write down your first shell script: name it
$ clear $ echo " Hello World "
After you save the script, you might use the following command to execute the script:
$./first
This cannot be done because the script does not now have permission to execute: The screen will be emptied and Hello world will be printed on the screen.
chmod 775 first$.
Script commands |
Meaning |
Clear |
Clear Screen |
echo "Hello World" |
Output the value of the information or variable on the screen, which makes the general use of the echo command: echo "Message" |
Exercise : Write the following script, save and execute, observe the output
Ginfo
1 Clear2 Echo "Hello $USER"3 Echo "Today is \c";Date4 Echo "Number of User login: \c";W.H.O.|WC-L5 Echo "Calendar"6 Cal7Exit0
Output:
Hello Wangtoday is \c2016 Friday November 04Ten: $: theCstnumber of UserLogin: \c2Calendar November .Day 1234561 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - + A at - - - - - in -
2.2 Variables in the shell
In Linux (Shell), there are two types of variables:
1) System Variables : Custom and generated by Linux, all in uppercase letters.
2) User-defined variables (UDV): Variables defined and generated by the user.
Some important system variables are shown in the following table:
System variables |
Meaning |
Bash |
Shell path: Like/bin/bash, this command is the same as the shell |
Bash_version |
Shell version, such as: 4.3.11 (1)-release |
COLUMNS |
The coloums of your terminal, such as 80; On my computer, when the terminal is maximized, this value becomes 205 |
HOME |
The home path of the computer, such as/home/wang |
LINES |
and columns mean the same. |
LOGNAME |
Current user name, such as Wang |
OSTYPE |
OS type: such as Linux-gnu |
PATH |
Our path settings |
PS1 |
Our prompt settings |
Pwd |
Current work Path |
SHELL |
Shell path: such as/bin/bash |
Uesrname |
Name of the user currently logged in to this PC |
If you want to print out the current home path, you can:
echo $HOME # output:/home/wang
But can you do that? The following command is OK without any errors, but it will output home instead of what the home refers to and add $ to the front of the home.
Echo Home # Output: Home
2.3 Reading statement (read)
reading data from the keyboard is stored in the variable: syntax: Read var1, Var2, ..., Varn
Write the following script file, prompt the user to enter a name, the user uses the keyboard input name, stored in the specified variable, and then display:
VI Sayh Echo " "read fname # user input is stored inside the fname!!! Echo " Hello $fname, Lets be friend! "
Execute the following statement to run Sayh:
chmod +x sayh$. /sayh
Your First Name:
Zhao
Hello Zhao, Lets be friend!
2.4 Wildcard characters
Reference documents:
[1] Linux Shell Scripting tutorial:http://www.freeos.com/guides/lsst/index.html
[2] http://blog.csdn.net/mci2004/article/details/7182829
Shell Script Series Tutorial Two: Start Shell programming