"Linux Learning 009" script programming variables and conditions to judge

Source: Internet
Author: User

One, bash environment variables

There are four types of variables in bash: Environment variables, local variables (local variables), positional variables, special variables.

1. Environment Variables

environment variables are the most extensive, and all child bash processes have access to the values in the environment variables, and the way to define environment variables is to use the Export keyword. Example:

[Email protected]:~$ export A=abc[email protected]:~$ echo $Aabc [email protected]:~$

2. Local Variables

The local variable is scoped to the current bash, and the child bash does not access the variable defined in the parent bash, and the method of defining the local variable is varname=value. Example:

[Email protected]:~$ b=cde[email protected]:~$ echo $Bcde [email protected]:~$

A local variable is a special form of a local variable with a smaller scope, which can only be used in a method, using the keyword local definition.

3. Position Variables

positional Variables act like latter references in regular expressions, which are referenced using "\ n" in regular expressions, which are referenced using "$n".

4. Special Variables

The special variable leaf is called "system variable", and this category of variables is bash's own defined variables, such as $? is a special variable (system variable), which is used to save the execution result of the previous command, 0 for the execution succeeds, the other (1-255) represents the execution failure

[Email protected]:~$ ls/tmpconfig-err-yxmeyjfcitx-socket-:0orbit-kdyzmsni-qt_sogou-qimpanel_1597- mn3jk9sogou-qimpanel:0. Pidsogou-qimpanel-cellsogou-qimpanelkdyzmsystemd-private-251274e9dff14256aadaac748704e759-colord.service-q7u1posystemd-pr Ivate-251274e9dff14256aadaac748704e759-rtkit-daemon.service-i81zdwsystemd-private-251274e9dff14256aadaac748704e759-system D-timesyncd.service-srkrjyunity_support_test.0[email protected]:~$ echo $[email protected]:~$ ls/templs:cannot Access/temp:no such file or Directory[email protected]:~$ echo [email protected]:~$
Second, how to implement the variable reference

Use ${varname} to implement variable references, where {} can be omitted directly in most cases:

[Email protected]:~$ animal=pig[email protected]:~$ echo "There is some ${animal}s" there is some pigs[email protected]:~ $

The above echo command must use "" instead of ", because" "is a weak reference, the variable can be parsed, ' is a strong reference, and the variable cannot be parsed.

[Email protected]:~$ echo ' There is some ${animal}s ' there is some ${animal}s[email protected]:~$
Iii. Miscellaneous

1. If the script is executed at the command line, the script has direct access to the variables currently defined by the shell, but if the script is run on a non-command line, the environment variable must be manually configured.

2./dev/null is a data black hole that redirects data that does not need to be displayed here to be discarded.

3. Use the unset command to unbind the variable and free up memory space.

4. How to view the environment variables for the current shell

Use Printenv, env, and export three commands to view environment variables in the current bash.

5. How to run a script

Specify the path to the script and enter. It is important to note that if the script file is under the current path, you also need to specify the./as the flag of the current path, otherwise the direct use of the file name will prompt to find the command, because the command line by default, you enter the command, so go back to path path to find, the result is not found, will error.

[Email protected]:~$ command.shcommand.sh:command not found[email protected]:~$./command.sh hello,world!

Note After creating the script file, be sure to add the "executable" permission, because the text file created by default is not executable.

chmod a+x./command.sh

If you do not want to add executable permissions to the script file but also want to execute the script, directly use the

Bash FILE

can be executed.

Iv. How to write a script

You must specify "magic number" before writing a script, specifying the function of the magic number is to tell the system where to find the parser to parse the script content, in the case of bash, the method of specifying the magic number is:

#! /bin/bash

The next step is to stack up some of the commands, in fact scripting is so simple.

V. Condition testing

1. Condition test type includes integer test, character test, file test

2. Three kinds of commands for conditional testing

[Expression] [[Expression]]    #} "[[" is a keyword in bash and [not a test expression]

3. Operators for integer comparisons

-eq    #测试两个数是否相等-ne    #测试两个书是否不想等-lt    #小于比较测试-gt    #大于比较测试-le    #小于等于测试-ge    #大于等于测试

4. The logical relationship between commands

&&    #与关系 | |    #或关系
Vi. Conditions of Judgment

1. The structure of the conditional judgment is as follows:

If judging condition; then    statement1    statement2else    statement3    Statement4fi

2. Determine the return value of the command execution result or the command execution status return value

Command execution result return value: The return value of the command execution

Command execution status return value: Only two results, one is 0 means execution succeeds, non 0 means execution failed, use $? To view the change value.

Example: the judgment condition of the service determines the return value of the command execution state.

#! /bin/bashif ID user2 &>/dev/null;then        echo "user1 exist! "Else        echo" User1 does not exist! "Fi
Vii. Practice

1. Give a user name to determine whether the user is an administrator or a regular user

#! /bin/bashname=kdyzmuserid= ' id-u $NAME ' if [$USERID-eq 0]; then        echo "Admin user" Else        echo "Common user" fi

2. Write a script to determine if there is a user's default shell for bash, and if so, how many of these users are displayed, otherwise there is no such user.

#! /bin/bashusers= ' grep ' bash$ '/etc/passwd | Wc-l ' If [$USERS-eq 0]; then        echo "No default shell for bash user exists!" "Else        echo" uses bash as the default shell for the number of users: $USERS "fi




This article is from the "stolen A Plum blog" blog, please be sure to keep this source http://kdyzm.blog.51cto.com/8316029/1740334

"Linux Learning 009" script programming variables and conditions to judge

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.