Shell Scripting - The Basics

Shell script concept

1, the command to be executed in order to save a text file
2, executable permissions to the file, can run
3, may be combined to accomplish a variety of control statements shell more complex operations

Shell script scenarios

1, repetitive operations
2, batch transaction processing
3, automated operation and maintenance
4, service health monitoring
5, to perform regular tasks

Perfect shell scripts that make up

1, the script statement
2, executable statement
3, the comment text

Shell role - command interpreter, "translator"
Shell Scripting - The Basics

Execute the script file method Daquan

Method One: Script file path (absolute and relative paths)
Shell Scripting - The Basics
Method Two: sh script file path
Shell Scripting - The Basics
Method three: source script file path to
Shell Scripting - The Basics
expand four:

[root@localhost ~]#.  first.sh

Summary:
1, except for an ./first.sh allocation execution privileges to perform, the remaining command execution file allocation without, can be executed directly;
2.sh.first.sh after ./first.sh executable file, not change the current directory location

Interactive hardware

Standard Input: data from the user input device receives
output standard: output data to the user equipment through the
standard error: execution error report information through the apparatus
Shell Scripting - The Basics
1, a redirection operation
Shell Scripting - The Basics
2, the pipeline operator "|" symbol
to the left of the command output as a result, as the processed command on the right
Shell Scripting - The Basics
3, awk: identifying longitudinal
Shell Scripting - The Basics
drawing command conversion:

awk -F:  ‘{print $1,$7}’=awk -F":"  ‘{print $1,$7}’

The role of variables

1, to provide a flexible management system Linux specific parameters, has two meanings
variable name: name fixed, preset or user defined by the system
variable values: the user can be set varies depending on the system environment

变量的类型

1、自定义变量:由用户自己定义、修改和使用
2、环境变量:由系统维护,用于设置工作环境
3、位置变量:通过命令行给脚本程序传递参数
4、预定义变量:Bash中内置的一类变量,不能直接修改

定义一个新的变量
变量名以字母或下划线开头,区分大小写,建议全大写
Shell Scripting - The Basics
查看变量名
Shell Scripting - The Basics

赋值时使用引号
单引号:禁止引用其他变量值,$视为普通字符
双引号:允许通过$符号引用其他变量值
反撇号:命令替换,提取命令执行后的输出结果
从键盘输入的内容为变量赋值
Shell Scripting - The Basics

设置变量的作用范围

两种格式可以混合使用
Shell Scripting - The Basics
Shell Scripting - The Basics
整数变量的运算
Shell Scripting - The Basics

常用的运算符

加法:+
减法:-
乘法:*
除法:/
求模(取余):%

环境变量

1、由系统提前创建,用来设置用户的工作环境
配置文件:/etc/profile、~/.bash_profile
2、创建的环境变量
PWD、PATH
USER、HOME、SHELL

位置变量

1、表示为$n,n为1~9之间的数字

[root@localhost ~]# ./myprog.sh one two three four five six

./myprog.sh:$0(当前执行的进程名)
one:$1(第1个位置参数)
two:$2(第2个位置参数)
six:$6(第6个位置参数)

预定义变量

● $ #: position command variable number of rows
● $ *: All the contents of the location variable
● a command execution status return $ ?: later, when returning the state value of 0 indicates execution
line normal, non-zero value indicates abnormality or error
● $ 0: currently executing process / program name
Shell Scripting - The Basics

Guess you like

Origin blog.51cto.com/14475593/2440042