29、shell变量、重定向及运算符

一、shell脚本

1.1、shell的定义

脚本:可运行的代码的集合,脚本语言(计算机语言)。

脚本的特点:从上到下,按行执行。

python:脚本语言 格式更严谨的执行缩进。也是从上到下按行执行。

shell脚本是在shell环境(/bin/bash)bash就是shell的解释器。linux环境下的编程语句。

----自动化运维,可重复执行的,自动化的在系统中实现增删改查的操作。

shell的解释器,默认的就是bash(cd翻译成计算机识别的语言)

sh:已经淘汰

bash:sh的扩展版

csh:类c语言

tcsh:整合了c语言的shell

nologin:用户无法登录到系统的shell

1.2、shell的作用:

1、自动化运维。

2、批量化的重复操作,以及配合定时任务执行。

3、有了脚本就可以减轻管理员的工作量。

4、避免配置出错。

5、提供处理批量文件的速度。

1.3、shell的构成:

[root@localhost ~]# vim test.sh

#!/bin/bash
#声明shell的解释器,这一行可以不写,默认就是bash ./python expect
,只要不是默认shell,其他的都要声明解释器
#被注释掉的部分是不会被执行的,步骤的含义。
#可执行语句:
#this is my first shell
cd /boot
echo "当前目录的位置"
#echo的作用就是打印
pwd
echo "展示其中以vml开头的文件:"
ls -lh vml*

第一行:声明解释器(默认就是bash,可以不写)

注释:以#开头,就是注释的信息,注释的部分不会被执行。

可执行语句:linux的命令都可以作为可执行语句,一行一行往下写。一行就是一个可执行的语句。

1.4、shell的执行方式:

3种执行方式:

1、不给脚本执行权限的执行方式:

sh +脚本:就是在当前的shell里产生一个子shell,运行的结果不改变父shell的环境。

[root@localhost ~]# sh test.sh
当前目录的位置
/boot
展示其中以vml开头的文件:
-rwxr-xr-x. 1 root root 6.4M 5月  15 22:34 vmlinuz-0-rescue-f40500d9daf845a7aac3e242fa92069c
-rwxr-xr-x. 1 root root 6.4M 11月  9 2018 vmlinuz-3.10.0-957.el7.x86_64
[root@localhost ~]#

2、bash test.sh:就是在当前的shell里产生一个子shell,运行的结果不改变父shell的环境。

[root@localhost ~]# bash test.sh
当前目录的位置
/boot
展示其中以vml开头的文件:
-rwxr-xr-x. 1 root root 6.4M 5月  15 22:34 vmlinuz-0-rescue-f40500d9daf845a7aac3e242fa92069c
-rwxr-xr-x. 1 root root 6.4M 11月  9 2018 vmlinuz-3.10.0-957.el7.x86_64
[root@localhost ~]#

3、source:点命令,他会改变当前shell的环境,也就是真正的执行代码。

[root@localhost ~]# source test.sh
当前目录的位置
/boot
展示其中以vml开头的文件:
-rwxr-xr-x. 1 root root 6.4M 5月  15 22:34 vmlinuz-0-rescue-f40500d9daf845a7aac3e242fa92069c
-rwxr-xr-x. 1 root root 6.4M 11月  9 2018 vmlinuz-3.10.0-957.el7.x86_64
[root@localhost boot]#

第二种:

脚本调试完,都是赋权执行。

[root@localhost ~]# ./test.sh
-bash: ./test.sh: 权限不够
[root@localhost ~]# chmod 777 test.sh
[root@localhost ~]# ./test.sh
当前目录的位置
/boot
展示其中以vml开头的文件:
-rwxr-xr-x. 1 root root 6.4M 5月  15 22:34 vmlinuz-0-rescue-f40500d9daf845a7aac3e242fa92069c
-rwxr-xr-x. 1 root root 6.4M 11月  9 2018 vmlinuz-3.10.0-957.el7.x86_64

二、重定向操作:

1、标准输入:ls

2、标准输出:

[root@localhost ~]# ls
test.sh  公共  模板  视频  图片  文档  下载  音乐  桌面

3、标准错误输出:

[root@localhost ~]# lsllsll
bash: lsllsll: 未找到命令...

4、重定向输出:

‘>’ 标准输出的结果保存到指定的文件,而且会覆盖文件原有的内容

[root@localhost ~]# ls > test1
[root@localhost ~]# cat test1
test1
test.sh

‘>>'标准输出的结果保存到指定的文件,原有的内容尾部追加内容,不会覆盖原有的内容。

[root@localhost ~]# ls >> test1
[root@localhost ~]# cat test1
test1
test.sh
test1
test.sh

