Linux Shell编程--脚本运行与变量置换

前言:本博客仅作记录学习使用,部分图片出自网络,如有侵犯您的权益,请联系删除

一、脚本运行

1、创建Bash脚本(Shell脚本)

1、创建脚本文件

 # !/bin/bash 指定命令解释器 
 # 注释 
 编写bash指令集合 (脚本内容) 

2、修改权限

2、bash/sh 脚本的绝对路径/相对路径

不管脚本有没有x(可执行)权限,都可以执行

2.1、sh脚本的相对路径执行脚本

 [root@centos opt]# vim hello.sh
 [root@centos opt]# pwd
 /opt
 [root@centos opt]# sh hello.sh 
 Hello!

2.2、sh脚本的绝对路径执行脚本

 [root@centos opt]# sh /opt/hello.sh
 Hello!

2.3、 bash 脚本的相对路径执行脚本

 [root@centos opt]# bash hello.sh 
 Hello!

2.4、bash 脚本的绝对路径执行脚本

 [root@centos opt]# bash /opt/hello.sh
 Hello!

3、脚本的绝对路径/相对路径(需要脚本的+x权限)

3.1、脚本的相对路径执行脚本

 [root@centos opt]# ./hello.sh
 -bash: ./hello.sh: Permission denied    # 权限不够
 [root@centos opt]# chmod +x hello.sh
 [root@centos opt]# ./hello.sh
 Hello!
 [root@centos opt]# ll
 -rwxr-xr-x  1 root root 26 Aug  1 14:11 hello.sh

可以发现,执行./hello.sh需要x权限】、

3.2、脚本的绝对路径执行脚本

收回权限再次使用绝对路径执行脚本

 [root@centos opt]# chmod -x hello.sh
 [root@centos opt]# /opt/hello.sh
 -bash: /opt/hello.sh: Permission denied 
 ​
 # 无权限,给脚本添加权限即可使用绝对路径执行
 [root@centos opt]# chmod +x hello.sh
 [root@centos opt]# /opt/hello.sh
 Hello!

4、source脚本的绝对路径/相对路径执行脚本

source执行脚本时,不管脚本有没有x(可执行权限),脚本都可以执行 我们平时配置完环境变量,用的就是source执行,如source /etc/profile

5、子shell

 [root@centos ~]# cat bash.sh
 cd /opt/
 pwd
 [root@centos ~]# sh bash.sh
 /opt
 [root@centos ~]# source bash.sh 
 /opt

6、Bash脚本测试

sh –x script
	# 这将执行该脚本并显示所有变量的值
sh –n script
	# 不执行脚本只是检查语法模式,将返回所有错误语法
sh –v script
	# 执行脚本前把脚本内容显示在屏幕上

二、变量置换

1、命令替换

a=`date +%m%d`
a=$(date +%m%d)
反引号亦可用$() 代替

2、变量替换

(1)${parameter:-word}

若 parameter 为空或未设置,则用 word 代替 parameter 进行替换,parameter 的值不变

# a=1
# unset b
# a=${b:-3} # echo $a
3
# echo $b

若 parameter 不为空,则不替换,parameter 的值不变

# unset b
# a=1
# b=2
# a=${b:-3} # echo $a
2
# echo $b
2

(2) ${parameter:=word}

若 parameter 为空或未设置,则用 word 代替 parameter 进行替换,parameter 的值改变

# a=1
# unset b
# a=${b:=3}
# echo $a
3
# echo $b
3

若 parameter设置了,则 不替换,parameter 的值不变

# a=1
# b=2
# a=${b:=3}
# echo $a
2
# echo $b
2

(3)${parameter:+word}

如果 parameter 已设置,那么 word将被扩展并替换为该表达式的结果;

# a=1
# b=2
# a=${b:+3}
# echo $a
3
# echo $b
2

如果 parameter 未设置或为空,则结果为空

# a=1
# unset b
# a=${b:+3}
# echo $a

# echo $b

(4) ${parameter:?message}

若 parameter 为空或未设置,则 message 作为标准错误打印出来,这可用来检查变量是否正确设置

unset a
# ${a:?unset a}
-bash: a: unset a

三、变量替换-匹配截取

