shell知识介绍

shell脚本在Linux运维工作中的地位

shell脚本语言很适合用于处理纯文本类型的数据,而linux系统中几乎所有的配置文件,日志文件,以及绝大多数的启动未及爱你都是纯文本类型的文件,因此学好shell脚本语言,就可以利用它在linux系统中发挥巨大的作用

shell脚本语言四弱类型语言(无须定义变量类型即可使用),在Unix/Linux中主要有量大类shell

[dd@foundation0 ~]$ which sh

/usr/bin/sh

[dd@foundation0 ~]$ ll /usr/bin/sh

lrwxrwxrwx. 1 root root 4 Nov 28  2018 /usr/bin/sh -> bash

[dd@foundation0 ~]$ cat /etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/usr/bin/sh

/usr/bin/bash

/usr/sbin/nologin

/bin/tcsh

/bin/csh

##linux系统中的主流shell是bash,bash是由sh发展而来的,同时bash还包含了csh和ksh的特色,但大多数脚本都可以不加修改的在sh上运行,如果使用sh后发现结果和预期有差异,那么可以尝试用bash代替sh

shell语言的优势

shell脚本语言的优势在于处理偏操作系统底层的业务,例如:linux系统内部的很多应用(有的是应用的一部分)都是使用shell脚本语言开发的,因为有1000多个linux系统命令为它做支撑,特别是linux正则表达式及文本三剑客grep awk sed等命令

对于一些常见的系统脚本,使用shell开发会更简单,更快捷

#使用shell更符合linux运委简单 易用 高效的三大基本原则

通过以下方法可以查看系统默认的shell

[root@foundation0 ~]# echo $SHELL

/bin/bash

[root@foundation0 ~]# grep root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@foundation0 ~]# cat /etc/red

redhat-access-insights/ redis/

redhat-release         

[root@foundation0 ~]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 7.3 (Maipo)

[root@foundation0 ~]# bash --version

GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

Copyright (C) 2011 Free Software Foundation, Inc. #bash的版本

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>



This is free software; you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

shell脚本运行的方式

# 使用source或'.'可以将自身脚本中的变量值或函数等的返回值传递到当前父shell脚本中使用

[root@server1 mnt]# sh 3.sh

[root@server1 mnt]# echo $userdir

[root@server1 mnt]# source 3.sh

[root@server1 mnt]# echo $userdir

/mnt

[root@server1 mnt]# cat 3.sh

userdir=`pwd`

我们可以得出以下结论:

儿子shell脚本会直接继承父亲shell脚本的变量,函数(就好像儿子随父的姓,基因也会继承父亲)等,反之则不可以

如果希望反过来继承(就好像是让父亲随儿子姓,让父亲的基因也继承儿子的),就要用source或'.'在父亲shell脚本中事先加载儿子的shell脚本

shell变量的特性

默认情况下,在bash shell中是不会区分变量类型的 #这和其他强类型语言(java/c)是有区别的

变量类型:

变量和分为两种类型:环境变量(全局变量)和普通变量(局部变量)

  环境变量也可称为全局变量,可以在创建它们的shell及派生出来的任意子进程shell中使用,环境变量又可以分为自定义环境变量和bash内置环境变量

"""

按照系统规范,所有环境变量的名字均采用大写形式,在将环境变量应用于用户进程程序之前,都应该用export命令导出定义

[root@server1 mnt]# export DDPP=99

[root@server1 mnt]# echo $DDPP

99

[root@server1 mnt]# exit

logout

Connection to 172.25.0.1 closed.

[root@foundation0 ~]# ssh root@172.25.0.1

root@172.25.0.1's password:

Last login: Wed Dec 11 11:47:52 2019 from foundation0.ilt.example.com

[root@server1 ~]# echo $DDPP

# 环境变量的设置

[root@server1 ~]# DDPP=99;export DDPP

[root@server1 ~]# echo $DDPP

99

[root@server1 ~]# declare -x NAME=DDPP

[root@server1 ~]# echo $DDPP

99

[root@server1 ~]# declare -x NAME=DDPP

[root@server1 ~]# echo $NAME

DDPP



[root@server1 ~]# echo "export DDPP=99" > /etc/profile

[root@server1 ~]# cat /etc/profile | grep DDPP

export DDPP=99

[root@server1 ~]# source /etc/profile

[root@server1 ~]# echo $DDPP

99

[root@server1 ~]# env|grep DDPP

DDPP=99

NAME=DDPP

环境变量知识小结:

1.变量名通常要大写

2.变量可以在自身的shell及子shell中使用

3.常用export来定义环境变量

4.执行env默认可以显示所有的环境变量名称及其对应的值

5.输出时用"$变量名" 取消时用"unset 变量名"

6.如果希望环境变量永久生效,则可以将其放在用户环境变量文件或全局变量文件里

"""

  普通变量也可称为局部变量,只能在创建它们的shell函数或shell脚本中使用 普通变量一般由开发者在开发脚本程序时创建

#若要在登陆后初始化或显示加载内容,则把脚本文件放在/etc/profile.d/下即可(无须加执行权限)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值