5、2> 2>>标准错误输出的结果,保存到指定的文件。

[root@localhost ~]# lssdsdsf 2> test1
[root@localhost ~]# cat test1
bash: lssdsdsf: 未找到命令...
[root@localhost ~]# lssdsdsf 2>> test1
[root@localhost ~]# cat test1
bash: lssdsdsf: 未找到命令...
bash: lssdsdsf: 未找到命令...

6、&> &>>混合输出,既可以保存标准输出,也可以保存错误输出

[root@localhost ~]# ls &> test1
[root@localhost ~]# lsdsdsdsdsdsds &>> test1
[root@localhost ~]# cat test1
test1
test.sh
公共
模板
视频
图片
文档
下载
音乐
桌面
bash: lsdsdsdsdsdsds: 未找到命令...

7、重定向输入:< 从指定的文件获取数据。

[root@localhost opt]# echo 123456 > passwd.txt
[root@localhost opt]# ls
login.sh  nginx-1.22.0  nginx-1.22.0.tar.gz  passwd.txt
[root@localhost opt]# cat passwd.txt 
123456
[root@localhost opt]# passwd --stdin dn < passwd.txt 
更改用户 dn 的密码 。
passwd:所有的身份验证令牌已经成功更新。

三、变量的作用和类型,如何声明变量

3.1变量的作用:

用来存放系统和用户所需使用的设定的参数,变量保存在内存中,调用的时候,直接到内存中查找变量所在的内存地址。

3.2、变量的类型:

3.2.1、自定义变量

自定义变量:名称由用户自定义 值也可以自定义。

​ 变量名的规范:

​ 1、不要使用系统的命令作为变量名

​ 2、不要使用中文

​ 3、不要使用特殊符号开头,可以使用_开头,不要使用数字开头。

​ 4、变量名我们一般会使用对应的英文名称来使用,computer student nubmer 。。。

​ 5、变量名要保持前后一致,要有注释。提供代码的可读性。

​ 变量值:int 整数 string 字符串 浮点 0.12 布尔: true false

​ 字符串需要双引号引起来。"tes

[root@localhost opt]# vim test.sh
[root@localhost opt]# sh test.sh
10
[root@localhost opt]# vim test.sh

a="test"
echo "$a"


[root@localhost opt]# sh test.sh
test
[root@localhost opt]# vim test.sh

a=1
b=2
c=$[$a+$b]
echo $c



[root@localhost opt]# sh test.sh
3

’ '的作用

[root@localhost opt]# vim test.sh

a='test'
echo '$a'


[root@localhost opt]# sh test.sh
$a

#a=‘test’
#echo ‘$a’
#双引号,单引号,反撇号之间的作用和区别
#弱引用和强引用
#双引号,就是弱引用,变量值就是变量值本身
#单引号,强引用,变量值不再是本身定义的值,保持自身的字符串形式
#反撇号,命令替换,把命令结果传给变量,作为变量的值,先执行命令>,再给变量赋值。

[root@localhost opt]# vim test.sh

a=`ls /opt`
echo "$a"


[root@localhost opt]# sh test.sh
login.sh
nginx-1.22.0
nginx-1.22.0.tar.gz
passwd.txt
test1
test1.sh
test2.sh
test.sh
[root@localhost opt]# vim test.sh

#自定义输入变量的值

read -p "提示信息:" a
read -p "数字b:" b
c=$(($a+$b))         ##只能做整数运算
echo $c
#-p 指定提示符,提示符的内容包含在“”中间,“”可以为空,但是双引号
必须要有。



[root@localhost opt]# sh test.sh
提示信息:16
数字b:15
31

3.2.2、全局变量

全局变量:对整个系统生效,所有用户都可以使用。

vim test.sh

c=$(($a+$b))
echo $c

[root@localhost opt]# export a
[root@localhost opt]# a=3
[root@localhost opt]# export b
[root@localhost opt]# b=5
[root@localhost opt]# sh test.sh
8                               ##当前shell


[root@localhost opt]# vim /etc/profile    ##全局配置文件,全局生效

export a=3
export b=5

[root@localhost opt]# source /etc/profile
[root@localhost opt]# sh test.sh
8


重新开shell
[root@localhost ~]# sh test.sh
当前目录的位置
/boot
展示其中以vml开头的文件:
-rwxr-xr-x. 1 root root 6.4M 5月  15 22:34 vmlinuz-0-rescue-f40500d9daf845a7aac3e242fa92069c
-rwxr-xr-x. 1 root root 6.4M 11月  9 2018 vmlinuz-3.10.0-957.el7.x86_64
[root@localhost ~]# sh /opt/test.sh
8

