Shell script (i)

Source: Internet
Author: User

Unlike a compiled language, shell scripting is an interpreted language.

When executing such a program, The interpreter (interpreter) needs to read the source code that we wrote and convert it to the target (object), which is then run by the computer.

The default script parser for Linux is bash.

Reference http://c.biancheng.net/cpp/view/6994.html

First shell script

The following shows our first shell script, using a text editor (such as VI), type the following code and save as Hello.sh

#!/bin/"Hello world! " "

Then give the script execute permission

chmod +x./hello.sh

Then execute the script and the terminal will show Hello world!

Where the first line of the script #!/bin/bash indicates the interpreter for the script

Read command

The following script uses the read command to get input from stdin and assigns a value to the person variable, and finally outputs on stdout

#!/bin/"What isyour name? " "  "Hello, $PERSON"

Here are the operating conditions

Shell variables

Defining variables

Variablename="value"

With a defined variable, just precede the variable name with a dollar sign ($), such as:

Name="runnyu"Echo ${name}

Curly braces can be added without adding curly braces to help the interpreter identify the bounds of the variable. Variables can be defined as read-only variables using the readonly+ variable name, unset+ variable names can be deleted

ReadOnly Nameunset name

Special variables

Writing special.sh Tests

#!/bin/"pid=$$ " "$""  $ "  "$" "$#  "  "$*"

Run results

$* and [email protected] All represent all parameters passed to a function or script, and are not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n".

But when they are enclosed in double quotation marks (""), "$*" takes all the parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the various parameters to "$" "$" ... All parameters are output in the form "$n".

Shell script (i)

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.