Getting Started with Shell scripting--on

Source: Internet
Author: User
Tags arithmetic

Hello everyone! Today's share is the basic knowledge of some shell scripts, chapter for the introduction of the content of this article are: variables (classification, assignment, query, reference), arithmetic operation implementation.

Introduction to shell Scripts  

There are two types of high-level computer languages, one is object-oriented (such as Java, C #), and one is process-oriented (such as Shell, Python). The difference in nature lies in the object-oriented trial of large and complex environment, can be more accurate and meticulous service delivery, the disadvantage is that the grammatical structure is cumbersome and rigorous. And the process-oriented language, simple and convenient to use, but not for the complex environment. For example: Now there is a large mechanical project, an experienced old designer, with several young engineers. The old designer was responsible for distributing the project to several engineers and informing them of the requirements, which the engineers operated to implement. The old designer is responsible for the division of Labor, the engineer is responsible for concrete implementation. The old designer communicates the requirements and ideas with the engineer, which is the object-oriented process. And the engineer receives the task that distributes, will go to concrete realization, this is the process-oriented.

The shell script we're going to cover next is a process-oriented one. Commonly used to simplify operations, to achieve a number of simple condition judgments, loops and so on

  1, variables: used to store some data, easy to manage

(1) Classification of variables

Local variables: Valid only for the current shell, not for child shells

Environment variable: Valid for both the current shell and its child shell.

Positional variables: The assignment of a parameter (2) variable that is passed to the script through the command line in the script code arname=value export varname Export varname=value declares an environment variable and assigns                          The value Declare-x Varname=value declares an environment variable and assigns a value (3) to the query of the local variable of the variable: set (that is, check local, also check environment) environment variable query:        Env printenv Export Declare-x (4) variable reference $varname         This method is commonly used to delimit the range of variables when the "$varname" ${varname} variable is followed by a character, when the variable worth contains a newline character, and if the value you want to reference also contains a newline character, you can only use "$varname" (5) to delete the variable unset varname 2. Positional variables: Invoke parameters passed to script through the command line in script code$, $, ...         : corresponding to the 1th and 2nd parameters, Shift [n] Position $: command itself $*: All parameters passed to the script, all parameters are combined as a string [email protected]: All parameters passed to the script, each argument is a separate string $#: The number of arguments passed to the script [email protected] $* will only have a difference set when it's wrapped up in double quotes--Empty all positional variables 3. Arithmetic operations in bash: Help- Let+,-, *,/,% modulus (take rest), * * (exponentiation) in BC the exponentiation is ^ implementation arithmetic: (1) Let var= arithmetic expression (2) var=$[arithmetic expression] (3) VA r=$ (arithmetic expression)) (4) var=$ (expr arg1 arg2 arg3 ...) when multiplying to translate *, i.e. \* (5) declare–i var = value (6) echo ' Arithmetic expression ' | Bc Practice:1. Write script/root/bin/, calculate the sum of the 10th user and 20th user ID in the/etc/passwd file [[email protected] bin] #vim #!/bin/bash u10= ' CA    T-n/etc/passwd |grep "^[[:space:]]\+10" |cut-d:-f3 ' u20= ' cat-n/etc/passwd |grep "^[[:space:]]\+20" |cut-d:-f3 ' sumid=$[$U $U] echo U10: $U echo U20: $U echo $sumid 2, write script/root/bin/, pass two file paths as parameters to the script, calculate the two files The sum of all blank lines in the [[email protected] bin] #vim #!/bin/bash sumspace01= ' cat $ |grep ^$ |wc-l ' sumspace02= ' Cat $ |grep ^$ |wc-l ' echo file01space01: $SumSpace echo file01space01: $SumSpace echo sumspace: ' echo $SumSpace 01+ $SumSpace |BC '

Getting Started with Shell scripting--on

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: 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.