Shell Learning (ii) Getting started to a complex script instance (Calculator)

Source: Internet
Author: User
Tags comments

Shell file Start

The shell file must start with the following line (must be on the first line of the file):

#!/bin/sh

The symbolic #! is used to tell the system that the parameters behind it are the programs used to execute the file. In this example we use/BIN/SH to execute the program.

When you edit a good script, you must also make it executable if you want to execute the script.

To make the script executable:

Run chmod +x filename This atch can be used./filename to run

Comments

In shell programming, comments are expressed in sentences that begin with # until the end of the line. We sincerely recommend that you use annotations in your programs.

Variable

In shell programming, all variables are composed of strings, and you do not need to declare the variables, direct assignment can be, the use of variables, the form of $+ variable names.

To assign a value to a variable, you can write this:

A= "Hello World"

Now print the contents of variable a:

echo "A is:"

Echo $a

Sometimes variable names can easily be confused with other words, such as:

num=2

echo "This is the $NUMND"

This does not print out "This is the 2nd" and only prints "This is the" because the shell searches for the value of the variable NUMND.

But this variable has no value at all. You can use curly braces to tell the shell what we want to print is the NUM variable:

num=2

echo "This is the ${num}nd"

This will print: This is the 2nd

Environment variables

The variables processed by the EXPORT keyword are called environment variables. We do not discuss environment variables, as we typically simply log in

Use environment variables in scripts.
These are not system commands, but they are really important.

Pipe (|) : The output of one command as input to another command.

grep "Hello" file.txt | Wc-l

Searches the file.txt for a row containing "Hello" and calculates its number of rows.

Here the output of the grep command is used as input to the WC command. Of course you can use more than one command.

Redirect: Outputs the results of a command to a file instead of the standard output (screen).

> Write files and overwrite old files

>> add to the end of the file and keep the old file contents.

Anti-Short Slash

Using a backslash ("'") you can use the output of one command as a command-line argument for another command.

Numeric variables:

Expr variable 1 operator variable 2 [operator variable 3]

+ addition operation

-Subtraction operation

\* multiplication operation

% the remainder after dividing the modulo operation

Predefined variables are a class of special variables that are predefined by the bash program, which users can only use without creating new predefined variables or assigning values directly to predefined variables. Common predefined variables are as follows:

$ #表示命令行中位置参数的个数

$* represents the contents of all positional parameters

$? Represents the return state after the previous command was executed, and a return of 0 indicates that the execution was correct, and any non-0 indicates that an exception was run

$ $ Indicates the currently executing script or program name

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.