Shell syntax usage

Source: Internet
Author: User

The first time I came into contact with shell programming in the past two days, the so-called shell programming is actually to combine various basic commands with certain syntaxes to let shell programs explain and execute. If you have an understanding of windows dos, you can understand it in this way. In fact, shell script files are similar to. bat batch processing files, but shell in linux is much more powerful than dos in windows.

There are many shell types, such as bash, sh, tcsh, and ksh. bash is used by default in linux.

Let's take a look at the first program to learn shell:

Enter the command vi my_01.sh to create a my_01.sh file and open it in the vi editor. The Edit content is as follows:

 #!/bin/    

The first line here is to let the bash shell execute this shell program. Echo is used for output. The second and third rows output two sentences respectively.

You can use the following methods to execute this shell program:

1./my_01.sh

2. my_01.sh

3 bash my_01.sh

The second shell program is as follows:

 #!/bin/ #my_02. read -p   

Here, the third-line read command refers to waiting for user input, and-p is followed by the prompt. It also has a parameter-t that sets the waiting time. The program running result is as follows:

[Liuling @ bogon test] $./my_02.sh
Please input your name: liuling
Liuling, welcome!

 

 

 

The following are examples:

 #!/bin/ #my_03.  read -p  read -p    test $name = "liuling"   [ $name =  -a $ =                
 #!/bin/ #my_04.  read -p    [ $score -gt          [ $score -gt  -a $score -le                
#! /Bin/# my_05. read-p # If you use (), you can only use <>>=< = ($ score> [[$ score-gt & $ score-le

Test whether the input file name is a directory or a file:

 #!/bin/ #my_06.  read -p   [ -              [ -                     [ -                                    

 

The three examples are about the use of if condition judgment. Conditions can be written in the following ways: [conditions], test conditions, [[conditions], and (Conditions). The most commonly used conditions are the first. Common judgment symbols are as follows:

 

  

 var *


The following are examples of case usage:

1. Select a simulated menu

 #!/bin/ #my_07.           read -p   $num                              -               -           *             

 

Iii. Use of cyclic statements

1. for Loop

Variable name


The following is a small example of for loop usage:

 #!/bin/ #my_08.   a  `              ((i=;i<=;i++       

  

The following is a small program for counting the number of files:

 #!/bin/ #my_09.  i=  name1  ` /     # i=` $i +     # let i++    # i=$[$i +     # i=$(($i +     ((i++    

  

 

The following is a shell script for entering customer data:

 #!/bin/ #my_10.                          $choice                                                                                  ${name1}${age1} >>              


 

Iv. Application of functions

 

The following is a small example program:

 #!/bin/ #fun.       $(($+$  add   add  

 

  

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.