Blog content is about the shell script number of the arrangement, more basic, mainly sharing logic, there are better ideas to share in the review of common progress.
The script implementation prompts the user to enter 3 integers sequentially, and the script sorts out 3 numbers according to the number size.
The result of the experiment is the output from large to small.
#!/bin/bash
Read-p "Please enter the integer 1:" A
Read-p "Please enter the integer 1:" b
Read-p "Please enter the integer 1:" C
If [$a-gt $b] && [$b-gt $c];then
echo "$a $b $c"
Fi
If [$a-lt $b] && [$b-lt $c];then
echo $c $b $a
Fi
If [$b-gt $a] && [$a-gt $c];then
echo $b $a $c
Fi
If [$b-gt $c] && [$c-gt $a];then
echo $b $c $a
Fi
If [$a-gt $b] && [$b-lt $c];then
echo $a $c $b
Fi
If [$c-gt $a] && [$a-gt $b];then
echo $c $a $b
Fi
If [$a-eq $b] && [$a-eq $c];then
echo "ABC equals"
Fi
In the first version, 7 times the corresponding judgment, the length is longer,
$$ is a logical judgement, and the meaning can be expressed in a (and), the current command succeeds when the second command is executed
|| is also a logical instruction, can represent two conditions or meaning, you can use the-O (or) to indicate that the current command fails, the following command will be executed.
Read-p is a command that can assign a value to a variable at the command line, with the prompt followed by a character
The script is to hope that you can more simple automation tools, the following improvements, random input three numbers, a,b,c, we will its output arrangement fixed to $a, $b, $c from large to small arrangement, when $a> $b, we will $ A, $b value to do the exchange, three times the corresponding operation can be sorted sequentially. Here's the code:
#!/bin/bash
Read-p "Please enter the integer 1:" A
Read-p "Please enter the integer 1:" b
Read-p "Please enter the integer 1:" C
If [$a-lt $b];then
Num= $a
A= $b
b= $num
Fi
If [$a-lt $c];then
Num= $a
A= $c
c= $num
Fi
If [$b-lt $c];then
num= $b
b= $c
c= $num
Fi
echo "Arrange numbers as $a, $b, $c"
The num only do the variable exchange of the transfer station, do the load, or can not call the variable repeatedly, of course, the script also has a small problem, for example, when one of the numbers is not input, the comparison of the null character and the number can not be judged, so the if judgment on whether the number is empty to determine, No more instructions here,
The arrangement of several random numbers of shell scripts (arithmetic basis)