首页 > 系统相关 >7-shell脚本编程

7-shell脚本编程

时间:2022-10-16 21:31:11浏览次数:33  
标签:脚本 shell 编程 call offset var rocky86 root bash

内容

  • 脚本基础格式
  • 变量
  • 运算
  • 条件测试
  • 配置用户环境
  • 流程控制
  • 函数
  • 脚本工具
  • 数组
  • 字符串处理
  • 高级变量

--------------------------------正文------------------------------------------

  • 脚本基础格式
  • 创建过程
  • 1. 用编辑器创建新文件,首行必须是 shell 声明(shebang),编辑完成后保存退出
  • 2. 添加可执行权限
  • 3. 运行脚本
  • shebang机制
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
#!/usr/bin/ruby
#!/usr/bin/lua

7-shell脚本编程_数组

  • 配置vim自动生成注释

7-shell脚本编程_字符串_02

autocmd BufNewFile *.sh exec ":call SetTitle()"

func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#")
call setline(3,"#****************************************************")
call setline(4,"#Date: ".strftime("%Y-%m-%d"))
call setline(5,"#FileName: ".expand("%"))
call setline(6,"#Description: func")
call setline(7,"#Copyright(C): ".strftime("%Y")." All right")
call setline(8,"#***************************************************")
call setline(9,"")
call setline(10,"")
call setline(11,"")
endif
endfunc

7-shell脚本编程_字符串_03

  • 执行本地脚本

7-shell脚本编程_数组_04

7-shell脚本编程_数组_05

  • 本地执行远程脚本

7-shell脚本编程_bash_06

7-shell脚本编程_bash_07

  • 脚本自动备份

7-shell脚本编程_字符串_08

  • 脚本调试
  • bash -n file
  • 检测脚本中的语法错误,不能检测命令错误,也不会执行脚本

7-shell脚本编程_bash_09

  • bash -x file
  • 逐行输出命令,并输出执行结果

7-shell脚本编程_bash_10

  • 调试思路
  • 语法错误:会导致后续的命令不继续执行,可以用 bash -n 检查错误,提示的出错行数不一定是准确的
  • 命令错误:默认后续的命令还会继续执行,用 bash -n 无法检查出来 ,可以使用 bash -x 进行观察
  • 逻辑错误:只能使用 bash -x 进行观察
  • 变量
  • 赋值
name='root' #直接字串
name="$USER" #变量引用
name=`COMMAND` #命令引用
name=$(COMMAND) #命令引用
  • 引用
$name
${name}

"$name" 弱引用,其中的变量引用会被替换为变量值
'$name' 强引用,其中的变量引用不会被替换为变量值,而保持原字符串
  • 例子

7-shell脚本编程_bash_11

7-shell脚本编程_bash_12

  • 保留命令执行格式

7-shell脚本编程_bash_13

7-shell脚本编程_字符串_14

7-shell脚本编程_字符串_15

  • 变量追加赋值

7-shell脚本编程_数组_16

  • 当前进程和子进程
[root@rocky86 ~]# vim test10.sh
#!/bin/bash

VAR1=123
VAR2=456
echo "test"
echo $BASHPID
sleep 20
[root@rocky86 ~]# chmod +x test10.sh

#当前进程中执行
[root@rocky86 ~]# echo $BASHPID
2570
#test10.sh中输出的 BASHPID 也是 2570
[root@rocky86 ~]# . test10.sh
test
2570
#执行完成后,当前进程中能使用 test10.sh 中定义的变量
[root@rocky86 ~]# echo $VAR1 $VAR2
123 456
#进程树中查看
[root@rocky86 ~]# pstree -p | grep 2570
|-sshd(1009)-+-sshd(2537)---sshd(2562)---bash(2570)---sleep(9403)
[root@rocky86 ~]# echo $BASHPID
2570
#进程ID不一样
[root@rocky86 ~]# ./test10.sh
test
9501
#子进程中的变量己经销毁了
[root@rocky86 ~]# echo $VAR1 $VAR2
[root@rocky86 ~]#
#进程树中查看
[root@rocky86 ~]# pstree -p | grep 9501
|-sshd(1009)-+-sshd(2537)---sshd(2562)---bash(2570)---

#子进程中执行
[root@rocky86 ~]# echo $BASHPID
2570
#进程ID不一样
[root@rocky86 ~]# ./test10.sh
test
9501
#子进程中的变量己经销毁了
[root@rocky86 ~]# echo $VAR1 $VAR2
[root@rocky86 ~]#
#进程树中查看
[root@rocky86 ~]# pstree -p | grep 9501
|-sshd(1009)-+-sshd(2537)---sshd(2562)---bash(2570)---test10.sh(9501)---sleep(9502)
  • 位置变量