${变量#关键词} 若变量内容从头开始的数据符合『关键词』,则将符合的最短数据切除
${变量##关键词} 若变量内容从头开始的数据符合『关键词』,则将符合的最长数据切除
${变量%关键词} 若变量内容从尾向前的数据符合『关键词』,则将符合的最短数据切除
${变量%%关键词} 若变量内容从尾向前的数据符合『关键词』,则将符合的最长数据切除
${变量/旧字符串/新字符串} 若变量内容符合『旧字符串』则『第一个旧字符串会被新字符串替代』
${变量//旧字符串/新字符串} 若变量内容符合『旧字符串』则『全部的旧字符串会被新字符串替代』

1、索引及切片

[root@centos ~]# a=12345678
[root@centos ~]# echo ${a:5}		# 从第5位开始截取
678
[root@centos ~]# echo ${a:3:4}	# 从第3位开始截取4位
4567
[root@centos ~]# echo ${a:2:-1}	# 从第2位开始截取到倒数第1位之前(不包括倒数1位)
34567
[root@centos ~]# echo ${a:2:-2}	# 从第2位开始截取到倒数第2位之前(不包括倒数2位)
3456

2、变量内容的删除

[root@centos ~]# url=www.sina.com.cn
[root@centos ~]# echo ${#url}		# 获取变量的长度
15
[root@centos ~]# echo ${url}		# 输出正常值
www.sina.com.cn

[root@centos opt]# echo ${url#*.}	# 从前往后,最短匹配
sina.com.cn
[root@centos ~]#  echo ${url##*.}	# 从前往后,最长匹配
cn
[root@centos ~]# echo ${url%.*}		# 从后往前,最短匹配
www.sina.com
[root@centos ~]# echo ${url%%.*}	# 从后往前,最长匹配
www
[root@centos ~]# echo ${url#a.}		# 删除url变量值中最短的以a.结尾的前缀
www.sina.com.cn
# 删除url变量值中最短的以a.结尾的前缀,并且*表示匹配任意数量的字符
[root@centos ~]# echo ${url#*a.}	
com.cn

3、变量内容的替换

[root@centos ~]# a=123456123789
[root@centos ~]# echo ${a/1/}	# 在变量 a 的值中替换第一次出现的 1 为一个空字符串
23456123789		
[root@centos ~]# echo ${a//1/}	# 将变量 a 中所有的 1 替换为空字符串
2345623789		
[root@centos ~]# echo ${a/1/x}	# 将变量 a 值中第一次出现的 1 替换为 x
x23456123789
[root@centos ~]# echo ${a//1/x}	# 将变量 a 值中所有的 1 替换为 x
x23456x23789

[root@centos ~]# file=/dir1/dir2/dir3/my.file.txt
[root@centos ~]# echo ${file#*/}	# 去掉匹配到第一个/之前的所有字符串
dir1/dir2/dir3/my.file.txt
[root@centos ~]# echo ${file#*.}	# 去掉匹配到第一个.之前的所有字符串
file.txt
[root@centos ~]# echo ${file##*.}	# 去掉匹配到最后一个.之前的所有字符串
txt
[root@centos ~]# echo ${file%/*}	# 去掉从末尾往前匹配第一个/之间的所有字符串
/dir1/dir2/dir3
[root@centos ~]# echo ${file%.*}	# 去掉从末尾往前匹配第一个.之间的所有字符串
/dir1/dir2/dir3/my.file

记忆的方法为:

# 是去掉左边
% 是去掉右边
单一符号是最小匹配;两个符号是最大匹配(贪婪匹配)。

4、basename & dirname

4.1、basename命令

去除目录后剩下的名字

# 不会检测文件系统,只是取路径的最后一段,将他截取出来
例:
# temp=/home/temp/1.test
# base=`basename $temp`
# echo $base
结果为:1.test

4.2、dirname命令

去除文件的文件名,留目录名

# 不会检测文件系统,默认路径的最后一段为文件名,把它删除
例:
#temp=/home/temp/1.test
#dir=`dirname $temp`
#echo $dir
结果为:/home/temp

致谢

在此,我要对所有为知识共享做出贡献的个人和机构表示最深切的感谢。同时也感谢每一位花时间阅读这篇文章的读者,如果文章中有任何错误,欢迎留言指正。 

学习永无止境,让我们共同进步!!

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小李学不完

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

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

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

打赏作者

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

抵扣说明:

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

余额充值