Introduction to shell programming in Linux (2)

Source: Internet
Author: User

1. Create and run Shell programs
What is a shell program? Simply put, a shell program is a program that contains several rows.
Shell or Linux Command file.
Like programming in advanced languages, writing a shell program requires a text editor, such as VI.
In the text editing environment, enter shell/Linux Command lines according to shell syntax rules to form a complete
Program file.
There are three methods to execute shell program files
(1) # chmod + X file (in/etc/profile, add export path =$ {path }:~ /Yourpath, which can be directly run under the command line, just like executing a common command)
(2) # sh File
(3) #. File
(4) # source file
When writing a shell, the first line must specify the shell that the system needs to explain your shell program, such :#! /Bin/bash,
#! /Bin/CSH,/bin/tcsh, or #! /Bin/pdksh.
2. Variables in Shell
(1) Common System Variables
$ #: Number of command line parameters for saving programs
$? : Save the return code of the previous command.
$0: Save the program name
$ *: Saves all input command line parameters in the form of ("$1 $2 ...")
$ @: Saves all input command line parameters in the form of ("$1" $2 "...)
(2) define variables
The shell language is a non-type interpreted language. Unlike C ++/Java programming, variables must be declared in advance.
Variable values are actually defined variables.
In all the shells supported by Linux, you can assign values to variables using the value assignment symbol (=.
For example:
ABC = 9 (Bash/pdksh cannot leave spaces on both sides of the equal sign)
Set abc = 9 (tcsh/CSH)
Because shell program variables are non-typed, you can use the same variable to store characters and
Integer.
For example:
Name = ABC (Bash/pdksh)
Set Name = ABC (tcsh)
After assigning a value to a variable, you only need to add $ to reference the variable.
For example:
Echo $ ABC
(3) location variable
When you run a shell program that supports multiple command line parameters, the values of these variables are stored in the location variables respectively.
The first parameter is stored in location variable 1, the second parameter is stored in location variable 2, and so on..., shell retains
These variables do not allow users to define them in an out-of-order manner. The same variables are referenced with the $ symbol.

3. How to use quotation marks in Shell
Shell uses quotation marks (single quotes/double quotes) and backslash ("/") to shield some special characters from the shell interpreter.
Reverse quotation marks (") have special significance for shell.
For example:
ABC = "how are you" (Bash/pdksh)
Set abc = "how are you" (tcsh)
In this command line, how are you strings composed of three words are assigned to the variable ABC as a whole.
ABC1 = '@ LOGNAME, how are you! '(Bash/pdksh)
Set ABC1 = '$ LOGNAME, how are you! '(Tcsh)
Abc2 = "$ LOGNAME, how are you! "(Bash/pdksh)
Set abc2 = "$ LOGNAME, how are you! "(Tcsh)
The LOGNAME variable is the shell variable that saves the current user name. Assume that the current value is: Wang. After two commands are executed,
The content of ABC1 is: $ LOGNAME, how are you !. The content of abc2 is: Wang, how are you !.
Like single quotes, The backslash can also block all special characters. However, it can only block one character at a time.
A group of characters.
The anti-quotation mark function is different from the above three symbols. It does not have the function of shielding special characters. However, you can use
The running result of a command is passed to another command.
For example:
Contents = 'LS' (Bash/pdksh)
Set contents = 'LS' (tcsh)
4. Test command in shell Program
In bash/pdksh, the command test is used to calculate the value of a conditional expression.
The statement is used to determine whether certain conditions are met.
Syntax format of the test command:
Test expression
Or
[Expression]

Many internal shell operators can be used in the test command. These operators are described as follows:
(1) The string operator is used to calculate the string expression.
Test command | meaning
-----------------------------------------
Str1 = str2 | returns true if str1 and str2 are the same
Str1! = Str2 | returns true if str1 and str2 are different
STR | returns true if STR is not a null character.
-N str | returns true if the STR length is greater than 0.
-Z str | returns true if the STR length is 0.
-----------------------------------------
(2) integer operators have functions similar to character operators, but their operations are for integers.
Test expression | meaning
---------------------------------------------
Int1-EQ int2 | returns true if int1 is equal to int2
Int1-ge int2 | returns true if int1 is greater than/equal to int2
Int1-Le int2 | returns true if int1 is smaller than/equal to int2
Int1-GT int2 | returns true if int1 is greater than int2
Int1-ne int2 | returns true if int1 is not equal to int2
-----------------------------------------
(3) operators used for file operations. They can check whether a file exists and the file type.
Test expression | meaning
------------------------------------------------
-D file | returns true if file is a directory.
-F file | returns true if file is a normal file.
-R file | returns true if the file is an engraved file.
-S file | returns true if the file length is greater than 0.
-W file | returns true if the file is a writable file.
-X file | returns true if file is an executable file.
------------------------------------------------
(4) Shell logical operators are used to modify/connect expressions that contain integers, strings, and file operators.
Test expression | meaning
----------------------------------------------------------
! Expr | returns true if the value of expr is false.
Expr1-A expr2 | returns true if both expr1 and expr2 are true.
Expr1-O expr2 | returns true if at least one value of expr1 and expr2 is true.
-----------------------------------------------------------
Note:
Tcsh shell does not use the test command, but the expressions in tcsh can share the same functions. tcsh
The supported expressions are the same in C. They are usually used in the IF and while commands.
Tcsh expression | meaning
-------------------------------------------------------
Int1 <= int2 | returns true if int1 is smaller than/equal to int2
Int1> = int2 | returns true if int1 is greater than/equal to int2
Int1 <int2 | returns true if int1 is smaller than int2
Int1> int2 | if int1 is greater than int2, true is returned.
Str1 = str2 | returns true if str1 and str2 are the same
Str1! = Str2 | returns true if str1 and str2 are different
-R file | returns true if file is a readable file.
-W file | returns true if the file is a writable file.
-X file | returns true if file is an executable file.
-E file | returns true if the file exists.
-O file | returns true if the file owner is the current user.
-Z file | returns true if the file length is 0.
-F file | returns true if file is a normal file.
-D file | returns true if file is a directory.
Exp1 | exp2 | returns true if at least one of the values of exp1 and exp2 is true.
Exp1 & exp2 | returns true if the values of exp1 and exp2 are both true.
! Exp | returns true if the exp value is false.
-------------------------------------------------------

 

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.