Script Editor $random and Case statements (notes) to be continued

Source: Internet
Author: User

$RANDOM

A personal understanding is a variable used to generate a random number, thus writing a script

[email protected] script]# cat random.sh

#!/bin/bash

#

Declare-i max=0

Declare-i min=0


For I in ' seq 1 10 '; Do

Myrandom= $RANDOM

[$I-eq 1] && min= $RANDOM

If [$I-le 9]; Then

Echo-n "$MYRANDOM,"

Else

echo "$MYRANDOM"

Fi

[$MYRANDOM-gt $MAX] && max= $MYRANDOM

[$MYRANDOM-lt $MIN] && min= $MYRANDOM

Done


Echo $MAX, $MIN

[Email protected] script]#

This script is used to compare the number of random numbers generated to a maximum and minimum number and display

Script Execution Results:

[Email protected] script]# bash random.sh

20744,24195,5854,24168,10197,16685,10095,27664,14198,22593

27664, 5854

[Email protected] script]#


Programming is divided into two categories

Process oriented

Control structure

Sequential structure

Select structure

Loop structure

Object oriented


Select structure

If statement: Single branch, dual branch, multi-branch

Now a new choice structure case

Use formatting

Case $ in

value1)

Statement

...

;; (Note that you must add a double semicolon)

value2)

Statement

...

;;

VALUE3)

Statement

...

;;

Esac


Here's a simple script when the user follows a parameter to identify whether it's a number or an uppercase or lowercase letter, or a special character

[email protected] script]# cat recognition.sh

#!/bin/bash

#

Case $ in

[0-9])

echo "a number";;

[[: Upper:]])

echo "Upper";;

[[: Lower:]])

echo "a lower case";;

*)

echo "special character";;

Esac

[Email protected] script]#


Execution Result:

[Email protected] script]#/recognition.sh 2

A number

[Email protected] script]#./recognition.sh E

Upper

[Email protected] script]#./recognition.sh E

A lower case

[[email protected] script]#./recognition.sh #

Special character

[Email protected] script]#


Of course I this only support single-character, later study advanced again practice, here only do a brief introduction!! Oo


Not to be continued

Script Editor $random and Case statements (notes) to be continued

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.