Shell Programming Learning

Source: Internet
Author: User
Tags php programming

1.Shell programming As with Java, PHP programming, as long as there is a code to write a text editor and a can explain the execution of the script interpreter can be

#!/bin/sh (#! tells the system that the program specified after the path is the shell that interprets this script file)

The 2.shell script has an. sh extension and echoes Echo

The string after echo can be quoted or not added

Show escape characters
Echo " \ "It is a test\" "

The result will be:

  " It is a test "
Show variables

The read command reads a row from the standard input and assigns the value of each field in the input row to the shell variable

  #!/bin/sh  read name   echo"$name It is a test"
4. Show line breaks
  Echo " ok!\n " #-e turn on  escape echo"it it a test"

Output Result:

Ok! it it a test
Show No Line breaks
#!/bin/shecho"ok! \c" #-e turn on escape \c do not wrap echo"It is a test"

Output Result:

Ok! It is a test
Show results directed to a file
  Echo " It is a test " > MyFile
Output a string as is, without escaping or taking a variable (in single quotes)
  Echo ' $name \ " '

Output Result:

  $name \"
Show command Execution results
  echo 'date'

The result will show the current date

   - ::

3. How to run a shell script

chmod +x./test. SH   #使脚本具有执行权限. /test. SH  #执行脚本

./Tell the system to find it in the current directory

You can also run the interpreter directly, whose parameters are the file name of the shell script

/bin/sh test. SH

4. Variables

Define variables in the same way as batch commands (batch with set, no double quotes)

Your_name="runoob.com"

5.for statements

 for file  in 'ls /etc '

The above statement loops out the file name of the directory under/etc.

6. Using variables

With a defined variable, just precede the variable name with a dollar sign, the curly braces outside the variable name are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variable. Such as:

Your_name="qinjx"echo  $your _nameecho ${your_name}

Note: Reference variable in batch is% variable%

7. String

Strings can be in single or double quotes, or without quotes

Single quotation marks
str='This isa string'

Single-Quote String restrictions:

    • Any character in a single quotation mark is output as is, and the variable in the single-quote string is not valid;
    • 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="$your _name"  ! " greeting_1="Hello, ${your_name}! " " echo $greeting $greeting _1

Get string length

string="hello"echo ${#string}

Extract substring

string="hello"echo ${string:1:2}

Print St

8.Shell arrays

Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays.

Defining arrays

In the shell, the array is represented by parentheses, and the elements of the array are separated by a "space" symbol. The general form of the definition array is:

Array name = (value 1 value 2 ...) Value N)

For example:

Array_name= (value0 value1 value2 value3)

You can also define individual components of an array individually:

array_name[0]=value0array_name[1]=value1array_name[n]=valuen
Reading an array

The general format for reading array element values is:

${array name [subscript]}

For example:

Valuen=${array_name[n]}

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

echo ${array_name[@]}
Gets the length of the array

The method of getting the length of the array is the same as getting the string length, for example:

# Gets the number of array elements length=${#array_name [@]}# or length=${#array_name [*]}# gets the length of the single element of the array lengthn=${ #array_name [n]}
9.Shell comments

Lines that begin with "#" are comments, which are ignored by the interpreter.

There is no multiline comment in sh, only one # for each line

10.Shell Test command

The test command in the shell is used to check if a condition is true, and it can be tested in three aspects of numeric, character, and file.

Parameter description-eq equals True-ne not equals True-gt greater than true-ge greater than or true-lt less than true-le less than equals is true

Numerical test
parameter    description -eq    equals True -ne    not equals true-GT is greater than    true -ge    greater than equals is true- LT is    less than true -le is    less than or equal to true

Example Demo:

num1=num2=if test $[num1]-eq $[num2]then    Echo  'thenumbers is equal! ' Else    Echo ' The numbers is not equal! ' fi

Output Result:

The numbers is equal!


String test

parameter     Description =      equals true ! =      Inequality true-z string string     length pseudo true-n string string     length False true

Example Demo:

num1=num2=if test num1=num2then     echo'thestrings is equal! ' Else    Echo ' The strings is not equal! ' fi

Output Result:

The strings is equal!

Shell Programming Learning

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.