shell脚本

一、shell脚本之特殊变量

1.常用的特殊位置参数变量:
位置变量作用说明
$0获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,那么就包括脚本路径
$n获取当前执行的shell脚本的第n个参数值,n=1…9,当n为0时表示脚本的文件名;如果n大于9,则用大括号括起来,例如 ${10},接的参数以空格隔开
$#获取当前执行的shell脚本后面接的参数的总个数
$*获取当前shell脚本所有传参的参数,不加引号和 $@ 相同;如果给 $*加上双引号," $ *",则表示将所有的参数视为单个字符串,相当于"$1 $2 $3"
$@获取当前shell脚本所有传参的参数,不加引号和$*相同;如果给 $ *加上双引号,"$ *",则表示将所有的参数视为不同的独立字符串,相当于"$1" “$2” “$3”

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

2.shell进程的特殊状态变量:
位置变量作用说明
$?获取执行上一个指令的执行状态返回值(0为成功,非0为失败)
$$获取当前执行shell脚本的进程号(PID),不常用
$!获取上一个在后台工作的进程的进程号(PID),不常用
$_获取在此之前执行的命令或脚本的最后一个参数,不常用

“$?”返回值的用法如下:

(1)判断命令、脚本或函数等程序是否执行成功
(2)若在脚本中调用执行“exit数字”,则会返回这个数字给“ $? ”变量
(3)如果是在函数里,则通过“return数字”把这个数字以函数返回值的形式传给“ $? ”

[root@localhost test]# echo $!
26994
[root@localhost test]# echo $$
24115
[root@localhost test]# ls -a -l
[root@localhost test]# echo $_
-l
[root@localhost test]# sh color.sh
ping tong
bu tong
[root@localhost test]# echo $?
0
[root@localhost test]# cat testi
cat: testi: No such file or directory
[root@localhost test]# echo $?
1
3.shell的内置变量

内部变量:echo,eval,exec,export,read,shift
(1)echo
echo的参数等信息说明:

参数说明
-n不换行输出内容
-e解析转义字符
\n换行
\r回车
\t制表符(tab)
\b退格
\v纵向制表符

(2)exec
功能:exec命令能够在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该进程(也就是最初的shell)就终止了。
在这里插入图片描述
当使用exec打开文件后,read命令每次都会将文件指针移动到文件的下一行进行读取,直到文件末尾,利用这个可以实现处理文件内容。
在这里插入图片描述
(3)read
read -p:后面跟提示信息,即在输入前打印提示信息

(4)shift
功能:shift语句会按如下方式重新命名所有位置参数变量,即$2成为$1、$3成为$2等,以此类推,在程序中每使用一次shift语句,都会使所有的位置参数依次向左移动一个位置,并使位置参数 $#减1,直到减到0为止。

应用场景:当我们写shell希望像命令行的命令通过参数控制不同的功能时,就会先传一个类似-c的参数,然后再接内容。

(5)exit
功能:退出shell程序。在exit之后可以有选择地指定一个数位作为返回状态。

二、shell脚本之子串及特殊变量

shell变量子串说明:

表达式说明
${parameter}返回变量$parameter的内容
${#parameter}返回变量$parameter内容的长度(按字符),也适用于特殊变量
${parameter:offset}在变量${parameter}中,从位置offset之后开始提取子串到结尾
${parameter:offset:length}在变量${parameter}中,从位置offset之后开始提取长度length的子串
${parameter#word}从变量${parameter}开头开始删除最短匹配的word子串
${parameter##word}从变量${parameter}开始删除最长匹配的word子串
${parameter%word}从变量${parameter}结尾开始删除最短匹配的word子串
${parameter%%word}从变量${parameter}结尾开始删除最长匹配的word子串
${parameter/pattern/string}使用string代替第一个匹配的pattern
${parameter//pattern/string}使用string代替所有匹配的pattern
[root@localhost test]# echo $a |wc -L
10
[root@localhost test]# echo "$a" |awk '{print length($0)}'
10
[root@localhost test]# echo ${#a}
10
[root@localhost test]# echo $b
abcdefg
[root@localhost test]# echo ${#b}
7
[root@localhost test]# echo ${b:2}
cdefg
[root@localhost test]# echo ${b:3:2}
de
[root@localhost test]# b="abcdefgabcde"
[root@localhost test]# echo ${b#a*c}
defgabcde
[root@localhost test]# echo ${b##a*c}
de
[root@localhost test]# echo ${b%c*e}
abcdefgab
[root@localhost test]# echo ${b%%c*e}
ab
[root@localhost test]# echo ${b/a/A}
Abcdefgabcde
[root@localhost test]# echo ${b//a/A}
AbcdefgAbcde
[root@localhost test]# echo ${c//a/A}
[root@localhost test]# echo ${a//c/C}
1234567890

例:批量修改文件名
[root@localhost ~]# ls /root/test/test001
file_test_abc  file_test_abc01  file_test_abc02  file_test_abc03  file_test_abc04  file_test_abc05
[root@localhost test]# vim tihuan.sh
[root@localhost test]# sh tihuan.sh
[root@localhost test]# cat tihuan.sh
#!/bin/bash
#Author:hiolb
#Blog:https://blog.csdn.net/hiolb
#Time:2020-07-08 17:34:49
#Name:tihuan.sh
#Version:V1.0
#Description:This is a test script.
for i in `ls /root/test/test001`  
do
f="$i"
mv /root/test/test001/$i /root/test/test001/${f/abc/123}
done
[root@localhost ~]# ls /root/test/test001
file_test_123  file_test_12301  file_test_12302  file_test_12303  file_test_12304  file_test_12305
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值