shell_for notes

1. interactive input

interactive interface [read]

  :read a b c

   aa bb cc

  The read command may define a plurality of variables simultaneously; and the contents of the input space as a separator in default, the value input to the corresponding variable; If the default value is excessive, the final value will be all assigned to the last variable; if the default too little, extra variable or a null

  How to read input:

        echo "Please enter a directory:" // we need to cancel echo -n number newline

        Example: echo "Please enter a directory:"; read variables

          Please enter a directory: / etc

Common parameters:

      -p read -p "Enter a directory:" variable

      -t timeout defined read -t 5 -p "Please enter a directory:" variable

 

The default value script

   Typing -------------------------------

   |                                                    |

   Input is not entered correctly ----- ------------

    |                 |                               |                         |

  Correct Incorrect, input must be given to Tip defaults

  [ ! -n $filename ]

Exercise: Enter basic information about a device file, the output of this device file

. 1 ! # / Bin / the bash
 2  
. 3  #
 . 4  
. 5 Read -t . 5 - P "Enter a device file name:" devname
 . 6  
. 7 [$ devname the -Z] = `devname && && Exit the fdisk -l` . 1 
. 8  
. 9   
10  
. 11  iF [-b / dev / $ devname]; the then    # - B whether the block file
 12 is  
13 is     the fdisk -l / dev / $ devname 
 14  
15     Exit 0 
16  
. 17  the else 
18 is  
. 19     echo "$ devname device file is not"
 20 is  
21 is     echo " Usage: 'Please enter a device file, such as: sda' "
22 
23    exit 2
24 
25 fi

2. for loop

case statement

   The syntax of the case statement:

  Variables in case

  patten1)

    Block 1

    ;;

  patten2)

          Block 2

          ;;

       *)

      esac

for scenarios statement:

   There is a function, this function needs to execute the cycle, but different execution object; the object must be the same [Data]

 

format for statement:

  a = [ 'a', 'b', 'c'] - the python program, in many cases we need to call the appropriate methods to generate a list;

 

  for variable in list; do

    Loop

  done

Exercise: 99 multiplication table

 1 #!/bin/bash
 2 
 3 for I in`seq 9`;do
 4 
 5    for j in `seq 9`;do
 6 
 7       [$j -le $i] && echo -n -e “$i*$j=`echo $(($i*$j))`\t”
 8 
 9    done
10 
11    echo “ ”
12 
13 done
. 1 ! # / Bin / the bash
 2  
. 3  # multiplication table
 . 4  
. 5  for I in { . 1 .. . 9 }; do 
. 6  
. 7     for J in $ I; do 
. 8  
. 9        echo -n -e "* $ I $ J = $ [$ I * $ J] \ T "
 10  
. 11     DONE  
12 is  
13 is     echo 
14  
15  DONE

echo -e

  -e make echo command, a special symbol can be identified

   \ R carriage return line feed is not

   \ N newline

   \ T tab spaces tab = 4

 

  unix2dos \ r \ n \ n

  dos2unix

List Builder way:

1. Direct given string list

  for i in a b c d e;do

     Loop

  done

2. The list of integers

  a {start ... end} //{1..1} == python is not acceptable in the range {1..9}

  b seq command reference $ (seq [start [step]] end)

            Step python range {1,100,2}

            seq 20 30

 

Title: implemented for this loop through the host network segment about up

 1 declare -I sum=0
 2 
 3 for i in $(seq 1 254);do
 4 
 5    ping -c 1 -w 1 10.6.12.$i &> /dev/null
 6 
 7    if [ $? -eq 0 ];then
 8 
 9  let sum++
10 
11  echo10.6.12.$i是通的”
12 
13         else
14 
15              echo " 10.6 . 12 is . $ I is illogical"
 16  
. 17           Fi 
18 is  
. 19  DONE 
20 is  
21 is  echo "$ sum total of online computer"

3. Return the command list

  $(COMMAND)

  Scenarios: ls / var / log /

Title: Analyzing all files / var / log / a. A file ending in .log statistical processing and display:

 

. 1   ! # / Bin / the bash
 2  
. 3     #
 . 4  
. 5     DECLARE -i SUM = 0 
. 6  
. 7     for I in $ ( LS / var / log /); do 
. 8  
. 9  IF [ "* .log" = $ I]; the then 
10  
. 11           echo $ I
 12 is  
13 is           the let SUM ++
 14  
15  Fi 
16  
. 17          DONE 
18 is  
. 19          echo "ending with a file .log: $ SUM "

4. glob mechanisms - mechanisms file name wildcards

  $ (Ls / var / log /)

  / Var / log / *

test command summary:

The option -a -o 1.test commands can only be used in the command

  例如【`du -h $filename|cut -d: -f1` -lt 100k -a -x $filename】

  test `du -h $filename |cut -d: -f1` -lt 100k -a -x $filename

  Note: when doing string matching, and does not support regular wildcard

2.test test==[[]]

  And then connect the two test commands, you can not use the internal command option -o -a

  General use || && ==! => = <=

  For example: [. [Yum.log == * log]]

5. variable references

  $ @ $ * As the parameter list for the cycle

  $ 0 - and not the same as awk

   $ 0 for the bash script file to run

   [Executive] awk awk row in the entire line $ 0

  Need more time to use the word "$ @" requires a word when using "$ *"

Guess you like

Origin www.cnblogs.com/TheNeverLemon/p/11359390.html