[Introduction to Basic/linux basics of Linux]shell programming

Source: Internet
Author: User
Tags case statement python script

declaring the execution program
#!/bin/bash

Used to tell the system to use the/bin/bash program to execute the script. For example, a Python script can be written like this:

#!/usr/bin/python
assignments and references

Assignment formula:

Variable name = value

When you take the value of a variable, you need to precede the variable name with the $ symbol, for example:

var="Hello bash"echo $var

If there is a concatenation, and there is no space for the delimiter, this time you need to use braces to enclose the variable, such as:

num=3echo"This is the${num}th"

The result is: the 3th


The shell defaults to string manipulation. So if you're going to calculate it, you can't use it that way.

var=1var= $var +1echo $var

The result is:

For numerical calculations, there are the following methods

Var=1 Let"var+=1"# Let to represent the mathematical operation var="$[$var +1]"# $[] means that the expression inside the parentheses is mathematically calculated as the (()) sense concept (Var++)) var=$ (($var +1)) var="$ (Expr"$var"+ 1)"# expr denotes integer operation var="' Expr"$var"+ 1 '"
If it is too messy, it is advisable to remember (($var + 1)). Just keep in mind that the two-parenthesis content will be counted. Process Control

If statement

if  Then   ... elif  Then   ... Else    ... fi


Some of the common judgments

 [-F  file_path   " " # to determine if a file exists [-x  file_path   ] # to determine whether a file has executable permissions [-N  "  $var   "  # to determine if the $var variable exists [  "  $var 1   =   $var 2  ] # determine if $var1 and $var2 are equal 

[] is used to represent a condition test. Note that spaces are required before and after parentheses.

&& | |
As with most other languages,&& and | | The meaning is almost the same.
&&: If the first condition is ture, check the second condition/execute the second statement;
|| : Checks the second condition/executes the second statement if the first condition is false;
So you can use it like this:

" ${file_path} " Echo " cannot read ${file_path} " 1; }

This means that if the file is unreadable, the error message is printed and exited.

Case statement
Case is an expression used to match a string, not a number.
Case expression

 Case inch ...)  ...;; Esac

Example

#!/bin/Bash Case " $" inch"1")  Echo 1;;"2")  Echo " Both";;*)  Echo "your input is $";;Esac



Cycle
While expression

 while  Do   ...  Done

for-expression

 for inch  Do   ...  Done

Both of these expressions are relatively simple, and there is nothing special to say. I found some differences between the different systems. I found that red Hat can use the following expressions, but Debian is not. I don't know whether the system version or the system itself is different.

 for (i=0;i++;i<  ...  Done
Shell Functions
function_name () {    function}

Functions need to be mastered in any language. The Shell's function only needs to be defined before it is called. As in C, but no need to declare, and the C language is still different.
You do not need to write formal parameters when defining a shell function. Call the time according to a space separated by a number of parameters passed on it. Then use the $, $, $ ... these parameters to invoke.
Here is an example of a simple recursive invocation:

#!/bin/bashrecursion () {Echo$1; if[" $"-eq"0"]; Then    Echo " Done"  Elserecursion $ (($1-1))  fi}Echo "Start"; recursion5;
Although the above is a very simple example, but explains how the function is called, how to pass the parameter.

[Getting Started with Linux]shell programming Basics/linux

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.