Reference article: 1 http://blog.csdn.net/wuwenxiang91322/article/details/9259877 changing file permissions via chmod
Additional knowledge :
1 Linux files have three identities and four permissions, and three identities are:
- U: Owner of File
- G: The group to which the file belongs
- O: Other users
For each identity, there are four different permissions, namely:
- R: Permission to read files (read)
- W: Permission to write to file (write)
- X: Execute permission (execute)
- S: Special Permissions
2 There are two ways to represent file permissions in Linux, namely, numbers and symbolic representations.
3 chmod changing file permissions in digital form:chmod 755 test.sh (Explanation: Read and write execution weights are: 4 2 1 and 755 in turn represent the permissions for other users in the user group, converting 755 into character Type, for Rwxr-xr-x, that is, the file owner, the group and other users can read and run test.sh this file, but only the owner can write to this file, that is, other people do not have the right to modify test.sh this file. (Of course, the root user does not have this limitation, who wants to change who, which is also an experience that embodies the supremacy of the root account )
4 chmod changing file permissions as characters:
chmod u+x test.sh Only add executable permissions to the owner
chmod g+x test.sh Only add executable permissions to group identities
chmod o+x test.sh Only add executable permissions to other people
chmod a+x test.sh gives everyone x permission
chmod o-x test.sh If you want to remove a certain privilege from an identity, you only need to change the + to-can, for example, remove the executable permissions of other people's identities:
5 The process for writing shell scripts is as follows:
- Edit the shell script file in the editor.
- Save the file as a "*.sh" file.
- Add executable permissions to the script file. such as: sudo chmod a+x test.sh (because the general user does not have shell execution privileges)
- Executes a script file with a relative pathname.
6 The habit of writing a shell: In general, write a comment at the end of the file. First, because the shell has multiple versions, the shell is different under different Linux operating systems. For example, under Ubuntu is bash, so first of all indicate what shell script is written, if not, enter Echo $SHELL in the terminal to see the shell version. Second, write the comment description of the shell script, that is, the purpose of the function, the meaning of the parameter. This is the same as writing other programming languages, making it easier for others to understand what they are writing and what their scripts mean.
Text:
A variable
You can use variables in the shell language, but the variables here are different from those in other high-level languages. Variables in shell scripts do not need to be defined in advance, which means that each variable can be used when the programmer needs it. Because most Linux commands use lowercase letters, most of the environment variables in the shell are expressed in larger letters. Therefore, it is best to use lowercase letters for user-defined variables that appear in shell scripts, which can make a good difference to the environment variables in the shell. You can view all system-defined variables by entering set in the terminal. Use the unset command to delete the assignment of a variable so that the value of the variable is empty. If the user needs to clear the value of a variable to do so, it is equivalent to assigning the variable a null value. Scripting variables in the shell are sensitive to shouting and writing, just like Linux and many high-level programming languages. Note Use "#"
- Declaring variables: such as name= "Xiao Ming"
- Use variables such as: Echo $name #echo represents the output, and $ represents the value of name.
- Expression Evaluation: Mode 1 You should use the expr command in the expr command to use the math operator. For example, a numeric comparison operation, an integer operation, or a logical operation. Mode 2 You can also use double brackets in the shell script to evaluate the value of an expression instead of the expr command. The form is as follows: $ ((expression)) such as: Expr 3+2 and $ ((3+2)) are the same
- The variables in the shell script are very special, because the variables are not type-sensitive here. this is completely different from the C language and some other programming languages, and it's easy for beginners to get confused. Essentially, a variable in a shell script is a string, and in the interpretation of a variable it relies on the definition of the variable in the shell script.
- Positional variables: When you run a shell script, you can pass command-line arguments to the script, which can be referenced inside the shell script. Depending on the location of each command parameter, it can be represented in the shell using $ $9. $ A indicates the file name of the current execution process, which is the execution file name of the program.
Two exit states
The exit command ends the execution of a shell script, like the call "exit (0)" In the C language, or in the main function "return 0;" The same. The shell script also returns a value at the end of the run, and this value is passed to the calling script's parent process. This parent process is usually the shell, but sometimes some other user programs invoke the shell script as well. The parent process receives this value (this value is actually part of the end state of the child process) and does the next step. Each shell command returns an exit status code at the end of execution. A successful execution of the command returns 0; otherwise, the unsuccessful command returns a non-0 value. A non-0 value is usually interpreted as an error code, and returning 0 from a conventional standpoint is normal. If a value other than 0 is returned, the process has an exception. You will need to find out why the process exited based on the exit status code. Typically, a protocol that exits the status code should be agreed between the programmer and the system administrator so that the system administrator helps the programmer to find the error. Note: The exit status code must be a decimal number, and the range must be 0 to 255. When the script ends with an exit command with no arguments, the script's exit status code is determined by the last command executed in the script. The command before exit. The following example shows the use of the exit status code for a shell script that invokes a simple program written by a user. Readers can compare the difference between a two-version shell script that exits with the exit command and exits without using the command.
Three-condition test
1 conditional test commands generally have two forms. 1 Test Condition 2 [condition] Note: When using "[]", add a space between the condition and "[]".
There are many conditional expressions for testing file status, but the most common ones are shown below.
-d file is a directory;
-S file is longer than 0;
-F file is a normal file;
Whether the-l file is a symbolic link;
-U file whether the suid bit is set;
-r file is readable;
-W file is writable;
The-x file is executable.
When the return status of the conditional test is 0 o'clock, the test succeeds, otherwise it fails. The following example tests whether the Test.txt file is readable, writable, and executable. The script uses two methods of conditional testing, which the reader can compare to understand.
(1) Edit the script in the VI editor as follows:
#!/bin/sh
# file.sh Test test.txt file is readable writable executable
#测试 the Read permission for the Test.txt file, use the "[]" method
[-R Test.txt]
echo $?
#测试 Write permission to the Test.txt file, use the "[]" method
[-W Test.txt]
echo $?
#测试 the Execute permission for the Test.txt file, use the "[]" method
[-X Test.txt]
echo $?
#测试 the Read permission for the Test.txt file, use the "Test Command" method
Test-r Test.txt
echo $?
#测试 Write permission to the Test.txt file, use the "Test Command" method
Test-w Test.txt
echo $?
#测试 the Execute permission for the Test.txt file, use the "Test Command" method
Test-x Test.txt
echo $?
Exit 0
(2)
After writing the script,
Add executable permissions to the shell script file.
You can use the chmod command to add file.sh
Executable permissions for the file.
$ chmod u+x file.sh
(3) Use the LS command to view Test.txt's permission information.
$ls –l Test.txt
-rw-r--r--
1
Root
0
Feb
7
04:50
Test.txt
(4) Run the shell script in the shell as follows:
0
0
1
0
0
1
Run result Description The Test.txt file has only read and write permissions, but no executable permissions.
The following example tests whether files that pass in the file name of the script are normal files, directory files, and symbolic links. The script uses positional variables as the file name for testing.
(1) Edit the script in the VI editor as follows:
#!/bin/sh
# test.sh types of test files
#第一个文件是一个普通文件
[-F $]
echo $?
#第二个文件是一个目录文件
[-D-$]
echo $?
#第二个文件是一个符号链接
[-L $ $]
echo $?
#运行的 shell script itself is also a common file
[-F $]
echo $?
Exit 0
(2)
After writing the script,
Add executable permissions to the shell script file.
You can use the chmod command to add test.sh
Executable permissions for the file.
$ chmod u+x test.sh
(3) Use the mkdir command to create a directory dir.
$mkdir dir
(4) Create a symbolic link using the symlink command.
$symlink Link Test.txt
(5) Run the shell script in the shell as follows:
$./test.sh test.txt dir link
0
0
0
0
The test results show that each step of the test is successful, and that each file type is the same as expected.
Use logical operators when testing
Similar to the C language, the shell script also provides three logical operations to complete this function. Logical operators are typically and sub-
The implementation process of the program is different with the support statement.
-A logic with, the two operands are true, the result is true, otherwise false.
-O logic or, two operands one is true, the result is true, otherwise false.
! The logic is not, the condition is false, the result is true, otherwise it is false.
Shell Programming Note 1