shell programming flow control statements --if-- conditional expression syntax rules

A. With expr

Format expr m + n or $ ((m + n)), the operator must pay attention to the space expr

Such as (3 + 2) * value of 4

1. calculation step

  S=‘expr 2 + 3’

  expr $ S \ * 4 ## * No need to escape

2. The calculation in one step

  expr 'expr 2 + 3' \ * 4

  echo 'expr \'expr 2 + 3\'\* 4'

II. With (()) // do not need space

((1+2))

(((2+3)*4))

count=1

((count++))

echo $ count result of the operation need to take references with $

a=$((1+2))

III. With $ []

a=$[1+2]

echo $ a

Four .if grammar

#!/bin/bash

read -p "please input your name:" NAME ## read commands for reading the input data from the console

## printf '%s\n' $NAME

if [ $NAME = root ]

  then

    echo "hello $(NAME), welcome!"

  elif [ $NAME = itcast ]

    then

      echo "hello $(NAME), welcome!"

  else

    echo "Get out here!"

be

V. judgment condition

1. The basic syntax conditional

  [Condition] (Note that condition must be a space before and after)

# Non-empty returns true, use $? Verification (0 is true,> 1 is true) [itcast]

# Null return false []

2. The composition determination condition

[Condition] && echo OK || echo notok condition is detected, the statement following &&; condition is not satisfied, the implementation of the latter statement ||

Note: [] difference between [[]] of: [[]] may be used in combination && || logic symbol, [] which can be used in combination logic -a -o

3. Analyzing common operator

String comparisons: = = -z -n!

-z returns true length of the string 0

-n string length is not zero return true

Integer Comparison: -lt -le less than equal to or less -eq -gt greater than -ne not equal to or greater -ge

It's the file: -d whether the directory

     Whether -f file

     -e whether there

Guess you like

Origin www.cnblogs.com/chengting/p/11531164.html