Quick query of Linux Shell syntax

Source: Internet
Author: User
Quick query of Linux Shell syntax compiled by myself (comparison using C language syntax)

Although I have learned Linux Shell programming, there are only a few chance to write shell scripts (usually I write more when setting up a development environment in the early stage of the project ), as a result, some syntaxes will be forgotten over time. The efficiency of reading a simple syntax is really slow, so I sorted out a table, since the programming language I use is C/C ++, the first thing that comes to mind in programming is how to write it in C language, so I will simply compare the C language syntax with the shell programming syntax, so that it is convenient to query it. HA:

 

(Note: I do not know why the pasted table in the word is not completely displayed here, and it is normal during editing)

 

 

Functions to be implemented

C Programming

Linux Shell Script Programming

Passing Program/Script Parameters

Int main (INT argc, char ** argv)

{

If (argv! = 4 ){

Printf ("Usage: % s arg1 arg2 arg3", argv [0]);

Return 1;

}

 

Printf ("arg1: % s \ n", argv [1]);

Printf ("arg2: % s \ n", argv [2]);

Printf ("arg3: % s \ n", argv [3]);

Return 0;

}

#! /Bin/sh

 

If [$ #-lt 3]; then

Echo "Usage: 'basename $ 0' arg1 arg2 arg3"> & 2

Exit 1

Fi

 

Echo "arg1: $1"

Echo "arg2: $2"

Echo "arg3: $3"

Exit 0

Int main (INT argc, char ** argv)

{

Int I;

For (I = 1; I <= argc; I ++ ){

Printf ("Arg: % s \ n", argv [I]);

}

Return 0;

}

#! /Bin/sh

 

While [$ #-Ne 0]

Do

Echo "Arg: $1"

Shift

Done

Logical/numeric operations

If (D = 0)

If ["$ D"-EQ "0"]; then

If (D! = 0)

If ["$ D"-ne "0"]; then

If (D> 0)

If ["$ D"-GT "0"]; then

If (d <0)

If ["$ D"-lt "0"]; then

If (d <= 0)

If ["$ D"-Le "0"]; then

If (D> = 0)

If ["$ D"-ge "0"]; then

String comparison

If (strcmp (STR, "ABC") = 0 ){

}

If ["$ Str "! = "ABC"]; then

Fi

Input and Output

Scanf ("% d", & D );

Read d

Printf ("% d", d );

Echo-N $ d

Printf ("% d", d );

Echo $ d

Printf ("press any to continue ...");

Char CH = getchar ();

Printf ("\ Nyou pressed: % C \ n", CH );

#! /Bin/sh

 

Getchar ()

{

Savedtty = 'stty-G'

Stty cbreak

Dd If =/dev/tty BS = 1 COUNT = 1 2>/dev/null

Stty-cbreak

Stty $ savedtty

}

 

Echo-n "press any key to continue ..."

Ch = 'getchar'

Echo ""

Echo "you pressed: $ ch"

 

Read d <& 3

Procedures for controlling programs and scripts

If (isok ){

// 1

} Else if (isok2 ){

// 2

} Else {

// 3

}

If [isok]; then

#1

Elif [isok2]; then

#2

Else

#3

Fi

Switch (d)

{

Case 1:

Printf ("you select 1 \ n ");

Break;

Case 2:

Case 3:

Printf ("you select 2 or 3 \ n ");

Break;

Default:

Printf ("error \ n ");

Break;

};

Case $ D in

1) echo "you select 1"

;;

2 | 3) echo "you select 2 or 3"

;;

*) Echo "error"

;;

Esac

For (INT loop = 1; loop <= 5; loop ++ ){

Printf ("% d", loop );

}

For Loop in 1 2 3 4 5

Do

Echo $ Loop

Done

Do {

Sleep (5 );

} While (! Isroot );

Is_root = 'who | grep root'

Until ["$ is_root"]

Do

Sleep 5

Done

Counter = 0;

While (counter <5 ){

Printf ("% d \ n", counter );

Counter ++;

}

Counter = 0

While [$ counter-lt 5]

Do

Echo $ counter

Counter = 'expr $ counter + 1'

Done

While (1 ){

}

While:

Do

Done

Break;

Break or break N, N indicates that the n-level loop exists.

Continue;

Continue

Function and Process Definition

Void Hello ()

{

Printf ("Hello \ n ");

}

...

// Function call

Hello ();

Hello ()

{

Echo "Hello"

} Or

Function Hello ()

{

Echo "Hello"

}

...

# Function call

Hello

Function parameters and return values

Int ret = doit ();

If (ret = 0 ){

Printf ("OK \ n ");

}

Doit

If ["$ ?" -EQ 0]; then

Echo "OK"

Fi

Or

Ret = doit

If ["$ RET"-EQ "0"]; then

Echo "OK"

Fi

Int sum (int A, int B)

{

Return A + B;

}

Int S = sum (1, 2 );

Printf ("the sum is: % d \ n", S );

Sum ()

{

Echo-n "'expr $1 + $2 '"

}

S = 'sum 1 2'

Echo "the sum is: $ S"

Bool isok () {return false ;}

If (isok ){

Printf ("Yes \ n ");

} Else {

Printf ("No \ n ");

}

Isok ()

{

Return 1;

}

If isok; then

Echo "yes"

Else

Echo "no"

Fi

 

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.