Puff---------Linux Bash Scripting---if supplement and for loop

Source: Internet
Author: User
Tags stdin

BASH: Procedural programming, in order to accomplish more complex tasks, support sequential execution, select execution, loop execution

Sequential execution: From left to right, execute command sequentially.

Select Execute: Select to execute a different code fragment based on the execution status result of the condition (condition).

Loop execution: Determines whether to enter the loop according to the execution status result of the condition (condition).

Condition

Ture: Indicates a condition status result of 0

False: Indicates conditional execution status result not 0


If

Single-branch, dual-branch, multi-branch, nested IF statements

1, single branch structure if condition; Then If-turefi2, double branching structure if condition; Then If-tureelse if-falsefi3, multi-branch structure if condition; then if-tureelif condition; then if-tureelif condition; Then if-ture ... else all-falsefi4, nested if statement if condition; Then if condition; Then if-ture fifiif condition; Then if condition; Then if-ture fielse if condition; Then If-ture fi fi


For


List loops

Format

1, format a for variable name in the list; Do loop body done2, format two for variable name in list do loop Hugh Done3, format three, command line for variable name in list; Do loop body; Done

List representation methods

1, give out table 2, {first. Mantissa}, for example 1 to 100 for "{1..100}" 3, Command Reference: 1) $ (ls DIR) 2) $ (expr [step]] mantissa), for example 1 to 100 is represented as "$ (1 1 100)" 4, wildcard, Glob. For example, the absolute path of all files and directories in the sub-directory under the/var directory. /VAR/*5, variable reference. $* all parameters passed to the script, as a whole. [email protected] All parameters passed to the script, each independent


Script format

#!/bin/bash# version:major.minor.release# author:# Desc:


Script Syntax detection

# bash-n File.sh


Debug scripts

# bash-x File.sh


Log script exit status code

Inteval=$? (Variable reference implementation Assignment)


User Mailbox Location

/var/mail


Define the type of the variable

Integral type: # declare-i var

Defining environment variables

# env var# declare-x var# export var

Defining read-only variables

# declare-r var# readonly var

Show local and environment variables

# set

Display environment variables

# export# env# printenv

Show read-only variables

# readonly-p


Example one: interactively give a file path to determine the type of file

#!/bin/bash# version:0.0.1# author:lcc.org# description:testingread-t 5-p ' Enter a file path: ' Filenameif [-Z ' $file Name "]; Thenecho "Enter a file path" Exit 1fiif [!-e $filename]; Thenecho "No such file." Exit 2fiif [-f $filename]; Thenecho "Common file." elif [-H $filename]; Thenecho "symbolic file." elif [-D $filename]; Thenecho "Directory." else echo "other type." Fi


Example two: Add 10 users, User1,,.... user10, password with the user name (only root can change the password).

* * Only root can change password * *

1, list, give the #!/bin/bash# version: 0.0.2# author: lcc.org# description: add  directly user# #避免执行命令的用户非root用户, a non-root user cannot change the password. if [  $UID  -ne 0 ]; then  echo  "Only root."   exit 1fi# #以给出列表的方式, cycle. When the list cycle is complete, the loop ends For i in user1 user2 user3 user4 user5 user6 user7  user8 user9 user10; do  ##  determine if the user exists.   if id  $i  &> /dev/null; then    ##  What does it mean when the condition is true when the execution status result is 0 o'clock?     echo  "$i  exist"   else         ##  Add user if ! useradd  $i  2> /dev/null; then  ## when the user does not exist   Add unsuccessful when,  in combination, reverse the command or test condition.   echo  "$i  is outside the law" else  ##  user name, Normal, can add the user normally, You can add a password to it          &Nbsp;echo  "$i"  | passwd --stdin  $i  > /dev/null 2>&1           ##  numeric test adds the execution status result of the password.   if [ $? -ne 0 ]; then    ##  results are not 0, indicating unsuccessful execution     echo  "Password is not legal"   fi     Fi  fidone2, {1..10}#!/bin/bash# version: 0.0.3# author: lcc.org#  description: {} represents the list [ !  $UID  -eq 0 ] && echo  "only  Root. "   && exit 1for i in {1..10}do  id user$i & > /dev/null  if [ $? -eq 0 ]; then    echo   "User$i exist"   else    useradd user$i 2> /dev/null [ $? -ne 0 ] && echo  "Username is not legal"  && continueecho  "User${i}"  |  passwd --stdin user${i} &> /dev/null[ $? -ne 0 ] & & echo  "Password is not legal" echo  "add user user$i finished"   fidone3, command Reference #!/bin/bash# version: 0.0.4# author: lcc.org# description:   $ (SEQ 10) represents the list [ !  $UID  -eq 0 ] && echo  "only  root. "   && exit 1for i in $ (seq 10) do  id user$i  &> /dev/null  if [ $? -eq 0 ]; then     echo  "User$i exist"   else    useradd user$i 2 > /dev/null[ $? -ne 0 ] && echo  "Username is not  legal " && continueecho  "User${i}"  | passwd --stdin user${i} &> / dev/null[ $? -ne 0 ] && echo  "Password is not legal" echo  "add user user$i finished"   fidone4, special variable #!/bin/bash# version:  0.0.5# author: lcc.org# description: $*,[email protected] represents the list [ !  $UID   -eq 0 ] && echo  "Only root."   && exit 1for i in $*do  id  $i  &>  /dev/null  if [ $? -eq 0 ]; then    echo  "$i  exist "  else    useradd  $i  2> /dev/null[ $? - ne 0 ] && echo  "Username is not legal"  &&  continueecho  "${i}"  | passwd --stdin ${i} &> /dev/null[ $? -ne 0 ] && echo  "Password  is not legal "echo " add user  $i  finished "  fidone


Example three: Determine the type of each file in the/var/directory

Method One #!/bin/bash# version: 0.0.6# author: lcc.org# description: file typefor  i in /var/*; do  if [ -f  $i  ]; then  echo   "Common file."   elif [ -L  $i  ]; then  echo  "Symbolic file."   elif [ -d  $i  ]; then  echo  "Directory."   else    echo  "Other type"   fidone method two:#!/bin/bash#  Version: 0.0.7# author: lcc.org# description: galaxy cd /varfor i  in $ (Ls /var); do  if [ -f  $i  ]; then     echo  "Common file."   elif [ -L  $i  ]; then    echo  "Symbolic file ."   elif [ -d  $i  ]; then    echo  "DirectOry. "   else    echo  "Other type"   fidone method three:#!/bin/bash#  version: 0.0.8# author: lcc.org# description: add dirfor i in $ ( Ls /var)  doif [ -f /var/$i  ]; thenecho  "Common file" elif [  -l /var/$i  ]; thenecho  "Symbolic file" elif [ -d /var/$i  ];  thenecho  "Directory" elseecho  "Other type" Fidone


Example four: How many listen are in the establish State under the TCP protocol, and how many have several states

Method One: #!/bin/bash# version: 0.0.9# author: lcc.org# description: statustcpdeclare  -i listen=0declare -i established=0declare -i other=0for i in $ ( netstat -tan | grep  ' ^tcp\> '  | tr -s  '   '  | cut  -d '   '  -f6); doif [  "$i"  ==  "LISTEN"  ]; thenlet listen++elif  [  "$i"  ==  "established"  ]; thenlet established++elselet other++ fidoneecho -e  "listen statu:  $listen \nestablished statu:  $established \nother  status:  $other \ntotal type: $ (netstat -tan | grep  ' ^tcp\> '  |  tr -s  '   '  | cut -d '   '  -f6 | sort -u |  Wc -l) "Method two:# netstat -tan | grep  ' ^tcp\> '  | awk -v fs= '   '   ' {array[$NF]++}end{for (I in array) {Print i,array[i]}} ' listen 12close_wait 1established 3 


This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1955978

Puff---------Linux Bash Scripting---if supplement and for loop

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.