#位置变量:在bash shell中内置的变量
在脚本代码中调用通过命令行传递给脚本的参数

$1,$2,... #对应第1个、第2个等参数,shift [n]换位置
$0 #命令本身,包括路径
$* #传递给脚本的所有参数,全部参数合为一个字符串
$@ #传递给脚本的所有参数,每个参数为独立字符串
$# #传递给脚本的参数的个数
#注意:$@ $* 只在被双引号包起来的时候才会有差异

7-shell脚本编程_数组_17

  • set脚本出现错误时处理
  • set -u
  • 在扩展一个没有设置的变量时,显示错误信息, 等同set -o nounset
  • set -e
  • 如果一个命令返回一个非0退出状态值(失败)就退出, 等同set -o errexit
  • set -o
  • option 显示,打开或者关闭选项 显示选项:set -o 打开选项:set -o 选项 关闭选项:set +o 选项
  • set -x
  • 当执行命令时,打印命令及其参数,类似 bash -x
  • 范例

7-shell脚本编程_数组_18

报错则终止执行

7-shell脚本编程_bash_19

7-shell脚本编程_字符串_20

  • 算数运算
#公式
let var=expression
((var=expression))
var=$[expression]
var=$((expression))
var=$(expr arg1 arg2 ...)
declare -i var=number
echo expression|bc

7-shell脚本编程_数组_21

  • read
  • 使用read来把输入值分配给一个或多个shell变量
  • 范例

7-shell脚本编程_字符串_22

7-shell脚本编程_bash_23

判断用户输入内容

7-shell脚本编程_bash_24

7-shell脚本编程_字符串_25

  • bash shell 配置文件
  • 全局
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
  • 特定用户
~/.bash_profile
~/.bashrc
  • 配置文件的执行顺序
#放在每个文件最前
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
~/ .bash_ profile
~/ .bashrc
/etc/bashrc #此文件执行两次

#放在每个文件最后
/etc/profile.d/*.sh
/etc/bashrc
/etc/profile
/etc/bashrc #此文件执行两次
~/.bashrc
~/.bash_profile

ps:修改profile和bashrc文件后需要重新加载才能生效(重启shell或source重新加载文件)

  • Bash 退出任务
  • 保存在 ~/.bash_logout用户文件中
  • 作用
  • 创建自动备份
  • 清除临时文件
  • 流程控制
  • if判断

7-shell脚本编程_bash_26

7-shell脚本编程_数组_27

  • case判断

7-shell脚本编程_数组_28

7-shell脚本编程_字符串_29

  • 循环

计算100之内自然数的累加

7-shell脚本编程_数组_30

  • 乘法表

方式一

7-shell脚本编程_数组_31

方式二

7-shell脚本编程_字符串_32

加随机彩色

7-shell脚本编程_数组_33

7-shell脚本编程_数组_34

倒序

7-shell脚本编程_数组_35

  • 括号写法

7-shell脚本编程_数组_36

7-shell脚本编程_字符串_37

  • while

7-shell脚本编程_bash_38

while read 读取文件每一行

7-shell脚本编程_bash_39

7-shell脚本编程_数组_40

  • shift 缺省

7-shell脚本编程_bash_41

  • select 无限循环菜单

7-shell脚本编程_bash_42

7-shell脚本编程_数组_43


  • function
  • 使用函数文件
  • 创建函数文件,只存放函数的定义
  • 在shell脚本或交互式shell中加载函数文件
  • 调用函数

7-shell脚本编程_数组_44

7-shell脚本编程_bash_45

  • 数组
  • 普通数组 declare -a ARRAY_NAME
  • 关联数组 declare -A ARRAY_NAME 关联数组必须先声明,再使用

范例:

生成10个随机数保存于数组中,并找出其最大值和最小值

7-shell脚本编程_bash_46

  • 字符串截取
${#var} #返回字符串变量var的字符的长度,一个汉字算一个字符

${var:offset} #返回字符串变量var中从第offset个字符后(不包括第offset个字
符)的字符开始,到最后的部分,offset的取值在0 到 ${#var}-1 之间(bash4.2后,允许为负值)

${var:offset:number} #返回字符串变量var中从第offset个字符后(不包括第offset个字
符)的字符开始,长度为number的部分

${var: -length} #取字符串的最右侧几个字符, 注意:冒号后必须有一空白字符

${var:offset:-length} #从最左侧跳过offset字符,一直向右取到距离最右侧lengh个字符之
前的内容,即:掐头去尾

${var: -length:-offset} #先从最右侧向左取到length个字符开始,再向右取到距离最右侧
offset个字符之间的内容,注意:-length前空格,并且length必须大于offset

标签:脚本,shell,编程,call,offset,var,rocky86,root,bash
From: https://blog.51cto.com/u_15791168/5760566

相关文章