Shell Programming - programming specification variable (1)

Shell Script Overview

The concept of shell scripts:

  • The command to be executed in order to save a text file

  • Executable permissions to the file, you can run

  • It may be combined to accomplish a variety of control statements shell more complex operations

shell script scenarios:

  • Repetitive operations

  • Batch Transactions

  • Automated operation and maintenance

  • Service health monitoring

  • Timing task execution

  • ...

The role of shell - command interpreter, "translator"

  • Interposed between the kernel and user, is responsible for interpreting the command line

QQ screenshot 20190921141926.png

The user's login shell

  • After logging in with the default shell program, usually / bin / bash

  • Different internal shell commands, operating environment, etc. will be different

QQ screenshot 20190921141926.png

Shell first to write a script

Write script code

  • Use a text editor vim

  • Linux commands, one per line, in order of execution in order to prepare

[Root @ localhost ~] # vim first.sh first script editor
#! / bin / bash shell declaration 
#first test comment 
cd / boot / command line 
pwd 
LS -LH VML *

Given executable permissions

  • Making your script executable property

chmod + x first.sh performed imparting properties

Execute the script file

It requires execute permissions to perform, but does not change position

./first.sh 

Execute permission is not required, but to change the position

. first.sh 

source first.sh 

You do not need to execute permissions do not change position

sh first.sh 

Better script constitution

  • Script statements

  • Comment Information

  • Executable statement

! # / bin / bash 
#First the Test 
cd / the Boot / 
echo "current directory is:" 
pwd 
echo "of which the beginning of the file vml include:" 
LS -LH vml *

Redirection and pipeline operations

Interactive hardware

标准输入:从该设备接收用户输入的数据

标准输出:通过该设备向用户输出数据

标准错误:通过该设备报告执行出错信息

类型 设备文件 文件描述编号 默认设备
标准输入 /dev/stdin 0

键盘

标准输出 /dev/stdout 1 显示器
标准错误输出 /dev/stderr 2 显示器

重定向操作

类型 作符 用途
重定向输入 < 从指定的文件读取数据,而不是从键盘输入
重定向输出 > 将输出结果保存到指定的文件(覆盖原有内容)
>> 将输出结果追加到指定文件
标准错误输出 2> 将错误信息保存到指定温家安(覆盖原有内容)
2>> 将错误信息追加到指定文件中
混合输出 &> 将标准输出,标准错误的内容保存到同一个文件中

管道操作符号“|”

  • 将左侧的命令输出结果,作为右侧命令的出来对象

QQ screenshot 20190921141926.png

QQ screenshot 20190921141926.png

shell变量的作用,类型

变量的作用

为灵活管理Linux系统提供特定参数,有两层意思

  • 变量名:使用固定的名称,由系统预设或用户定义

  • 变量值:能够根据用户设置,系统环境的变化而变化

变量的类型

  • 自定义变量:由用户自己定义,修改和使用

  • 环境变量:有系统维护,用于设置工作环境

  • 位置变量:通过命令行给脚本程序传递参数

  • 预定义变量:Bash中内置的一类变量,不能直接修改

自定义的变量

定义一个新的变量

  • 变量名以字母或下划线开头,区分大小写,建议全大写

变量名=变量值

查看变量的值

echo $变量名

QQ screenshot 20190921141926.png

赋值时使用引号

  • 双引号:允许通过$符号引用其他变量值

  • 单引号:禁止引用其他变量值,$视为普通字符

  • 反撇号:命令替换,提取命令执行后的输出结果

从键盘输入内容为变量赋值

read [-p "提示信息"] 变量名

QQ screenshot 20190921141926.png

设置变量的作用范围

格式1:export 变量名...
格式2:export 变量名=变量值...

——两种格式可以混合使用

QQ screenshot 20190921141926.png

整数变量的运算

expr 变量1 运算符 变量2 [运算符 变量3]...

常用的运算符

  • 加法运算:+

  • 减法运算:-

  • 乘法运算:\*

  • 除法运算:/

  • 求模(取余)运算:%

小脚本实例:输入两个数字,并求出他们的和

#!/bin/bash
#输入两个整数,求他们的和
#输入第一个数
read -p "请输入一个整数" num1
#输入第二个数
read -p "请输入一个整数" num2
#进行加法运算
sum=`expr $num1 + $num2`
#显示出两个数之和
echo "两个数字之和是:$sum"

QQ screenshot 20190921141926.png


特殊的shell变量

环境变量

  • 由系统提前创建,用来设置用户的工作环境

  • 配置文件:/etc/profile , ~/.bash_profile

常见的环境变量

  • PWD,PATH

  • USER,SHELL,HOME

QQ screenshot 20190921141926.png

位置变量

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

#!/bin/bash
echo "求和为:`expr $1 + $2`"

QQ screenshot 20190921141926.png

预定义变量

  • $#:命令行中位置变量的个数

  • $*:所有位置变量的内容

  • $?:上一条命令执行后返回的状态,当返回状态值为0时表示执行正常,非0值表示执行异常或出错

  • $0:当前执行的进程/程序号

! # / bin / bash 
echo "sum is:` expr $ 1 + $ 2` " 
echo" Script Name: $ 0 " 
echo" command the number of variables line: $ # " 
echo" the contents of all position variables: $ * "

QQ screenshot 20190921141926.png

Guess you like

Origin blog.51cto.com/14080162/2439960