Shell Basic Syntax

Source: Internet
Author: User
Tags case statement echo command syslog

A Linux basic commands

1.1. CP command

The function of this command is to copy the given file or directory to another file or directory, which is very powerful.

Syntax: CP [option] source file or directory destination file or directory

1.2. MV Command

Users can use the MV command to rename files or directories or to move files from one directory to another.

Syntax: MV [options] source file or directory destination file or directory

1.3. RM command

Users can use the RM command to delete unwanted files or directories.

Syntax: RM [Options] File

If the-r option is not used, RM does not delete the directory.

1.4. mkdir and RmDir

Syntax: mkdir [Options] Dir-name

Syntax: rmdir [Options] Dir-name

1.5. CD command

Function: Change the working directory.

Syntax: CD [directory]

1.6. ls command

LS is a shorthand for the English word list, and its function is to list the contents of the directory. This command is similar to the dir command under DOS.

Syntax: LS [options] [directory or file]

1.7. su command

This command is very important. It allows a normal user to have Superuser or other users ' privileges, and also allows the superuser to do something as a normal user. Ordinary users must have a password for the superuser or other user when using this command.

? The general form of the command is: su [options] [user account]

1.8. PS command

Displays the programs executed in the system.

Syntax: PS [options]

1.9. Kill command

Remove a program in execution

Syntax: Kill [Options] PID

1.10. grep command

Search for specific text for output

Syntax: grep string

Cases:

PS aux | grep matlab

Kill PID

1.11. Echo command

The function of the echo command is to display a piece of text on the display, which generally acts as a hint.

The general format of the command is: Echo [-n] String

1.12. Clear command

The function of the clear command is to clear the information on the screen, which resembles a CLS command in DOS. After you clear the screen, the prompt moves to the upper-left corner of the screen.

Two Basic knowledge of the shell

2.1. Shell prompt and its environment

Prompt: $

Environment: Linux,unix,dos, etc.

? The shell is actually an explanation of the execution of the command program, so-called shell programming is to use a certain syntax to combine various basic commands, let the shell program to explain the execution.

Like traditional programming languages, the shell provides a number of features that make your shell script programming more useful, such as data variables, parameter passing, judgment, Process Control, data input and output, subroutines, and interrupt processing.

2.2 How to execute a shell program

Shell Program (***.sh)

? When you execute this command

(1) #./***.sh

(2) #chmod u+x ***.sh

#***.sh

(3) #sh ***.sh

2.3 Constants

? strings, such as "Hello world! ”。

? Numbers, for example 705, 23.

2.4 Variables

In shell programming, they are no type, or weakly typed programming language, which can contain a number, a string, a word, and so on. You do not need to declare this variable, it will be created when referencing the variable.

Str= "Hello world!"

Echo $STR

Home= "/Home"//note assignment with no spaces

A=123

home_list=$ (Ls/home)//Assign the execution result of the command to the variable

Ehco $HOME//$ symbol is the value that gets the variable HOME

A=a+1

 

System variables:

The execution name of this program

$n the nth parameter value of this program, n=1...9

$* all parameters of this program

$# the number of parameters for this program

$$ the PID of this program

$! PID to perform the previous background instruction

$? The return value of the previous instruction

2.5 Local Variables

A local variable generally refers to a variable written in a function, and his vitality is limited. Local variables are better understood, and there are no more introductions here.

2.6 Entering variable values from the keyboard

Using the Read command

Read Var1 var2 ... Varn

2.7 Notes

? Comments in Shell programming begin with #

2.8 Numeric operations

The main point is integer arithmetic. The expr command can convert a character variable to an integer operation

Syntax: expr integer operator integer

where operator is +-*/%, but the use of * should be with escape character/, such as:

[Email protected] ~]$ more dvd.sh

#!/bin/bash

