Shell basics 1

Source: Internet
Author: User

The first shell script program. VI create a file test. SH and grant the execution permission

 

#!/bin/shecho 'hello word !'

 

Execution result: Hello word!

Shell script structure #! Start with/bin/sh, and next to the line feed is the program body. Of course, here we only use #! /Bin/sh is used as an example.

I generally follow this process to learn a new language.

1. assign values to variables and variables

2. Process Control

3. dataset operation: Set Array

4. Class and method call

I am familiar with the above things and have the most basic knowledge of this language.

I. Variables

All variables are strings and do not need to be declared.

first='OKOKOK'

Note that there is no space between the left and right sides of the equal sign.

Reference: $ {First} or $ {First}

# Assign the first value to secondsecond = $ first

 

 

first=2echo 'this is ${first}'

Execution result: This is 2

 

2. Process Control

1. If judgment

if...then.....elif....then.....else....fi

Format.

Different from Java, Java conditions are wrapped with (), and shell conditions are enclosed with [] or () square brackets. Note that there must be spaces before and after square brackets.

For example:

a='1'if [ ${a} -eq '1' ]then echo 'this is 1'elif [ ${a} -eq '2' ]then echo 'this is 2'else echo 'this is other'fi

 

Execution result: This is 1

The above program compares whether a variable is equal to a value.

 

The following is the if judgment of the composite condition.

a='3'if [[ ${a} -eq '1' ]] || [[  ${a} -eq '3' ]]then echo 'this is 1'elif [ ${a} -eq '2' ]then echo 'this is 2'else echo 'this is other'fi
 
Execution result: this is 1
In java, if (a. equals ("1") | a. equals ("3"), but not here.
This is also possible:
if [ ${a} -eq '1' -o   ${a} -eq '3' ]
-O: Yes |, or
 
When it comes to process judgment, expressions and logical operators are required. The next article will write the corresponding information.
 

 

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.