shell essays

Shell Variables

When you define a variable, the variable name without the dollar sign ($, PHP language variables needed), such as:

your_name="runoob.com"

Note that there is a space between the variable name and the equal sign, which may be familiar to you and all programming languages ​​are not the same. Meanwhile, the name of the variable name must follow these rules:

  • Named only letters, numbers and underscores, the first character can not start with a number.
  • Excluding space, you can use an underscore (_).
  • You can not use punctuation.
  • Bash can not be used in keyword (help command to view the available reserved keywords).

Shell valid variable names examples are as follows:

RUNOOB
LD_LIBRARY_PATH
_var
var2

Invalid variable names:

?var=123
user*name=runoob

In addition to direct assignment explicitly, it can also be used for variable assignment statement, such as:

for file in `ls /etc`  for file in $(ls /etc)

The above statement will name the file / etc directory out of the loop.


Using Variables

Use a variable defined earlier, the variable name preceded by a dollar sign in so long, such as:

your_name="qinjx"
echo $your_name
echo ${your_name}

Variable name outside the curly braces are optional, plus without anything, add braces to help identify the boundary interpreter variables, such as the following case:

for skill in Ada Coffe Action Java; do echo "I am good at ${skill}Script" done

If you do not add braces to skill variables, written echo "I am good at $ skillScript", the interpreter will put $ skillScript as a variable (its value is empty), code execution is not the result we expect the same again.

Recommended for all variables braces, this is a good programming practice.

Defined variables, can be re-defined, such as:

your_name="tom"
echo $your_name
your_name="alibaba" echo $your_name

Write is legal, but note that when the second assignment can not write $ your_name = "alibaba", using variables when it added a dollar sign ($).

Read-only variables

Use readonly command to define variables as read-only variables, the value of read only variables can not be changed.

The following examples try to change the read-only variable, the results being given:

#!/bin/bash
myUrl="http://www.google.com"
readonly myUrl myUrl="http://www.runoob.com"

Run the script with the following results:

/bin/sh: NAME: This variable is read only.

Delete variables

Use the unset command to remove variables. grammar:

unset variable_name

Can not be used again after the variable is deleted. unset command can not remove the read-only variable.

Examples

#!/bin/sh
myUrl="http://www.runoob.com"
unset myUrl
echo $myUrl

Examples of the above executes no output.

Variable Types

When you run the shell, will at the same time there are three variables:

  • 1) local variables local variables defined in the script or command, only valid in the current shell instance, start the other shell program can not access local variables.
  • 2) environmental variables all procedures, including the shell is started, can access environment variables, some programs require environment variables to ensure their normal operation. When necessary, the shell script can also define environment variables.
  • . 3) shell variables shell variable is a special program variables set by the shell. shell variable environment variable part is, in part, local variables, to ensure the normal operation of the shell

Shell string

String shell programming is the most common and useful type of data (except numbers and strings, but also other types of handy lacks), the string enclosed in single quotes, double quotes may be used, it may be practiced without quotes. The difference between single and double quotation marks are similar with PHP.

apostrophe

str='this is a string'

Single quoted strings limits:

  • Any characters in single quotes are output as a single quotation mark in a string variable is invalid;
  • Single quotes string can not appear in a single apostrophe (single quotation marks nor escape after use), but can occur in pairs, as string concatenation.

Double quotes

your_name='runoob'
str="Hello, I know you are \"$your_name\"! \n" echo -e $str

The output is:

Hello, I know you are "runoob"!

Double quotes advantages:

  • Double quotes can have variable
  • Double quotes can appear escape character

String concatenation

your_name="runoob"
# 使用双引号拼接 greeting="hello, "$your_name" !" greeting_1="hello, ${your_name} !" echo $greeting $greeting_1 # 使用单引号拼接 greeting_2='hello, '$your_name' !' greeting_3='hello, ${your_name} !' echo $greeting_2 $greeting_3

The output is:

hello, runoob ! hello, runoob ! hello, runoob ! hello, ${your_name} !

Get string length

String = "ABCD"  echo $ {# String } # 4 outputs 

Extract a substring

The following examples from the character string of 2 characters beginning taken . 4 characters:

string="runoob is a great site"
echo ${string:1:4} # 输出 unoo

Find substring

Find character i or o position (which first appeared on the letter which calculated):

string="runoob is a great site"
echo `expr index "$string" io` # 输出 4

Note: The above script in `anti quotes, rather than single quotes , 'Do not be deceived, oh.


Shell Array

bash support one-dimensional array (not support multi-dimensional array), and do not limit the size of the array.

Similar to the C language, the subscript of the array elements are numbered starting from zero. To obtain the use of elements in the array subscript or index may be an integer arithmetic expression whose value is greater than or equal to 0.

Definition array

In the Shell, with parentheses for the array, the array element divided by the "space" symbol. The general form of an array defined as:

Array name = (value 1 value 2 ... value n- )   

E.g:

array_name=(value0 value1 value2 value3)

or

array_name=(
value0
value1
value2
value3
)

You can also define each individual component arrays:

array_name[0]=value0
array_name[1]=value1 array_name[n]=valuen

May not be used continuously subscripts and subscript range is not limited.

Read array

The general format of the read array element value is:

$ {Name array [index]}

E.g:

valuen=${array_name[n]}

Use the @ symbol can get all the elements in the array, for example:

echo ${array_name[@]}

Get the length of the array

Get the length of the array of methods and methods of obtaining the same string length, for example:

# Number of elements in the array element 
length = $ {# ARRAY_NAME [@]} # or length = $ {# ARRAY_NAME [*]} # length of the array of individual elements made lengthn = $ {# ARRAY_NAME [ n- ]}  

Shell Notes

To start with # is a comment and will be ignored interpreter.

Each row by adding a # number is set multi-line comments, like this:

-------------------------------------------- # # This is a comment # author: rookie tutorial # Site: www.runoob.com # Slogan: school is not only technology, but also dream! -------------------------------------------- # ##### user configuration area beginning ##### # # # this script can add descriptive information # # ##### ##### end-user configuration area

          

If the development process, encountered large chunks of code need to temporarily comment up after a while and uncommented, how to do it?

Each row add a # symbol too much effort, you can put a period to be annotated code in a pair of curly brackets, defined as a function, no place to call this function, this code will not be executed, and reached the same comment Effect.

Multi-line comments

Multi-line comments can also use the following format:

: << EOF
 annotation content ... annotation content ... annotation content ... EOF
 

EOF can also use other symbols:

: << ' 
comments contents ... 
the comment content ... 
footnotes ... 
' :! << Notes content ... annotation content ... footnotes ... !


   

Guess you like

Origin www.cnblogs.com/tyk3201/p/11967621.html