Shell variable Day01

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators

Variable

Defining variables
Your_name= "Runoob.com" #变量名和等号之间不能有空格

Using variables
Your_name= "QINJX"
Echo $your _name
Echo ${your_name}
echo "I am good at ${skill}script"
echo "I am good at ${skillscript}"
Curly braces are added to help the interpreter identify the bounds of the variable

Your_name= "Tom"
Your_name= "Alibaba"
Variables can be redefined

Read-only variables
Myurl= "http://www.w3cschool.cc"
ReadOnly Myurl
Myurl= "Http://www.runoob.com"
The value of a variable defined using the readonly command cannot be changed

Delete a variable
Unset variable_name
unset command cannot delete read-only variables

Variable type
1) Local variables are only valid in the current shell instance.
2) environment variable All programs, can access environment variables.
3) Shell variable shell variables are part of the environment variables, part of the local variables,

Shell string
Single quotation marks
Str= ' This is a string '

Single-Quote String restrictions:
1, any character in single quotation marks will be output as is, the variable in the single quote string is invalid;
2. Single quotation marks cannot appear in single quote strings (not after using escape characters for single quotes).

Double quotes
Your_name= ' QINJX '
Str= "Hello, I know your is \" $your _name\ "! \ n "

Advantages of double quotes:
You can have variables in double quotes.
Escape characters can appear in double quotes

Stitching strings
Your_name= "QINJX"
greeting= "Hello," $your _name "!"
greeting_1= "Hello, ${your_name}!"
echo $greeting $greeting _1

Get string length
string= "ABCD"
echo ${#string}

Finding substrings
String= "Runoob is a great company"
echo ' expr index ' $string ' is '

Shell Array
Bash supports only one-dimensional arrays,
The subscript can be an integer or an arithmetic expression
Subscript can be discontinuous
No need to define array size

Defining arrays
Brackets to represent the array, the elements are separated by "spaces"
1, array_name= (value0 value1 value2 value3)
2, Array_name= (
Value0
Value1
value2
Value3
)
3, Array_name[0]=value0
Array_name[1]=value1
Array_name[n]=valuen

Reading an array
${array name [subscript]}
Valuen=${array_name[n]}
${array_name[@]} #数组中的所有元素

Gets all the elements in the array
${my_array[*]}
${my_array[@]}

Gets the length of the array
length=${#array_name [@]}
length=${#array_name [*]}

Gets the length of an array of individual elements
lengthn=${#array_name [n]}

Comments
#
Defined as a function, there is no place to call this function, this code will not execute


Shell Pass Parameters

echo "file name executed: $";
echo "The first parameter is: $ zero";
echo "The second parameter is: $";
echo "The third parameter is: $ $";

./test.sh 1 2 3
Bash test.sh 1 2 3

File name executed: test.sh
The first parameter is: 1
The second parameter is: 2
The third parameter is: 3

$ #参数个数
$$ the current process ID number for the script to run
$! the ID number of the last process running in the background
$-shows the current options that the shell uses, as is the SET command function.
$? Displays the exit status of the last command. 0 means no error
$* outputs all parameters in the form of "$ $ $n".
[Email protected] with "$" "$" ... Output all parameters in the form "$n"

Shell Basic Operators
Val= ' Expr 2 + 2 '
There is a space between an expression and an operator
Conditional expressions are placed between square brackets and have spaces

Arithmetic operators
+ addition ' expr $a + $b ' result is 30.
-Subtraction ' expr $a-$b ' result is 10.
* multiply ' expr $a \* $b ' result is 200.
/Division ' expr $b/$a ' result is 2.
% "Expr $b% $a ' result is 0.

= Assignment A= $b assigns the value of variable B to a.
= = Equal. The same returns true. [$a = = $b] returns FALSE.
! = is not equal. is not the same, returns TRUE. [$a! = $b] Returns TRUE.

a=10
B=20

Val= ' expr $a + $b '
Val= ' expr $a-$b '
Val= ' expr $a \* $b ' # * must be added in front of
Val= ' expr $b/$a '
val= ' expr $b% $a '
if [$a = = $b]
Then
echo "a equals B"
Fi

if [$a! = $b]
Then
echo "A is not equal to B"
Fi

Relational operators
Relational operators only support numbers, and strings are not supported unless the value of the string is a number.

-eq==[$a-eq $b] returns false.
-ne!=[$a-ne $b] returns TRUE.
-gt>[$a-gt $b] returns false.
-lt<[$a-lt $b] returns TRUE.
-ge>=[$a-ge $b] returns false.
-le<=[$a-le $b] returns TRUE.

Boolean operator
The non-operation [! false] returns TRUE.
-oor[$a-lt 20-o $b-GT 100] returns TRUE.
-aand[$a-lt 20-a $b-GT 100] returns FALSE.

logical operators
&& and[[$a-lt && $b-GT 100]] returns false
|| or[[$a-lt | | $b-GT 100]] returns True

String operators
A= "ABC"
b= "EFG"

= detects whether two strings are equal and returns true for equality. [$a = $b] returns FALSE.
! = detects whether two strings are equal, and returns true if they are not equal. [$a! = $b] Returns TRUE.
-Z detects if the string length is 0 and returns true for 0. [-Z $a] returns false.
-N detects whether the string length is 0 and does not return true for 0. [-N $a] returns true.
STR detects if the string is empty and does not return true for null. [$a] returns TRUE.

File Test Operators

-B file detects block device files, and returns True if yes. [-B $file] returns FALSE.
-C file detects character device files, and returns True if it is. [-C $file] returns false.
The-D file detects the directory and returns True if it is. [-D $file] returns false.
-f file detects normal files (neither directories nor device files), and returns True if it is. [-F $file] returns TRUE.
The-G file detects that the SGID bit is set and returns True if it is. [-G $file] returns false.
-K file detection sets the sticky bit (Sticky bit) and returns true if it is. [-K $file] returns false.
-P file detects the named pipe, and returns True if it is. [-P $file] returns false.
-U file detection sets the SUID bit and returns true if it is. [-U $file] returns false.
-r file detects readable, and returns true if it is. [-R $file] returns TRUE.
-W file detects writable, and returns True if it is. [-W $file] returns TRUE.
The-X file detects the executable and, if it is, returns True. [-X $file] returns TRUE.
-S file detects null (if the file size is greater than 0), not NULL returns TRUE. [-S $file] returns TRUE.
-e file detection exists, and if it is, returns True. [-e $file] returns TRUE.

Shell variable Day01

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.