Shell Script notes (i) Concepts and variables

Source: Internet
Author: User
Tags arithmetic

Concepts and Variables I. Understanding shell scripts

The shell is a command interpreter that executes the commands and programs that the user enters in order (the command that encounters the sub-script, which executes the sub-script first) .

The Shell scripting language is a weakly typed language, and the advantage over other scripting languages is that it can handle the underlying business of a biased operating system.

To view the system default shell:

#方法一: Echo $SHELL # Method Two: grep root/etc/passwd

Development specification:

1) The first line of the script specifies the interpreter

#!/bin/bash  

2) Beginning to add version, copyright and other information

3) Try not to use Chinese in the script

4) extension. sh

5) script should be placed under fixed path

Execution method:

1) Bash Script-name or sh script-name: This method is also a common method if the script does not have X permissions or if the script does not specify an interpreter at the beginning.

2) Path/script-name: Execute script Under current path, script must have x permission.

3) source Script-name or. Script-name: Run the script directly in the current shell (other way to open the new shell), so you can pass the variables or functions in the script to the current shell.

second, shell variables

Shell variables are not type-sensitive and can be declare defined if you want to specify a variable type.

a) environment variables

1) All environment variables are system global variables that can be used in all child processes.

2) The environment variable is exported with export, but if you do not write to the specified configuration file (~/.bash_profile,~/.bash,/etc/bashrc,/etc/profile), the exit command line is lost.

3) The environment variable should use uppercase.

To display environment variables:

Set: Outputs all variables, including global variables, local variables.

ENV: Displays environment variables.

DECLARE: All variables, functions, integers, etc.

Set-o: All parameter configuration information.

unset eliminate local variables and environment variables:

unset java_home

System environment File read process:

1. The process of logging in through the system User:

2. Non-login shell

will only load ~/.BASHRC or/ETC/BASHRC

II) Common variables

Defining local Variables

1 var_name1=12345                           #为连续内容无需解析其他变量时可不用引号 2 var_name2='do have time. '              #用单引号, Output 3 var_name3="No, I ' m working. "               #会对其中变量进行解析, other identical single quotes 4 var_name4= 'ls -alh '                       #获取命令结果 5 var_name5=$ (ls -l)                        #同上
III) special positional parameter variables

Common Special Position parameter description:

Special state variables in the process:

Usage:

1) Determine if the commands, scripts, and functions are executed successfully

2) Execute "exit number" and return the number to $?

3) in function "return number", the effect is as above

FOUR) Bash shell system built-in variable command:

1) Echo: Output variable command format on screen echo args where arges can be a combination of variables and strings

2) Eval:

Command format: eval args

Function: When the shell program executes to the Eval statement, the shell reads in the parameter args and combines them into a new command.

3) Exec:

Command format: EXEC command parameters

Function: If you do not create a new child process, go to execute the specified command, and specify that the command is finished to terminate the process.

4) Read:

Command format: read [parameter] [variable name] (the variable cannot be a special variable)
Function: Reads a string from a standard input and passes it to a variable defined inside the shell.

1 Ten '  ' num

5) Shilft:

Command format: shilft-shilft positional parameters

Function: Each time the SHILFT statement is used in a program, all positional parameters are moved to the left one position.

6) Exit:

To exit the shell, you can select a number as the return value.

V) Variable Quantum string

VI) Special extension variables

Third, numerical calculation of a variable a) arithmetic operator

* *: Power arithmetic other slightly

II) arithmetic Operations command

Shell Script notes (i) Concepts and variables

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.