Linuxday8--shell Script Programming Basics

Source: Internet
Author: User
Tags arithmetic

Variable

A variable is a named memory space

All variables in the shell are treated as character variables by default, and bash does not support floating-point variables

Naming rules for variables:

You cannot make a reserved word in a program: for example if, for

Use only numbers, letters, and underscores, and cannot start with a number

See the name of righteousness

Uniform naming rules: Hump naming method

The effective range of variables can be divided into:

Local variable: The active scope is the current shell process and is not valid for Shell processes other than the current shell, including the current Shell's child shell process

Environment (GLOBAL) variable: The active scope is the current shell process and its child processes

Local variable: The effective range is a snippet of code in the current shell process, usually referred to as a function

Positional variables: $, $, ... To indicate that the script is used to invoke parameters passed to it through the command line in the script code.

Special variables: $?, $, $*, [email protected], $#,$$

Local variables:

Variable assignment: var= ' value '

String: name= "root"

Variable reference: name= "$USER"

Command reference: Name= whoami

Variable reference: ${var}

"": weak reference, where the variable reference is replaced with the value of the variable

': Strong reference, where the variable reference is not replaced with the value of the variable, while preserving the original string

Set shows all the variables that have been defined

unset var delete a defined variable

Local variables are only valid for the current bash

Environment variables:

Variable declaration:

Export Name=value

Declare-x Name=value

commands to display environment variables:

Env

Printenv

Export

Declare-x

Bash built-in environment variables

PATH

SHELL

USER

Uid

HOME

Pwd

SHLVL is an accumulator that records the nesting depth of multiple Bash process instances

Lang

MAIL

HOSTNAME

Histsize

-Last parameter after execution of previous command

Read-only variable (constant)

Statement:

ReadOnly VAR

Declare-r VAR

Readonly-p viewing read-only variables

Positional variables

$1,$2 ...: corresponding script parameter location

Shift: Kick out the previous position variable

$#: Number of script parameters

$ A: Represents the absolute path of the script, or the absolute path of the symbolic link if it is a symbolic link

$*: All parameters passed to the script, all parameters are combined into a string

[Email protected]: All parameters passed to the script, each parameter is a separate string

[Email protected] $* only when it's wrapped in double quotes.

Set--Clears all positional variables

Exit status

$? : script or command exit status, 0 for success, not 0 (1-255) for failure

Exit [#]: Customize exit status Code and exit the current bash process

If no exit status code is specified for the script, the exit status code for the entire script depends on the status code of the last command executed in the script

Arithmetic operations

Operator:

+

-

*

/

%: modulo

* *: exponentiation

^: XOR

      • [[email protected] ~]# a=3[[email protected] ~]# b=5[[email protected] ~]# echo $a $b 3 5[[email protected] ~]# a=$[a^b];b= $[A^B];A=$[A^B] #a和b的值互换 [[email protected] ~]# echo $a $b 5 3

notation for arithmetic operations:

Let-var= arithmetic expression

var=$[Arithmetic expression]

var=$ ((arithmetic expression))

var=$ (expr arithmetic expression, but space is required in the middle of the operator)

echo ' Arithmetic expression ' |BC

$RANDOM: Bash built-in random number generator (0-32767)

Conditional test Logic operations

0,true

1,false

And:

1 and 1 = 1
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0

Or:

1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0

Non -:

! 1 = 0
! 0 = 1

Condition Testing (TEST)

Test EXPRESSION

[EXPRESSION]

[[EXPRESSION]]

Must have white space characters before and after expression

Conditional test, returns 0 for true, 1 for false

The logical relationship between commands

CMD1 && CMD2: Logical And if CMD1 returns to True, then CMD2 executes; otherwise CMD2 does not execute

CMD1 | | CMD2: Logical OR, if CMD1 is returned as false, then CMD2 executes, otherwise CMD2 does not execute

CMD1 | | {CMD2; CMD3; }: If CMD1 returns False, execute CMD2 and CMD3

Test character: Variable test:

-V: Whether the variable is set

Numerical test

-eq: Equals

-ne: Not equal to

-LT: Less than

-le: Less than or equal to

-GT: Greater Than

-ge: greater than or equal to

String test

= =: Equal

: Greater than, compare ASCII code

<: Less than, compare ASCII code

! =: Unequal

=~: Whether the left string can be matched by the pattern on the right, with [[[STRING =~ PATTERN]],pattern must be an extended regular expression

hostping.sh

-Z: Empty

-N: Is not empty

File test

-A = =-E: Whether it exists

-B: Whether it is a block device

-C: Whether it is a character device

-D: Whether it is a directory

-F: is normal file

-H = =-L: Whether it is a symbolic link

-P: Whether it is a pipe file

-S: is a socket file

-R: Readable

-W: Writable

-X: whether executable

-U: Do you have SUID permissions

-G: Do you have Sgid permissions

-K: Do you have sticky permissions

-S: is non-empty file

-T FD: Whether the file descriptor is in the open state

-N: Whether the file has been modified since the last time it was read

-O: Whether the current active user is a file owner

-G: Whether the current active user is a file group

File Properties Binocular Test

File1-ef file2:file1 is a hard link to FILE2

File1-nt File2:file1 is new to FILE2 (Mtime)

File1-ot File2:file1 is older than FILE2

Combination condition test

Combination test conditions

-A: With relationship (and)

-O: or relationship (OR)

! : Non-relational

Morgan's Law:

! (a-o B) =! A-a! B

! (a-a B) =! A-o! B

ReadCommand

Use read to assign the input value to one or more shell variables,

Read reads values from standard input, assigns a variable to each word

All remaining words are assigned to the last variable

Options:

-P: Enter prompt information

-S: Silent Input for password entry

-N #: Limit the length of the input string

-d ' EOF: Automatically ends when matched to a terminator

-T #: Time-out

#!/bin/bash#read-t 5-p "Please input head number:" Headnuread-t 5-p "Please input feet Number:" Feetnuecho-e "chook Number is $ ((($FEETNU -2* $HEADNU/2)) \n$ (($HEADNU-$ ((($FEETNU -2* $HEADNU/2)))) "

Linuxday8--shell Script Programming Basics

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.