Shell programming notes 1, shell programming notes
Article: 1 http://blog.csdn.net/wuwenxiang91322/article/details/9259877 changing file permissions through chmod
Additional knowledge:
1Linux Files have three identities and four permissions. The three identities are:
Each identity has four permissions:
2In Linux, there are two methods for expressing file permissions: numbers and symbols.
3Chmod: Change the File Permission In numbers: chmod 755 test. sh, rwxr-xr-x, which means that the file owner, owner group, and other users can read and run test. sh. sh file. (Of course, the root user does not have this restriction, and whoever wants to change should be the root user. This is also an experience that reflects the supreme power of the root account)
4Chmod changes the file permission in the form of characters:
Chmod u + x test. sh only adds executable permissions to the owner
Chmod g + x test. sh only adds executable permissions to group identities
Chmod o + x test. sh only adds executable permissions to other identities
Chmod a + x test. sh grant x permissions to all users
Chmod o-x test. sh to remove a permission for an identity, you only need to change + to-. For example, you can remove the executable permissions for other identities:
5The procedure for writing shell scripts is as follows:
6The habit of writing shell: In general, write a comment at the beginning of opening the file. first, because the shell has multiple versions, the shell is different in different linux operating systems. for example, bash is used in ubuntu, so it first shows what shell script is written. If you do not know, enter echo $ SHELL in the terminal to view the shell version. second, write the description of the shell script, that is, the purpose of the function and the meaning of the parameter. this is actually the same as writing other programming languages, so that others can understand the meaning of their scripts.
Body:
One Variable
Variables can be used in shell language, but the variables here are different from those in other advanced languages. Variables in shell scripts do not need to be defined in advance, that is to say, every variable can be used as needed by programmers. Most Linux commands use lower-case letters, while most shell environment variables use larger letters. Therefore, it is better to use lower-case letters for User-Defined variables in shell scripts to distinguish the environment variables in shell. You can enter set in the terminal to view all the system-Defined variables. You can use the unset command to delete the value assignment of the variable so that the value of the variable is null. If you need to clear the value of a variable, you can do this, which is equivalent to assigning the value of this variable to null. Shell script variables are sensitive to shouting and writing, which is the same as linux and many advanced programming languages "#"
Two return states
The exit command ends a shell script, just like calling "exit (0)" in C or "return 0;" in the main function. A shell script returns a value when it stops running, and the value is passed to the parent process that calls the script. This parent process is usually shell, but sometimes other user programs also call shell scripts. After the parent process receives this value (in fact, this value is part of the sub-process end state), it performs the next step. Each shell command returns an exit status code at the end of execution. If the command is successfully executed, 0 is returned. Otherwise, a non-zero value is returned for an unsuccessful command. A non-zero value is usually interpreted as an error code. From the conventional perspective, returning 0 indicates normal. If a non-zero value is returned, the process has encountered an exception. At this time, you need to find the reason for the process to exit based on the exit status code. Generally, the programmer and the system administrator should agree on an exit status code protocol, so that the system administrator can help the programmer find errors. Note: The exit status code must be in decimal format and must be 0 to 255. When the script ends with an exit command without parameters, the exit status code of the script is determined by the last command executed in the script. That is, the command before exit. The following example demonstrates the use of the exit status code of the shell script, which calls a simple program compiled by the user. You can compare the two versions of shell scripts that exit without the exit command.
Three pieces of test
A conditional test command is generally in two formats. 1 test condition 2 [condition] Note: When "[]" is used, a space must be added between the condition and.
There are many conditional expressions for the Test File status, but the following are the most common expressions.
-D indicates whether the file is a directory;
-S file length is greater than 0;
-F indicates whether the file is a common file;
-L whether the file is a symbolic link;
-Whether the suid bit is set in the u file;
-R file readable;
-W indicates whether the file can be written;
-X indicates whether the file is executable.
If the return status of the conditional test is 0, the test is successful. Otherwise, the test fails. The following example tests whether the test.txt file is readable, writable, and executable. This script uses two conditional testing methods for your understanding.
(1) edit the script in the vi editor as follows:
#! /Bin/sh
# File. sh test whether the test.txt file is readable and writable and executable
# Test the read permission of the test.txt file by using the [] Method
[-R test.txt]
Echo $?
# Test the write permission of the test.txt file by using the "[]" Method
[-W test.txt]
Echo $?
# Test the execution permission of the test.txt file by using the "[]" Method
[-X test.txt]
Echo $?
# Test the read permission of the test.txt file by using the "test command"
Test-r test.txt
Echo $?
# Test the write permission of the test.txt file, using the "test command" Method
Test-w test.txt
Echo $?
# Test the execution permission of the test.txt file by using the "test command" Method
Test-x test.txt
Echo $?
Exit 0
(2)
After writing the script,
Add executable permissions for shell script files.
You can use the chmod command to add file. sh.
File executable permissions.
$ Chmod u + x file. sh
(3) Use the ls command to view the permission information of test.txt.
$ Ls-l test.txt
-Rw-r --
1
Root
0
Feb
7
Test.txt
(4) run the shell script in shell as follows:
0
0
1
0
0
1
The running result indicates that the test.txt file only has the read and write permissions, but does not have the executable permissions.
The following example shows whether the file with the input script file name is a common file, directory file, and symbolic link. This script uses location variables as file names for testing.
(1) edit the script in the vi editor as follows:
#! /Bin/sh
# Test. sh test file types
# The first file is a common file
[-F $1]
Echo $?
# The Second file is a directory file
[-D $2]
Echo $?
# The Second file is a symbolic link
[-L $3]
Echo $?
# The Running shell script itself is also a common file
[-F $0]
Echo $?
Exit 0
(2)
After writing the script,
Add executable permissions for shell script files.
You can use the chmod command to add test. sh.
File executable permissions.
$ Chmod u + x test. sh
(3) Use the mkdir command to create a directory dir.
$ Mkdir dir
(4) use the symlink command to create a symbolic link.
$ Symlink link test.txt
(5) run the shell script in 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 the type of each file is the same as expected.
Use logical operators during testing
Similar to C, shell scripts also provide three logical operations to complete this function. Logical operators
Statements are used in combination to implement different program execution processes.
-Logic a and the two operands are true and the result is true. Otherwise, the result is false.
-O logic or, if one of the two operands is true, the result is true. Otherwise, the result is false.
! If the logic is not, the condition is false, and the result is true. Otherwise, the result is false.