Shell – interactive input

Interactive input and for statement (8-15)

Interactive input

1, read Usage: The read command can define multiple variable values ​​at the same time, and the input content can be used as the allocator by default to enter the value into the corresponding variable; if the default value is too much, all the values ​​will be assigned to the last variable< /p>

2. If the default is too few, the extra variables will be empty.

3. Example read abc aa bb cc

4. How to enter Read: echo “Please enter a directory” (we need echo- nCancel line breaks)

Echo “Please enter a directory:” read variable (please enter a directory/etc)

5, Common parameters: -p Example: read -p “Please enter a directory” variable

-t: Define the timeout time Example: read -t 5 -p “Please enter a directory:” Variables

6, script default values:

share pictures

Exercise:

Enter a device file , Output the basic information of this device file.

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/ bash
#Input a device file, and output the basic information of the device file.
read -t 5 -p "Please enter a device file name:" devname code>
[ -z $devname] && devname=` fdisk -l`
if < code class="bash plain">[ -b /dev/ $devname ]; then
fdisk -l /dev/ $devname
exit 0
else
echo "$devname is not a device file"
echo "Usage:'Please enter a device file, such as sda'"
fi

Execution result:

share picture

7. Syntax format of case statement:

case variable in

PATTEN1)

  Code block 1

  ;;

PATTEN2)

  Code Block 2

  ;;

*)

  Code Library

esac

For statement loop

1. Application scenario: There is a function that needs to be executed in a loop, but the loop object is different, but The loop object is the same kind of data

2, for statement format:

for variable in list; do

   loop body

Done

3, echo command parameters: -e: enable the echo command to recognize special characters

          
: carriage return without line feed

          
: New line and the cursor moved to the beginning of the line

                                  output >

4. How the list is generated: 1. Give a list of characters directly (for i in abcdef;do)

          2. List of integers a{start....end}

p>

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

(seq most common features is the step size)

3 , The command to return to the list (example: $(command))   

          4. glob mechanism-the mechanism of file name wildcards

         : variable application ---- [email] protected] $*Use parameters as a list of for loops

            $0 is different from awk. The $0 in bash represents the script file to run

The $0 in Awk represents the entire line of content-awk is executed by line

             Use "[email protected]" when multiple words are needed, and "$" when one word is needed

5. Common usage of vim: In command mode, dd means cut, yy means copy, and p means paste

6. Summary of test commands: 1. The parameters of the test command -a, -o only Used in commands; Note: Regular expressions and wildcards are not supported when doing character matching.

         2. test test==[[]], when connecting two test commands, you cannot use the command inside The parameters -a, -o are generally used || && == != >= <=

1
2
3
4
5
6
7
8

9
10
11
12
#!/bin/bash
#Input a device file, output this device Basic information of the file.
read -t 5 -p "Please enter a device file name:" devname code>
[ -z $devname] && devname=` fdisk -l`
if < code class="bash plain">[ -b /dev/ $devname ]; then
fdisk -l /dev/ $devname
exit 0 code>
else
echo "$devname is not a device file"
echo "Usage:'Please enter A device file, such as sda'"
fi

1
2
3
4
5
6
7
8
9 < /div>

10
11
12
#!/bin/bash
#Input a device file, output the device file Basic Information.
read -t 5 -p "Please enter a device file name:" devname code>
[ -z $devname] && devname=` fdisk -l`
if < code class="bash plain">[ -b /dev/ $devname ]; then
fdisk -l /dev/ $devname
exit 0
else
/code> echo "$devname is not a device file"
echo "Usage:'Please enter a device file, such as sda '"
fi

1

2

3

4

5

6

7

8

9

10

11

12

#!/bin/bash
#Input a device file, output this device file Basic information.
read -t 5 -p "Please enter a device file name:" devname code>
[ -z $devname] && devname=` fdisk -l`
if < code class="bash plain">[ -b /dev/ $devname ]; then
fdisk -l /dev/ $devname
exit 0

< div class="line number9 index8 alt2"> else

else
code> echo "$devname is not a device file"
echo "Usage:'Please enter a device file, such as sda' "
fi

#!/bin/bash

#Input a device file, and output the basic information of the device file.

read -t 5 -p "Please enter a device file name:" devname

[ -z $devname] && devname=` fdisk -l`

if [ -b /dev/ < code class="bash plain">$devname ]; then

fdisk -l /dev/ $devname

exit 0

else

< code class="bash functions">echo "$devname is not a device file"

echo "Usage:'Please enter a device file, such as sda'"

fi

Leave a Comment

Your email address will not be published.