Linux FAQ 1

Source: Internet
Author: User
Tags shebang
In linux, # is used in Shell scripts to express comments, which is equivalent to the C language // comments. But if # is at the beginning of the first line and is #! (Shebang) is an exception. it indicates that the script uses the specified interpreter/bin/sh to explain and execute $ chmod + xscript. sh $... linux FAQ 1: # is used in Shell scripts to express comments, which is equivalent to the C language // comments. But if # is at the beginning of the first line and is #! (Shebang) is an exception. it indicates that the script uses the specified interpreter/bin/sh to explain and execute $ chmod + x script. sh $. /script. two methods for executing Shell scripts: $. /script. sh $ sh. /script. sh3. multiple commands separated by semicolons (;) can be entered in one line. $ cd ..; ls-l4 exists only in the current Shell process. you can use the set command to display all variables defined in the current Shell process (including local variables and environment variables) and function environment variables are all concepts of any process, while local variables are unique concepts of Shell. In Shell, the definitions and usage of environment variables and local variables are similar. Define or assign a variable in Shell: $ VARNAME = value. Note that no space is allowed on both sides of the equal sign. otherwise, Shell will interpret the variable as a command or a command line parameter. A variable is defined only in the current Shell process. it is a local variable. you can use the export command to export local variables as environment variables. you can define and export environment variables in one step: $ export VARNAME = value can also be completed in two steps: $ VARNAME = value $ export VARNAME you can use the unset command to delete defined environment variables or local variables. $ Unset VARNAME: if a variable is named VARNAME, $ {VARNAME} can be used to represent its value. without ambiguity, $ VARNAME can also be used to represent its value. The following example compares the differences between the two representations: $ echo $ SHELL $ echo $ SHELLabc $ echo $ SHELL abc $ echo $ {SHELL} abc. Note that you do not need to use $ When defining variables, $ is used to get the variable value. Different from the C language, Shell variables do not need to clearly define the type. In fact, the Shell variable value is a string. for example, we define VAR = 45. In fact, the VAR value is a string 45 rather than an integer. Shell variables do not need to be defined first and then used. If no value is defined for a variable, the value is a null string. 5. single quotes and double quotation marks in Shell scripts are the same as character string delimiters (next section of double quotation marks), rather than character delimiters. Single quotation marks are used to keep the nominal values of all characters in the quotation marks. even if the \ and carriage return characters in the quotation marks are no exception, the single quotation marks cannot appear in the string. If the quotation marks are not paired, enter the carriage return. Shell will give a prompt to continue the line, asking the user to enclose the quotation marks with a pair. For example: $ echo '$ shell' $ SHELL $ echo 'ABC \ (press enter)> De' (press enter again to end the command) ABC \ DE6, command replacement: 'or $ () is enclosed by backquotes. Shell first executes the command and immediately replaces the output result with the current command line. For example, to define a variable to store the output of the date command: $ DATE = 'Date' $ echo $ date Command replacement can also be expressed as $ (): $ DATE = $ (DATE) 7. double quotation marks are used to keep the nominal values of all characters in the quotation marks (carriage return is no exception), except in the following cases: $ the variable name can be used to get the value of the variable. the reverse quotation marks still indicate command replacement. \ $ indicates $ '\' indicates \ in addition to the above situations, \ before other characters has no special meaning, only the nominal value $ echo "$ SHELL"/bin/bash $ echo "'date'" Sun Apr 20 11:22:06 CEST 2003 $ echo "I 'd say: \ "Go for it \" "I 'd say:" Go for it "$ echo" \ "(Press enter)>" (Press enter again to end the command) "$ echo" \ "\ 8. when bash is started, the following script is automatically executed: Run/etc/profi first. Le. this script is executed when every user logs on to the system. if the system administrator wants a setting to take effect for all users, write it in this script and search for the main directory of the current user in sequence ~ /. Bash_profile ,~ /. Bash_login and ~ /. Profile, find the first file that exists and is readable for execution. if you want a setting to only take effect for the current user, you can write it in this script, since this script is executed after/etc/profile, the values of some environment variables set by/etc/profile can be modified in this script, that is, the current user's settings can be overwritten) global Settings in the system. ~ /. Profile the startup script is defined by sh, bash requires that you first find ~ The start script starting with/. bash _. if not, execute ~ /. Profile to be consistent with sh. By the way, it will be executed when you log out ~ /. Bash_logout script (if it exists ). 9. The test Command test or [can test whether a condition is true. if the test result is true, the Exit Status of the command is 0. if the test result is false, the Exit Status of the command is 1 (note that the logical representation of the C language is exactly the opposite ). For example, test the relationship between two numbers: $ VAR = 2 $ test $ VAR-gt 1 $ echo $? 0 $ test $ VAR-gt 3 $ echo $? 1 $ [$ VAR-gt 3] $ echo $? 1 although it looks strange, the left square brackets [it is indeed the name of a command. the parameters passed to the command should be separated by spaces, for example, $ VAR,-gt, 3, and] are the four parameters of the [command, which must be separated by spaces. The test or [parameters are the same, but the test command does not need] parameters. Take the [command as an example. The following table lists common test commands: [-d DIR] if DIR exists and is a directory, it is true. [-f FILE] if FILE exists and is a common FILE, it is true. [-z STRING] if STRING length is zero true [-n STRING] if the STRING length is not zero, it is true [STRING1 = STRING2] if the two strings are the same, it is true [STRING1! = STRING2] if the strings are different, the true [ARG1 OP ARG2] ARG1 and ARG2 should be integers or variables with an integer value, and the OP is-eq (equal to)-ne (not equal) -lt (less than)-le (less than or equal to)-gt (greater than)-ge (greater than or equal to) is similar to c, test conditions can also be performed between and, or, non-logical operations: With AND, OR, non-Test commands [! EXPR] EXPR can be any of the test conditions in the preceding table ,! Indicates that the logical inversion [EXPR1-a EXPR2] EXPR1 and EXPR2 can be any of the test conditions in the preceding table, -a indicates the logic and [EXPR1-o EXPR2] EXPR1 and EXPR2 can be any of the test conditions in the preceding table.-o indicates the logic or example: $ VAR = abc $ [-d Desktop-a $ VAR = 'abc'] $ echo $? 0 Note: If the $ VAR variable in the previous example is not defined in advance, it is expanded as an empty string by Shell, it may cause syntax errors for test conditions (expanded to [-d Desktop-a = 'abc']). as a good Shell programming habit, always put the variable value in double quotation marks (expanded to [-d Desktop-a "= 'abc']): $ unset VAR $ [-d Desktop-a $ VAR = 'abc'] bash :[: too Upload arguments $ [-d Desktop-a "$ VAR" = 'abc'] $ echo $? 110 The case/esaccase command is similar to the switch/case statement in C language. esac indicates the end of the case statement block. The case in C language can only match the constant expressions of integer or numeric type, while the case in Shell script can match the string and Wildcard. each matching branch can have several commands and must end, find the first matched branch during execution and execute the corresponding command. then, jump directly to esac without using break like the C language. #! /Bin/sh echo "Is it morning? Please answer yes or no. "read YES_OR_NOcase" $ YES_OR_NO "inyes | y | Yes | YES) echo" Good Morning! "; [NN] *) echo" Good Afternoon! "; *) Echo" Sorry, $ YES_OR_NO not recognized. enter yes or no. "exit 1; esacexit 0 example of using case statement can be in the script Directory of the system service/etc/init. d. Most scripts in this directory have this form (take/etc/apache2 as an example): case $1 instart )...;; stop )...;; reload | force-reload )...;; restart )... *) log_success_msg "Usage:/etc/init. d/apache2 {start | stop | restart | reload | force-reload | start-htcacheclean | stop-htcacheclean} "exit 1 ;; the esac command to start apache2 is $ sudo/etc/init. d/apache2 start $1 is a special variable. it is automatically set to the first command line parameter (that is, start) when the script is executed. Similarly, if the command line parameter is set to stop, reload, or restart, you can go to another branch to run commands related to stopping the service, reloading the configuration file, or restarting the service. 11. if fi is similar to the C language. in Shell, use commands such as if, then, elif, else, and fi to implement branch control. This process control statement is essentially composed of several Shell commands, such as the if [-f ~ /. Bashrc]; then .~ /. Bashrcfi is actually three commands, if [-f ~ /. Bashrc] is the first, then .~ /. Bashrc is the second and fi is the third. If the two commands are written in the same line, they must be separated by a; number. if only one command is written in one line, no; number is required. In addition, there is a line break after then, but this command has not been written, shell will automatically continue the line and process the next line following then as a command. Like [commands, note that commands and parameters must be separated by spaces. The parameters of the if command form a sub-command. if the Exit Status of the sub-command is 0 (true), run the sub-command after then, if Exit Status is not 0 (false), run the subcommand after elif, else, or fi. The sub-commands after if are usually test commands, but can also be other commands. The Shell script does not have {} brackets, so fi is used to indicate the end of the if statement block. See the following example :#! /Bin/sh if [-f/bin/bash] then echo "/bin/bash is a file" else echo "/bin/bash is NOT a file" fiif :; then echo "always true"; fi: a special command called an empty command. this command does not do anything, but the Exit Status is always true. In addition, you can run/bin/true OR/bin/false to obtain the true or false Exit Status. Let's look at another example :#! /Bin/sh echo "Is it morning? Please answer yes or no. "read YES_OR_NOif [" $ YES_OR_NO "=" yes "]; then echo" Good morning! "Elif [" $ YES_OR_NO "=" no "]; then echo" Good afternoon! "Else echo" Sorry, $ YES_OR_NO not recognized. enter yes or no. "exit 1 fiexit 0 the read command in the above example is used to wait for the user to enter a string and store it in a Shell variable. In addition, Shell also provides the & | syntax, which is similar to the C language and has the Short-circuit feature. many Shell scripts like to write like this: test "$ (whoami )"! = 'Root' & (echo you are using a non-privileged account; exit 1) & equivalent to "if... then... ", and | equivalent to" if not... then... ". & | Used to connect two commands. the-a and-o mentioned above are only used to connect two test conditions in the Test expression. pay attention to their differences. for example, test "$ VAR"-gt 1-a "$ VAR"-lt 3 is equivalent to test "$ VAR"-gt 1 & test "$ VAR"-lt 3
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.