Shell Programming-cont.

Source: Internet
Author: User
Tags arithmetic exit in

Shell scripts can be seen as a stack of commands, implemented by combining command flow control mechanisms.

The interpreter #!/bin/bash or/usr/bin/python is also known as Shebang

The generic script needs to give execution permission, or you can use bash xxx.sh to declare it beforehand


Shell's conditional judgment can be divided into

1: Integer test

2: String test

3: File test


Conditional test Expression

[Expression] #注意方括号与表达式之间有空格

[[Expression]] #也有这种格式的测试格式

Test expression


Integer comparison

-eq: Two integers are equal #equal

-ne: Whether two integers are unequal #not equal

-GT: Whether the previous number is greater than the next number

-LT: Whether the previous number is less than the next number

-ge: Whether the previous number is greater than or equal to the next number

-LT: Whether the previous number is less than or equal to the last number


File test:

-e/path/to/file: Test file exists

For example: [-e/etc/inittab] [!-e/etc/inittab]

-d/path/to/file: Test for Directory

-f/path/to/file: Test for normal files

-r/path/to/file test file has read access to the current user

-w/path/to/file test file has write access to the current user

-x/path/to/file test file has execute permissions on the current user


Inter-command logic

Logic and:&&

Logical non-:!

Logical OR: | |


Conditional judgment, control structure

if judgment condition; then #单分支

Statement1

Statement2

...

Fi


if judgment condition; then

Statement

...

Else

Statement

...

Fi


Arithmetic operations

Arithmetic expression:

1:let Arithmetic Expression example: let c= $A + $B

2:$[arithmetic operation expression] Example: c=$[$A + $B]

3:$ (arithmetic expression)) Example: c=$ (($A + $B))

The 4:expr arithmetic expression #表达式中各操作数和运算符之间要有空格, and you want to use a command reference

Example: c= ' expr $A + $B '


Use exit in the script to exit the current shell

Exit # Custom Return status value

#如果脚本没有定义状态退出码, the exit code for the last command is the script exit code


Multi-Branch If statement

if judgment condition; then

Statement

...

Elif Judging Condition 2;then

...

...

Else

...

Fi


Test scripts

Bash-n xxx.sh #简单检查脚本语法错误.

Bash-x xxx.sh #可查看每个语句的执行结果 to check for errors.



This article is from the "11994222" blog, please be sure to keep this source http://12004222.blog.51cto.com/11994222/1857647

Shell Programming-cont.

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.