Shell basics 3

Source: Internet
Author: User

Continue Process Control

This is case. It can match the variable content and multiple comparison characters. If the matching is successful, this part of code is executed. It can only be a string.

The writing structure is as follows:

Case match character in comparison symbol) Statement; esac

Note: The statement must be followed by a double semicolon ;;.
For example:

The current directory contains the.txt file,

File a.txt: ASCII text

It is used to match the execution result. The program is as follows:

#! /Bin/shfileType = 'file "$1" '# aa: ASCII textecho $ {fileType} case $ {fileType} inaa *) echo 'this is file name'; ASCII) echo 'this is file Code'; text) echo 'this is type'; *) echo 'this is end' esac # execution result this is file name

 

Note: fileType = 'file "$1" '. The outermost part is not a single quotation mark, but a reverse quotation mark (also called a duplicate note, whatever you know.) on the tab key.

In ruby, shell scripts are called to use duplicate notes, for example, 'sh test. Sh'

 

Loop in shell: for, while

1. while

Basic Structure:

While [conditional expression] do... done
 
Great sweat! After writing for half a day, csdn has not been automatically saved. Rewrite it.
Write an example:
echo 'put a:'read awhile [ ${a} -lt 100 ]do  echo ${a}  a=` expr ${a} + 1 `done

Program: enter a number, whether it is less than 100, if it is less than, output and + 1

Execution result: Enter 96, output: 96 97 98 99

The following is an interesting small program. The output result is as follows:

00101210232103432104543210565432106765432107876543210898765432109

There are many program implementation methods. The following are for your reference only.

i=0while [ ${i} -lt 10 ]do  y=${i}  while [ ${y} -ge 0 ]  do   echo -n ${y}   y=` expr ${y} - 1 `  done  echo ${i}  i=` expr ${i} + 1 `done

2.

Basic Structure:

For does not have the $ variable in variable do ...... done

The for loop of a string is to split the string by spaces. Example:

For I in "a s d f g h" do echo $ {I} done # input result: a s d f g h

Number loop. Returns an integer that can be divided by three integers in less than 10. The example is as follows:

For I in $ (seq 10) doif (I % 3 = 0) thenecho $ ifidone # execution result: 369

In combination with command execution. Displays the files in the current directory, for example:

All = 'sudo ls' for I in $ {all} do echo $ {I} done # execution result: aacycle.shoperFile.shout.txtprocess.shrename.shstderr.txtstdout.txt test. shtrackCodeCheck. rb

In addition, the loops in shell include until and select. You can view the relevant information for specific applications.

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.