Linux05--shell Programming 01

Source: Internet
Author: User
Tags builtin

1.Shell Script Introduction

Basic Introduction:

A shell script is an executable plain text file that consists of multiple shell commands.

Command execution is from top to bottom, from left to right analysis and execution

command, multiple whitespace between parameters is also ignored

# is a comment

#! Used to specify the shell name used, such as bash CSH, etc.

Shell scripts typically use. sh as the suffix name.

Internal commands and External commands

Internal command: The shell is done by executing the corresponding section in its own code.

External commands: Some binary executables or shell scripts

2. Use the Type command to see the type of command

[[Email protected] ~]$ type [-TPA] name option with parameter:    : When no options and arguments are added, type shows whether the name is an external directive or bash built-in directive-T: When the-t parameter is added, the type will name the bottom These words show what he means:      file: Represented as an external instruction,      alias: Indicates the name set by the command alias,      Builtin: Indicates that the directive is a function of the commands built into bash, and-P: If the name that follows is an external directive, The full file name is displayed;-A: The path defined by the PATH variable lists all instructions with the name, including the alias example one: Query ls is the command for bash built?  [[Email protected] ~]$ type ls ls aliased to ' ls--color=auto ' <== without any parameters, list the main use of LS [[email protected] ~]$ type-t ls alias                               < = only lists the basis of LS execution [[email protected] ~]$ type-a ls ls is aliased to ' LS--color=auto ' <== first use aliase ls Is/usr/bin/ls                   <= = There are also external instructions found in/bin/ls example two: So what about CDs? [[Email protected] ~]$ Type cd CD is a shell builtin               <== see? CD is a shell built-in directive

3. Variable Bash shell variable function

The variable is an important concept and function in the bash shell, similar to a variable in the C/java language. The variable-related action command that needs to be mastered:

1. Display and setting of variables: Echo, unset

Output variable value: Echo $PATH

The syntax for setting a variable is: a=b

The syntax for canceling a variable is: unset a

2. Related actions of environment variables: env, export

To view environment variables using the ENV command

? Use the SET command to view all variables: including environment variables and custom variables

3. The main difference between environment variables and custom variables is scope differences.

? environment variables are valid throughout the bash run, and custom variables are only valid in the current process. The child process inherits the environment variables of the parent process and does not inherit the custom variables.? Use the Export command to convert a custom variable to an environment variable.

3. Input of variables: Read

Reading variable values from the keyboard

? Use the Read command to read variable values from the keyboard, and user interaction, often used in shell script. The read syntax is:
[[email protected] ~]$ read [-pt] variable options and Parameters:-P: Can be followed by the prompt character! -T: The number of seconds to wait after! "This is more fun ~ not always waiting for the user!" Example one: Let the user input a content by the keyboard, the content into a variable named atest [[email protected] ~]$ Read Atest         This is a test <== at this time the cursor will wait for you to enter! Please enter the text on the left to see [[email protected] ~]$ echo ${atest} This is a test           <== you just entered the information has become a variable content! Example two: Prompt the user to enter their own name in 30 seconds, the input string as a variable named named content [[email protected] ~]$ read-p "Please keyin your name:"-T-named please key In your Name:vbird Tsai    <== Look, there will be prompt characters Oh! [[email protected] ~]$ echo ${named} vbird Tsai         <== input data becomes a variable content again!

  

4. Arrays and declarations: Declare/typeset,

[[email protected] ~]$ declare [-AIXR] variable options and Parameters:-A: The variable with the following name variable is defined as an array (array) type-I: The variable named variable later is defined as an integer number ( Integer) Type-x: Use the same way as export, which is to turn the variable into an environment variable;-r: Sets the variable to the readonly type, which cannot be changed or unset example one: Make the variable sum 100+300+ 50 total results [[email protected] ~]$ sum=100+300+50 [[email protected] ~]$ echo ${sum} 100+300+50   <== Hey! Why didn't you help me calculate the sum? Because this is the variable attribute of the literal type! [Email protected] ~]$ declare-i sum=100+300+50 [[email protected] ~]$ echo ${sum} 450          

  

Linux05--shell Programming 01

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.