Expr 5/* 5

[Email protected] ~]$ sh dvd.sh

25

2.9 Logical Operations

Test is a logical operation, enclosed in [] is the test operation.

Int1-eq int2 equal?

Int1-ne Int2 range?

INT1-GT int2 int1 > Int2?

Int1-ge int2 int1 >= int2?

Int1-lt Int2 int1 < Int2?

Int1-le Int2 int1 <= int2

2.10 double quotes and single quotes

? $echo "$HOME $PATH"--Show variable values

/home/hbwork Opt/kde/bin:/usr/local/bin:

$echo ' $HOME $PATH '--show the contents in single quotes

$HOME $PATH

2.11 Spaces

Because the shell does nothing with the extra space in the command, it prevents the shell from removing the whitespace by enclosing it in quotation marks.

$ str1=abcd

$ str2= "ABCD"

2.12 Branch Structure

2.12.1 Structure One

if [variable = value]

Then

Command

Else

if [variable = value]

Then

Command

Else

Command

Fi

2.12.2 Structure II

if [variable = value]

Then

Command

elif [variable = value]

Then

Command

Fi

Example of an IF statement:

If ["-lt"] then//Note space

echo "Less than 33"

Else

echo "No"

The end of the FI//if statement

2.13 Case Statement

Case value in

PATTERN1)

command;;

PATTERN2)

command;;

...

PATTERNN)

Command

Esac

Example case:

echo "Enter a number"

Read ANS//reads a variable read

Case $ans in

1)

echo "You Numer is $ans"

;; Note that the symbol is two;

2)

echo "You are number is 2"

;;

[3-9])

echo "You are $ans"

;;

*)//* wildcard characters

echo "Others"

Esac

2.14 AND And OR

Command1 && Command2

? Command1 | | Command2

2.15 Loop Statements

2.15.1 for Loop

For Var in arg1 arg2 ... argn

Do

Command

....

Command

Done

For Loop example

Int=1

For $int in 1 2 3 4 5

Do

sq= ' Expr $int/* $int '

Echo $sq

Int= ' expr $int + 1 '

Done

2.15.2 while loop

While command

Do

Command

Command

Command

...

Done

Example of a while loop

Int=1

While [$int-le 5]

Do

sq= ' Expr $int/* $int '

Echo $sq

Int= ' expr $int + 1 '

Done

2.15.3 until cycle structure

Until command

Do

Command

Command

....

Command

Done

Until Loop example

? Int=1

Until [$int-GT 5]

Do

sq= ' Expr $int/* $int '

Echo $sq

Int= ' expr $int + 1 '

Done

2.16 exiting from the loop: Break and Continue commands

Break immediately exits the loop

Continue ignore the other commands in this loop and proceed to the next loop

2.17 Functions (sub-procedures)

FuncName ()

{

Command

...

Command #分号

}

function Example

Setup ()

{command list;}

Do_data ()

{command list;}

Setup

Do_data

Debugging of the 2.18 shell program

? Use-X to track execution, execute and display each instruction.

2.19 comparison Operators

2.19.1 File comparison Operators

-e filename true if filename exists [-e/var/log/syslog]

-D filename True if filename is a directory [-d/tmp/mydir]

-F filename True if filename is a regular file [-f/usr/bin/grep]

-L filename True if filename is a symbolic link [-l/usr/bin/grep]

-R filename True if filename is readable [-r/var/log/syslog]

-W filename if filename is writable, true [-w/var/mytmp.txt]

-X filename is true if filename is executable [-l/usr/bin/grep]

Filename1-nt filename2 If filename1 is newer than filename2, then true [/tmp/install/etc/services-nt/etc/services]

Filename1-ot filename2 If filename1 is older than filename2, then true [/boot/bzimage-ot Arch/i386/boot/bzimage]

 

2.19.2 string comparison operator (note the use of quotation marks, which is a good way to prevent whitespace from disturbing the code)

-Z String True if string length is zero [-Z ' $myvar ']

-N String if string length is nonzero, true [-n ' $myvar ']

string1 = string2 If string1 is the same as string2, then true ["$myvar" = "One of the three"]

String1! = string2 If string1 is different from string2, then true ["$myvar"! = "one, three"]

  

2.19.3 Arithmetic comparison operators

Num1-eq num2 equals [3-eq $mynum]

Num1-ne num2 Not equal to [3-ne $mynum]

Num1-lt num2 less than [3-lt $mynum]

Num1-le num2 less than or equal to [3-le $mynum]

NUM1-GT num2 greater than [3-GT $mynum]

Num1-ge num2 greater than or equal to [3-ge $mynum]

Three Some of the Shell's considerations

3.1. Basic

The first sentence of the #!/bin/bash//bash script is this, and he will let the system specify that the script be interpreted with bash

#//shell Script Comment Symbols

3.2. Variable

Variables are assigned to "=" on both sides of the content to be close to "="

For example: sum=0, write Java write for a long time like written sum = 0, but the result is that the editor does not recognize

3.3. When using if, while, pay attention to the division between the variable and the symbol

For example: if [' Expr $a% 3 '-ne 0],while ["$var"! = "End"], as far as possible to write separately, so also pretty good also easy to identify.

3.4. Let means that the contents of the "=" are followed by arithmetic operations, for example: Lets X=a-b

3.5. In most cases, test commands can be used to test conditions, such as the ability to compare strings, determine whether a file exists and whether it is readable, etc... Usually "[]" to indicate the condition test, note that the space here is important, to ensure that the space before and after the square brackets.

[-F "somefile"]: Determine if it is a file

[-X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions

[-N ' $var]: Determine if the $var variable has a value

["$a" = "$b"]: Determine if $ A and $b are equal

3.6 Several simple examples

For filename in $ (LS)

Do

Cat $filename

Done

 

for ((i=0; i<10; i++)

Do

Echo $i

Done

X=1

Sum=0

While [$x-le 10]//note [] spaces on both sides

Do

Let sum=sum+ $x//shell arithmetic calculations using let

Let X=x+1

Done

Echo $sum

From http://blog.csdn.net/tianlesoftware/article/details/5953577

Shell Basic Syntax

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.