SHLL Foundation, the original script is not difficult!

Source: Internet
Author: User
Tags case statement

------A small calculator-----

#!/bin/bash
#this

Read-p "Please enter the first integer:" One
Read-p "Please enter the second integer:" Two
Read-p "Please enter operator:" TH
echo "$ONE $th$two=$ (($ONE $TH $TWO)"

---------an If single branch statement-----

#!/bin/bash
#this

Read-p "Input directory:" DIR
if [!-D $DIR]
Then Mkdir-p $DIR
Fi

echo "Done"

---------an If double-spoke statement---

#!/bin/bash
#this

PING-C1-I0.1-W1 $

If [$?-eq 0]
Then echo "Are up!"
else echo "is down!"
Fi

=================if Nesting ================
#! /bin/bash
#
Read-p "Please enter your score:" A
If [$A-le 10]
Then
echo "Congratulations to the finals"
Read-p "Please enter gender (boy/girl):" Sex
if [$SEX = boy]
Then
echo "Into the men's group"
Else
echo "Into the women's group"
Fi
Else
Echo, "I'm sorry you were eliminated."
Fi

-------The following is the case statement----------

#!/bin/bash
#this
Export LC_ALL=C//Case Sensitive

Read-p "Please enter the character:" KEY

Case $KEY in

    [A-Z] )            echo "您输入的$KEY是大写字母。"    ;;    [a-z] )            echo "您输入的$KEY是小写字母。"    ;;    [0-9] )            echo "您输入的$KEY是数字。"    ;;    ,)            echo "您输入的是$KEY。"    ;;    "#")            echo "您输入的是$KEY。"    ;;    *)            echo "您输入的是其它字符。"    ;;

Esac

-----------------The following is a function------------------
#!/bin/bash
Sum () {
Read-p "Please enter the first integer:" NUM1
Read-p "Please enter a second integer:" NUM2
echo "The two numbers you entered are: $NUM 1 and $num2. "
sum=$ (($NUM 1+ $NUM 2))
echo "Two number of the and is: $SUM"
#return $ (($NUM 1+ $NUM 2))//using the return value method, the maximum can not be greater than 255
}
Sum
#echo "Two number of the and is: $?"

---------The following is a for loop----------------

For IP in 192.168.80. {5..183}
Do
PING-C1-I0.2-W1 $IP &>/dev/null
If [$?-eq 0]
Then echo "$IP is up!"
else echo "$IP is down!"
Fi
Done

----------The following is an example------------

#!/bin/bash
#

For USER in test{1..10}
Do
Useradd $USER
echo "${USER}ABC" | passwd--stdin $USER &>/dev/null
Done

cat/etc/passwd

-----------Here is the while example-----------

#!/bin/bash
#

I=1
Pre=test

While [$i-le 10]
Do
Useradd $PRE $i
echo "123" | passwd--stdin $PRE $i &>/dev/null
Let i++
Done
Echo $i
cat/etc/passwd

===================until Loop Statement =====================
# #条件不满足循环, stop when you are satisfied # #
Calculates the value of a 1~50
[Email protected] ~]# VI sum1to50_until_v1.sh
#!/bin/bash
# #filename: sum1to50_until_v1.sh
I=0;s=0
Until [$i-eq 50]
Do
Let "i= $i +1", let "s= $s + $i"
Done
Echo ' sum (1..50) = ' $s

=========================== Double Cycle ==================
#!/bin/bash
#
H=1
While [$H-le 10]
Do
L=1
While [$L-le $H]
Do
Echo-n "$L"
Let l++
Done
echo ""
Let h++

Done
Final output:
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910

===================shell Array ========================

Shell Array
1. Get the array length
[Email protected] ~]# arr_number= (1 2 3 4 5);
[Email protected] ~]# arr_length=${#arr_number [*]}
[Email protected] ~]# echo $arr _length
5

2. Reading a subscript assignment
[[email protected] ~]# arr_index2=${arr_number[2]}
[Email protected] ~]# echo $arr _index2
3

3. Array traversal
[[email protected] ~]# for V in ${arr_number[@]}

Do
Echo $v
Done
1
2
3
4
5

SHLL Foundation, the original script is not difficult!

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.