全局生效,每个脚本的变量值是固定的。

持久性

共享性

全局变量适合于不需要经常更改的值。

排错的方法:bash -n 脚本名:检测语法。

bash -x 脚本名:会把每一步打印出来,方便定位每一步的错误。

3.2.3、环境变量

环境变量:环境变量是系统创建的,主要用来设置用户的工作环境。

环境变量可以理解为特殊的全局变量,/etc/profile配置文件。

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

可执行的命令,或者可执行的程序默认路径。

[root@localhost ~]# vim 123.sh
[root@localhost ~]# 123.sh
bash: 123.sh: 未找到命令...
[root@localhost ~]# ls
123.sh  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# 123.sh
bash: 123.sh: 未找到命令...
[root@localhost ~]# chmod 777 123.sh
[root@localhost ~]# 123.sh
bash: 123.sh: 未找到命令...
[root@localhost ~]# cat 123.sh
[root@localhost ~]# cd /opt/
[root@localhost opt]# cat 123.sh
cat: 123.sh: 没有那个文件或目录
[root@localhost opt]# 123.SH
bash: 123.SH: 未找到命令...
[root@localhost opt]# 123.sh
bash: 123.sh: 未找到命令...
[root@localhost opt]# PATH="$PATH:/root"
[root@localhost opt]# echo $path

[root@localhost opt]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root
[root@localhost opt]# 123.sh
[root@localhost opt]# cd ..
[root@localhost /]# cd ~
[root@localhost ~]# 123.sh
[root@localhost ~]# cd /opt/
[root@localhost opt]# cat 123.sh
cat: 123.sh: 没有那个文件或目录
[root@localhost opt]# 

在这里插入图片描述

3.2.4、位置变量

位置变量:又名命令行参数,是在脚本的外面给脚本内部传值。

$1$2$3…$9

从第一个到第九个

10以后要用{}把数字括起来

( 10 ) (10) (10)(11)

[root@localhost opt]# vim test.sh

a=$1
b=$2
c=$(($a+$b))
echo $c


[root@localhost opt]# sh test.sh 12 13
25

3.2.5、预定义变量

预定义变量:编程语言和解释器提供的,用户无法更改,只能由系统自行更新和设备。

∗ 和 *和 @:表示命令或者脚本需要处理的参数。二者的区别在于处理方式的不同。

∗ 和 *和 @:不加引号的表现一致,都把参数一个一个处理。

$*加了引号,会把参数当成一个整体来进行处理。

[root@localhost opt]# vim test1.sh


function print_args {

for arg in "$*"
do
 echo "$arg"
done

}
print_args "1" "2" "3"

[root@localhost opt]# sh test1.sh 
1 2 3

$*不加引号,结果如下:

[root@localhost opt]# vim test1.sh

function print_args {

for arg in $*
do
 echo "$arg"
done

}
print_args "1" "2" "3"

[root@localhost opt]# sh test1.sh 
1
2
3

$#加引号

[root@localhost opt]# vim test1.sh

function print_args {

for arg in "$@"
do
 echo "$arg"
done

}
print_args "1" "2" "3"


[root@localhost opt]# sh test1.sh 
1
2
3

$@不加引号

[root@localhost opt]# vim test1.sh



function print_args {

for arg in $@
do
 echo "$arg"
done

}
print_args "1" "2" "3"

[root@localhost opt]# sh test1.sh 
1
2
3

$#也是一个全局变量,统计传递给脚本或者函数的参数个数。

[root@localhost opt]# vim test1.sh

function print_args {

for arg in $@
do
 echo "$arg"
done
echo "传了几个:$#"
}
print_args "1" "2" "3"

[root@localhost opt]# sh test1.sh 
1
2
3
传了几个:3
echo $?   ##查看上一次命令的执行状态

true false 命令执行之后返回的状态码,

只有0,表示成功

所有非0都表示失败

[root@localhost opt]# ls
login.sh      nginx-1.22.0.tar.gz  test1.sh
nginx-1.22.0  passwd.txt           test.sh
[root@localhost opt]# echo $?
0
[root@localhost opt]# sddss
bash: sddss: 未找到命令...
[root@localhost opt]# echo $?
127

返回上一次,执行命令的状态

例子1:

[root@localhost opt]# vim test2.sh

read -p "输入第一个数" a
read -p "输入第一个数" b
if [ $a -eq $b ]
then
echo $?
else
echo $?
fi

[root@localhost opt]# sh test2.sh 
输入第一个数12
输入第一个数12
0
[root@localhost opt]# sh test2.sh 
输入第一个数12
输入第一个数13
1

