Shell Programming (i)

Source: Internet
Author: User

In my resume, I wrote that I am familiar with common Linux commands and can do shell programming, so it is necessary to learn shell programming.

Read the catalogue:

    • First, Shell and bash

    • II. structure and execution of shell scripts

    • Iii. loops, if judgments, functions in the shell
First, Shell and bash

The shell is the interface program that the user interacts with the Linux operating system kernel, and is also a command language interpreter, which interprets the commands entered by the user to the Linux kernel.

? There are many kinds of shells, common with Bourne shell (/usr/bin/sh or/bin/sh), Bourne Again Shell (/bin/bash), C Shell (/usr/bin/csh), K Shell (/usr/ Bin/ksh), Shell for Root (/sbin/sh), and so on.

Bash is a shell and is the default shell used by most Linux systems.

II. structure and execution of shell scripts

1. Script format

Scripting with the VI editor? The format is fixed, as follows:

#!/bin/sh? #! tells the system that the program specified later in the path is the shell program that interprets this script file.

#comments//Comment lines

Your commands go here

2. Execute the Script

After editing the script, save the file name filename.sh, and before running this script, you need to modify the executable permissions for this script:

Chmod+x fi?lename.sh

Execute script:

./filename.sh

3. The simplest Hello World program

#!? /bin/sh

A= "Hello World"; Variable Assignment variable_name = Variable_value

echo $a;? Use variable $variable or ${variable}

Iii. loops, if judgments, functions in the shell

1. For loop?

For I in $ (SEQ 0 5);d o

echo $i?

Done?

2.while Cycle

while condition; do

Command

Done

For example: a=10

While? [$a-ge 1];d o

? Echo $a

a=$[$a-1?]

Done

3. If judgment statement; Then command fi

if Judgment statement; Then command

Else? Command

Fi

If judgment statement one; then command

Elif Judgment Statement two; Then command

else command

Fi

if ((a<5)) is equivalent to if[$a-lt 5]? -lt less than

if ((a>5))? Equivalent to if[$a-GT 5]-gt greater than

if ((a>=5)) is equivalent to if[$a-ge 5]-ge greater than or equal

if ((a<=5)) is equivalent to if[$a-le 5]-le less than or equal to

if ((a==5)) is equivalent to if[$a-eq 5]-eq equals

if ((a!=5))? Equivalent to if[$a-ne 5]-ne not equal to

Determine the size of the value in addition to (()) The form, you can also use []?

? The mathematical calculation is to be enclosed in [] and a $ is taken outside.

A=1

b=2

sum=$[$a + $b]?

4. Functions in shell scripts

Function name () {

Command

}

In a shell script, the function is written in front.

function sum () {

SUM=$[$1+$2]

Echo $sum

}

Sum $?

Shell Programming (i)

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.