Shell运维实战1-核心与数值计算

Shell 初步入门


Shell 分类

对于 Unix/Linux 两种系统,shell 主要由以下两种类型

Bourne shell 其下还包括子分支 Bourne shell(sh)、Korn shell(ksh)、Bourne Again Shell(bash)三种类型

C shell 又包括 csh、tcsh 两种类型

目前主要留学的是 csh 以及 bash


幻数

任意位置创建一个 sh 文件 s1.sh

写入以下代码

#! /bin/bash
echo tom

#! 叫做幻数,在其后面指出该文件使用的 shell 解释器
(对于大多数 linux 系统,目前都会默认使用 bash,但这一行还是不可以省略)

保存该文件,同目录下,使用 bash 指令运行,发现输出了 tom
运行代码 bash s1.sh


这是书中给出的常用 sh 开头写法

#! /bin/sh
#! /bin/bash
#! /usr/bin/awk
#! /bin/sed
#! /usr/bin/tcl
#! /usr/bin/expect      #<==expect解决交互式的语言开头解释器。
#! /usr/bin/perl        #<==perl语言解释器。
#! /usr/bin/env python  #<==python语言解释器。

注释

非常简单,使用一个 # 即可


Shell 核心与实践


变量

变量名加等号即可赋值

使用美刀符号输出变量值

#! /bin/bash

value="helloworld"

echo $value

终端模式下,可以使用以下三个命令获取对应作用域内的变量
set 命令输出所有的变量,包括全局变量和局部变量;
env 命令只显示全局变量;
declare 命令输出所有的变量、函数、整数和已经导出的变量


常见的系统环境变量
$HOME:用户登录时进入的目录。
$UID:当前用户的 UID(用户标识),相当于 id -u。
$PWD:当前工作目录的绝对路径名。
$SHELL:当前 SHELL。
$USER:当前用户。


引号输出

a=123 不加引号直接赋值,值被解析后输出

a='123' 单引号,不作任何解析,有什么就输出什么

a="123" 双引号,引号里的变量及命令会经过解析后再输出内容


特殊变量

$0 取出当前执行脚本的文件完整名称

$# 取出脚本传参的个数

$*以及$@ 均为输出所有传入参数的值,但是会根据变量有无单双引号而呈现不同的解释状态


特殊状态变量

$? 获取执行上一个指令执行状态的返回值
$$ 获取当前 shell 脚本进程号 PID
$! 获取上一个于后台工作进程的进程号
$_ 获取在此之前执行命令的脚本的最后一个参数


bash 内置变量命令

eval 在当前代码执行位置插入新的指令并且执行他
eval "echo $0"

exec 不创建新的子进程的情况下执行对应的指令,执行完毕进程终止
exec data


read 从标准输入读取字符串信息

exit 退出 shell 或者转到下一个数位


变量子串

在这里插入图片描述

即在一个变量表达式内,使用对应的变量子串符号来达到快速操作字符串的效果

指令实例 ${#name}


特殊扩展变量

在这里插入图片描述

使用特殊扩展变量方法,为未初始化或者未赋值的变量在运行时显示指定内容

如下代码,由于 res 变量未定义,我们通过 :- 设置了当变量未初始化时输出的内容

echo $test # 没有定义的变量直接调用就是0
res=${test:-UNDEFINED} # 定义未定义的变量的方法
echo $res # UNDEFINED

Shell 变量数值计算实践

基本算术运算符

在这里插入图片描述


双小括号

在这里插入图片描述

双小括号可以作为一个简单的表达式使用,进行赋值或者回显操作时务必带上¥符号,否则将会出错
echo $((100+200))

表达式在命令行执行时不需要加 符号,直接使用 ( ( 6 符号,直接使用((6%2))形式即可,但是如果需要输出,就要加 符号,直接使用((6

“(())”里的所有字符之间没有空格、有一个或多个空格都不会影响结果


let

let 相当于双小括号,你可以这么写

let i=i+1


expr

可用于求职或者表达式运算

乘法运算要加上反斜杠进行转义

expr 2 + 2 # 4
expr 2 \* 2 # 4

使用反引号包裹 expr,使其以一个表达式的形式出现

i=5
i=`expr $i + 6`
echo $i # 11

利用 expr 做计算,将一个未知的变量和一个已知的整数相加,看返回值是否为 0,如果为 0 就认为做加法的变量为整数,否则就不是整数

可以使用 length 参数来计算对应字符串的长度

str="asd"
expr length $str # 3


bc

bc 是一款 linux 自带的计算器,我们也可将其用在命令行内

echo 3+4|bc # 7

i=10
i=`echo $i+10|bc` # 20

awk

适用于小数加减法

echo "7.7 8.2" |awk '{print ($1-$2)}' # -0.5

read

read 可以读取用户输入

# -t 10 设置等待时间为10s
# -p "please input:"输入提示词
# num输入内容被赋予的变量
read -t 10 -p "please input:" num

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zhillery

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值