四、运算符号

4.1、加法

[root@localhost opt]# vim test2.sh

read -p "输入第一个数" a
read -p "输入第一个数" b
c=$(($a+$b))
echo $c

+ - * /  %取余

[root@localhost opt]# sh test2.sh 
输入第一个数12
输入第一个数14
26

4.2、减法

[root@localhost opt]# vim test2.sh



read -p "输入第一个数" a
read -p "输入第一个数" b
c=$(($a-$b))
echo $c

+ - * /  %取余

[root@localhost opt]# sh test2.sh 
输入第一个数12
输入第一个数3
9

4.3、乘法

[root@localhost opt]# vim test2.sh

read -p "输入第一个数" a
read -p "输入第一个数" b
c=$(($a*$b))
echo $c

+ - * /  %取余

[root@localhost opt]# sh test2.sh 
输入第一个数13
输入第一个数12
156

4.4、除法

[root@localhost opt]# vim test2.sh

read -p "输入第一个数" a
read -p "输入第一个数" b
c=$(($a/$b))
echo $c

+ - * /  %取余

[root@localhost opt]# sh test2.sh 
输入第一个数14
输入第一个数5
2

4.5、取余

[root@localhost opt]# vim test2.sh

read -p "输入第一个数" a
read -p "输入第一个数" b
c=$(($a%$b))
echo $c
+ - * /  %取余

[root@localhost opt]# sh test2.sh 
输入第一个数14
输入第一个数5

4.6、乘法补充:

c=$(expr $a \* $b )   ##乘法不一样
[root@localhost opt]# vim test2.sh
[root@localhost opt]# sh test2.sh 
输入第一个数12
输入第一个数14
expr: 语法错误          ##无\

[root@localhost opt]# vim test2.sh


read -p "输入第一个数" a
read -p "输入第一个数" b
c=$(expr $a \* $b )
echo $c

+ - * /  %取余

[root@localhost opt]# sh test2.sh 
输入第一个数14
输入第一个数15
210

4.7、let补充

let c=$a*$b     ##加减乘除都一样


[root@localhost opt]# vim test2.sh

read -p "输入第一个数" a
read -p "输入第一个数" b
let c=$a*$b
echo $c
# + - * /  %取余


[root@localhost opt]# sh test2.sh 
输入第一个数12
输入第一个数13
156

4.8、小数运算

#小数运算:bc是一个简易的系统自带的计算机程序,可以执行整数,也可以计算小数。

[root@localhost opt]# vim test2.sh

read -p "输入第一个数" a
read -p "输入第一个数" b

+ - * /  %取余

#小数运算:bc是一个简易的系统自带的计算机程序,可以执行整数,也>可以计算小数。
c=$(echo "$a+$b"|bc)

echo $c

[root@localhost opt]# sh test2.sh 
输入第一个数12.4
输入第一个数12.5
24.9

4.9、awk命令行运算

awk:命令行传参数

[root@localhost opt]# awk 'BEGIN{print 4.5*4.8}'
21.6
[root@localhost opt]# awk 'BEGIN{print 4.5+4.8}'
9.3
[root@localhost opt]# awk 'BEGIN{print 4.5-4.8}'
-0.3
[root@localhost opt]# awk 'BEGIN{print 4.5/4.8}'
0.9375
[root@localhost opt]# awk 'BEGIN{print 4.5%4.8}'
4.5

在这里插入图片描述
在这里插入图片描述

相关推荐

  1. Shell 输入/输出定向

    2024-06-12 05:12:03       28 阅读
  2. Linux Shell 021-输入输出定向

    2024-06-12 05:12:03       61 阅读
  3. 「Linux系列」Shell 输入/输出定向

    2024-06-12 05:12:03       43 阅读
  4. Linux Shell文件描述符和定向

    2024-06-12 05:12:03       39 阅读
  5. bash shell 定向输入和输出

    2024-06-12 05:12:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 05:12:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 05:12:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 05:12:03       87 阅读
  4. Python语言-面向对象

    2024-06-12 05:12:03       96 阅读

热门阅读

  1. docker使用auth登录

    2024-06-12 05:12:03       30 阅读
  2. 二进制兼容

    2024-06-12 05:12:03       32 阅读
  3. day6 C++

    2024-06-12 05:12:03       30 阅读
  4. 2024全国高考作文题解读(讯飞星火3.0版本)

    2024-06-12 05:12:03       25 阅读
  5. 常用的网络安全测试工具介绍

    2024-06-12 05:12:03       37 阅读
  6. 个人愚见的自主可控

    2024-06-12 05:12:03       35 阅读