Shell子串

bash一些基础的内置命令

echo

  1. -n 不换行输出
  2. -e 解析字符串中的特殊符号
  3. echo -e "解析换行\n符号"
  4. \n 换行
  5. \r 回车
  6. \t 制表符 四个空格
  7. \b 退格

eval 执行多个命令

  1. eval [command 1;command 2;....]
  2. eval ls;cd /root

exec 不创建子进程,执行后续命令,且执行完毕后自动exit

  1. [17:59:13 root@VM-8-17-centos study]#date
  2. Tue May 10 17:59:15 CST 2022
  3. [17:59:15 root@VM-8-17-centos study]#exec date
  4. Tue May 10 17:59:21 CST 2022
  5. Connection closed.
  6. Disconnected from remote host(175.178.27.151) at 17:59:21.
  7. Type `help' to learn how to use Xshell prompt.
  8. [C:\~]$

shell子串的花式用法

  1. ${变量} 返回变量值
  2. ${#变量} 返回变量长度,字符长度
  3. ${变量:start} 返回变量start数值之后的字符
  4. ${变量:start:length} 提取start之后的length限制的字符
  5. ${变量#word} 从变量开头删除最短匹配的word子串
  6. ${变量##word} 从变量开头,删除最长匹配的word
  7. ${变量%word} 从变量结尾删除最短的word
  8. ${变量%%word} 从变量结尾开始删除最长匹配的word
  9. ${变量/pattern/string} string替换第一个匹配的pattern
  10. ${变量//pattern/string} 用string替换所有的pattern
  1. [17:20:48 root@VM-8-17-centos ~]#name="AB 1212 AB"
  2. [17:21:53 root@VM-8-17-centos ~]#echo ${name};echo ${#name};
  3. AB 1212 AB
  4. 10
  5. [17:22:00 root@VM-8-17-centos ~]#echo ${name:4};echo ${name:4:5}
  6. 212 AB
  7. 212 A
  8. [17:24:06 root@VM-8-17-centos ~]#echo ${name#A*1};echo ${name##A*1}
  9. 212 AB
  10. 2 AB
  11. [17:25:10 root@VM-8-17-centos ~]#echo ${name%1*B};echo ${name%%1*B}
  12. AB 12
  13. AB
  14. [17:25:52 root@VM-8-17-centos ~]#echo ${name/AB/CD};echo ${name//AB/CD}
  15. CD 1212 AB
  16. CD 1212 CD

统计变量子串的长度

多种统计长度的命令

  • echo ${#变量} 效率最高
  • 使用wc -L 命令统计时间
  • expr命令的length函数统计
  • awk加工处理

    统计的执行速度?

    验证方式:time 命令 ``shell [19:54:39 root@VM-8-17-centos ~]#for n in {1..3};do char=seq -s “:” 8;echo ${char};done 1:2:3:4:5:6:7:8 1:2:3:4:5:6:7:8 1:2:3:4:5:6:7:8 [19:54:50 root@VM-8-17-centos ~]#time for n in {1..3};do char=seq -s “:” 8`;echo ${#char};done 15 15 15

real 0m0.005s #实际运行的时间 user 0m0.001s #用户态执行的时间 sys 0m0.004s #内核态执行的时间

  1. 验证:
  2. ```shell
  3. #time for n in {1..10000};do char=`seq -s ":" 100`;echo ${#char} &>/dev/null;done
  4. real 0m14.253s
  5. user 0m6.461s
  6. sys 0m7.422s
  7. #time for n in {1..10000};do char=`seq -s ":" 100`;expr length "${#char}" &>/dev/null;done
  8. real 0m27.239s
  9. user 0m12.426s
  10. sys 0m14.173s
  11. #time for n in {1..10000};do char=`seq -s ":" 100`;echo ${char}|awk '{print length($0)}' &>/dev/null;done
  12. real 0m40.712s
  13. user 0m19.115s
  14. sys 0m20.562s
  15. #time for n in {1..10000};do char=`seq -s ":" 100`;echo ${char}|wc -L &>/dev/null;done
  16. real 0m39.648s
  17. user 0m18.944s
  18. sys 0m19.729

shell编程,尽量使用linux内置的命令,内置的操作,和内置的函数,效率最高 C语言开发,效率最高
尽可能的减少管道符的操作

批量修改文件名

  1. #for i in {1..5};do touch test${i}_finished.jpg;done
  2. #ls
  3. different.sh masktest retuan.sh test1_finished.jpg test2_finished.jpg test3_finished.jpg test4_finished.jpg test5_finished.jpg
  4. #修改文件名
  5. #for file_name in `ls *.jpg`;do mv $file_name `echo ${file_name//_finished/}`;done
  6. #ls
  7. different.sh masktest retuan.sh test1.jpg test2.jpg test3.jpg test4.jpg test5.jpg