Bash programming and Linuxbash programming in Linux

Source: Internet
Author: User

Bash programming and Linuxbash programming in Linux

Bash programming is also called shell programming.

  

Predefined Variables
$? The return status of the last executed command. If the value of this variable is 0, it indicates that the previous command is correctly executed. If the value returned by this variable is not 0 (the specific number is determined by the command ), the execution of the previous command is incorrect.
$ Process ID of the current process (PID)
$! PID of the last process running in the background)
Operator
Type of the declare Parameter
Declare [+/-] [Option] variable name
-Set the type attribute for the variable
+ Cancel the type attribute of the Variable
-I: Name the variable as an integer.
-X declares the variable as an environment variable.
-P: shows the known type of the specified variable.

Example:

A = 1b = 2 declare-I c = $ a + $ bc = $ ($ a + $ B ))

Note that double parentheses are used, because everything is considered as a character in Linux.

If you do not add () as a command, the Linux Command in the shell script runs in $ (). Of course, you can directly write the command. If you reference some commands, you need $ ()


If condition statement for a single branch
If [conditional statement]; then program fior if [conditional statement] then program fi
Determine whether the logon user is a root user:
#! /Bin/bash # grep get the row wildcard # cut get the column wildcard, use = to split 2 to get the second column login_name =$ (env | grep LOGNAME | cut-d "="-f 2) # echo $ login_nameif ["$ login_name "! = "Root"] then echo 'isnot root' fi
Determine the usage of the root Partition
#! /Bin/bash # df-h show disk utilization # awk get the Fifth Column # cut split the character with % to get the first column value result = $ (df-h | grep sda1 | awk '{print $5}' | cut-d %-f 1) if ["$ result"-lt '90'] then echo 'the root dir is not full' fi
Dual-branch if Condition Statement
If [conditional filter] then... else... fi
Determine whether nginx is running (it is best not to include nginx as the file name)
#!/bin/bashresult=$(ps aux | grep nginx | grep -v grep)if [ -n "$result" ]        then                echo "$(date) nginx is ok !"        else                echo "$(date) nginx is not ok !"                sudo /etc/init.d/nginx start &>/dev/null                echo "$(date) restart nginx !!"fi
Multi-branch if statement
if []    then elif []    then...else    ...if

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.