Shell Learning (iii)

Source: Internet
Author: User

1. Position variable

Script:

[Email protected] sh]# cat/opt/sh/test1.sh
#/bin/bash
echo "The script name and path you executed are:" $
The first parameter of the echo "script is:" $
The second parameter of the echo "script is:"
The third parameter of the echo "script is:" $
The fourth parameter of the echo "script is:" $4
The fifth parameter of the echo "script is:" $
echo "Script has a number of parameters:" $#
echo "Script five parameters are:" [email protected]

Execution results

[[email protected] sh]#/opt/sh/test1.sh 1 2 3 4 5 6 7 8
The script name and path you executed are:/opt/sh/test1.sh
The first parameter of a script is: 1
The second parameter of the script is: 2
The third parameter of the script is: 3
The fourth parameter of the script is: 4
The fifth parameter of the script is: 5
The number of arguments to the script is: 8
The five parameters of the script are: 1 2 3 4 5 6 7 8

2.If statement

Script:

[email protected] sh]# cat test2.sh
#!/bin/bash
A=$1

If [$a-eq 0];then

echo "A is 0"

elif [$a-eq 1];then
echo "A is 1"

elif [$a-eq 2];then
Echo "A is 2"
Else
echo "A is not B"

Fi

Execution results

[Email protected] sh]#/test2.sh 0
A is 0
[Email protected] sh]#
[Email protected] sh]#/test2.sh 1
A is 1
[Email protected] sh]#/test2.sh 2
A is 2
[Email protected] sh]#/test2.sh 3
A is not B

3. While loop

Script

[email protected] sh]# cat test3.sh
#!/bin/bash

I=0
While [$i-ne 10]
Do
i=$ (($i + 1))

Echo $i
Done

Execution results

[Email protected] sh]#./test3.sh
1
2
3
4
5
6
7
8
9
10

4. For loop

Usage One:

#!/bin/bash

For ((i=1;i<=10;i++));

Do

echo $ (Expr $i \* 3 + 1);

Done

Usage Two:

#!/bin/bash

For I in $ (SEQ 1 10)

Do

echo $ (Expr $i \* 3 + 1);

Done

Shell Learning (iii)

Related Article

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.