Bash Script programming
One, Bash script programming
Bash script programming (can be understood as a stack of commands)
Program: Contains statements and expressions (with judgments and loops)
Sequential execution
Select Execute
Loop execution
program = instruction + data
Understanding : Programming is divided into 2 types: process-oriented Programming: the organization of data with instruction; object-oriented programming: data-centric organization of directives
So script programming can be summed up as process-oriented programming.
Execution model of the program:
1, compiled execution: source code, preprocessing, compilation-----Link (compiler)
2, interpretation of implementation: source---(interpreter) through the interpreter execution
Note: The bash script interpreter bash
ii. Writing of bash scripts
Shebang (definition script specifies interpreter), generally saved as. sh,linux no strict suffix required
#!/bin/bash
#!/bin/bashcat/etc/shells Sleep #执行的时候, create a new child process, sleep wait, child process takes up the parent process to wait
Note: When you run the script, you write the absolute path. or write bash test.sh to temporarily grant permission
Bash-x test.sh Debug Run
[[email protected] tmp]# bash-x test.sh + cat/etc/shells #每个命令逐条显示, if there are errors will be prompted/bin/sh/bin/bash/sbin/nologin/bin/dash/ bin/tcsh/bin/csh+ sleep #这里是等待了20秒之后才继续执行下一条命令 + dasdtest.sh:line 4:dasd:command not found[[email protected] tmp]#
Third, variable-related knowledge
Definition of a variable: named memory space
Variable name and variable value, different view variables have different classification
1. Type of data (by content)
Strong typing: For example, C language, strict classification of types
Weak type: Bash scripting and so on, bash treats all values as characters by default
The role of data types
(1) Defines the data storage format
(2) Scope of data representation
(3) Different types of operations that can be involved
2. Type (divided by format)
Numeric type: http://blog.51cto.com/user_index.php?action=addblog_new&did=126489
Exact numeric type: integer
Approximate numeric type: float (single precision) double (double) assignments (storage format for floating-point numbers)
Character type: does not differentiate between a single character
Boolean type: true,false-->0,1
Several important logical operators:
logic and Operations: && a && b true for True, False for false
logic or Operation: | | a | | b is fake, true for true
logical non-op: ! ! Operation of a negation
four, bash variable type
(1) Local variables: Valid only for the current shell process, Not valid for shells outside the current shell (such as parent shell processes and child shell processes are not valid)
[[email protected] tmp]# Aa=3[[email protected] tmp]# echo $aa 3[[email protected] tmp]# bash[[email protected] tmp]# Echo $AA [[email protected] tmp]# aa=2[[email protected] tmp]# exitexit[[email protected] tmp]# echo $AA 3[[email protected] tmp] #
(2) Environment variables: Valid for the current shell process and its child shell processes
[Email protected] tmp]# export hello= "Hello1" [[email protected] tmp]# echo $HELLOhello 1[[email protected] tmp]# Bash[[em AIL protected] tmp]# echo $HELLOhello 1[[email protected] ~]# env | grep ' Hello ' hello=hello1[[email protected] ~]# unset Hello #取消环境变量的设置 (here only the sub-processes are in effect) [[email protected] ~]# env | grep ' HELLO ' [[email protected] ~]# echo $HELLO [[email protected] ~]# Exitexit[[email protected] ~]# echo $HELLOhello 1[[ema Il protected] ~]#
Note: env (printenv) displays the environment variables used in the current shell
[Email protected] tmp]# env #这里只列出部分环境变量, env=printenvhostname=localhost.localdomainterm=xtermshell=/bin/ bashhistsize=1000ssh_client=192.168.199.180 54823 22qtdir=/usr/lib64/qt-3.3qtinc=/usr/lib64/qt-3.3/includessh_ Tty=/dev/pts/1user=root
(3) Local variables: variables that are valid only for a section of code space, typically used in script functions
(4) Position variable: $,...
(5) Special variables: $? $ $# $$ $* [email protected]
V. References and names of variables
1, ${variable name}== variable reference
• Strong quote: ' echo ' root shell is $SHELL ' as-is output, cannot interpret $ symbol
• Weak references: "echo" root shell is $SHELL "output" root shell Is/bin/bash "
2. Naming rules for variables
1, can not use the key words in the program: if Elif fi then for ....
2. Use only numbers, letters, or underscores, and cannot start with a number
3, as far as possible to see the name of the idea
VI. Bash configuration files
1. Profileclass : Provides configuration for the interactive login shell process.
• Valid profiles for all users are globally valid:/etc/profile and/etc/profile.d/*.sh
• Only the profile that is valid for the current user is valid for the user: ~/bash_profile
Note: The function of profile type configuration file:
(1) To define environment variables
(2) run a command or script
2.BASHRC class : Provides configuration for the non-interactive login shell process.
• Globally valid:/ETC/BASHRC
• Valid for individual users: ~/.BASHRC
Note: The function of the BASHRC class configuration file:
(1) Defining local variables
(2) Defining command aliases (alias)
How to bring the modified bash profile back into effect: (2 methods)
source configuration file or . Configuration file
Priority of the configuration file in effect:
• Interactive login:/ETC/PROFILE->/ETC/PROFILE.D/*.SH->~/BASH_PROFILE->~/.BASHRC->/ETC/BASHRC
So defining this file allows all other files to take effect.
• Non-interactive login: ~/.bashrc->/etc/bashrc->/etc/profile.d/*.sh
This article is from the "Journeys By chance" blog, make sure to keep this source http://meaningful.blog.51cto.com/4543452/1631658
Getting Started with Shell scripting basics