您的位置 首页 java

解决Shell脚本编程90%工作问题之初遇shell

变量处理

数据删除

  1. ${变量#匹配字符}: 从头开始匹配字符,找到即返回
  2. ${变量##匹配字符}: 从头开始匹配字符,属于贪婪模式。
  3. ${变量%匹配字符}: 从尾部开始匹配字符,找到即返回
  4. ${变量%%匹配字符}: 从尾部开始匹配字符,属于贪婪模式。

以下是我写的测试的一段shell脚本。

Last login: Tue Oct 15 10:27:09 2019 from 116.21.31.77
Welcome to Alibaba Cloud Elastic Compute Service !
[root@xieweihuagogogo ~]# variable="i love you, do you love me?"
[root@xieweihuagogogo ~]# echo $variable
i love you, do you love me?
[root@xieweihuagogogo ~]# var1=${variable#*ov}
[root@xieweihuagogogo ~]# echo $var1
e you, do you love me?
[root@xieweihuagogogo ~]# var2=${variable##*ov}
[root@xieweihuagogogo ~]# echo $var2
e me?
[root@xieweihuagogogo ~]# var3=${variable%ov*}
[root@xieweihuagogogo ~]# echo $var3
i love you, do you l
[root@xieweihuagogogo ~]# var4=${variable%%ov*}
[root@xieweihuagogogo ~]# echo $var4
i l
 

数据替换

1.${变量/旧 字符串 /新字符串}:替换第一个查询到的旧字符串

2.${变量//旧字符串/新字符串}: 替换所有查询到的旧字符串

[root@xieweihuagogogo ~]# echo $PATH
/usr/local/ zookeeper /zookeeper-3.4.13/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.8.0_191-amd64/bin:/root/bin
[root@xieweihuagogogo ~]# var1=${PATH}
[root@xieweihuagogogo ~]# var2=${var1/bin/Bin}
[root@xieweihuagogogo ~]# echo $var2
/usr/local/zookeeper/zookeeper-3.4.13/Bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.8.0_191-amd64/bin:/root/bin
[root@xieweihuagogogo ~]# var3=${var1//bin/Bin}
[root@xieweihuagogogo ~]# echo $var3
/usr/local/zookeeper/zookeeper-3.4.13/Bin:/usr/local/sBin:/usr/local/Bin:/usr/sBin:/usr/Bin:/usr/java/jdk1.8.0_191-amd64/Bin:/root/Bin
[root@xieweihuagogogo ~]# 
 

字符串处理

计算字符串长度

1.${#string}

2.expr lenght “$string” :string有空格,则必须加双引号

[root@xieweihuagogogo ~]# vara="abc"
[root@xieweihuagogogo ~]# len=${#vara}
[root@xieweihuagogogo ~]# echo $len
3
[root@xieweihuagogogo ~]# expr lenght $vara
expr: syntax error
[root@xieweihuagogogo ~]# varb=expr length $vara
-bash: length: command not found
[root@xieweihuagogogo ~]# echo $vara
abc
[root@xieweihuagogogo ~]# len=`expr length "$vara"`
[root@xieweihuagogogo ~]# echo $len
3
[root@xieweihuagogogo ~]#
 

获取子串的位置

expr index “$变量” 字符串 :注意这里面查找的字符串会被分割成一个一个字符。

示例:

[root@xieweihuagogogo ~]# vara="the life is long river"
[root@xieweihuagogogo ~]# echo `expr index "$vara" life`
3
[root@xieweihuagogogo ~]# echo `expr index "$vara" t`
1
[root@xieweihuagogogo ~]# echo `expr index "$vara" l`
5
[root@xieweihuagogogo ~]# echo `expr index "$vara" life`
3
[root@xieweihuagogogo ~]# 
 

获取子串的长度

expr match “$变量” 字符串 :注意这里查找的字符串必须从字符串开始位置算起才能查到,否则就会返回零。

[root@xieweihuagogogo ~]# echo expr match “$vara” life

0

[root@xieweihuagogogo ~]# echo expr match “$vara” the

3

[root@xieweihuagogogo ~]# echo expr match “$vara” the*

3

[root@xieweihuagogogo ~]# echo expr match “$vara” the.*

22

[root@xieweihuagogogo ~]# echo expr index “the leif is long river” life

3

获取子串

  1. ${变量:起始位置}:从首端算起,起始位置为0
  2. ${变量:起始位置:长度大小}
  3. ${变量: 负数位置}:代表从后面的开始算起,起始点为-1, 这里必须要加空格
  4. ${变量:(负数位置):长度大小}
  5. expr substr “$变量” 起始位置 长度大小

示例:

root@xieweihuagogogo ~]# vara="the life is a long river"
[root@xieweihuagogogo ~]# echo ${vara:10}
s a long river
[root@xieweihuagogogo ~]# echo ${vara:12}
a long river
[root@xieweihuagogogo ~]# echo ${vara:12:6}
a long
[root@xieweihuagogogo ~]# echo ${vara: -10:4}
long
[root@xieweihuagogogo ~]# echo ${vara:(-10):4}
long
[root@xieweihuagogogo ~]# echo `expr substr "$vara" 12`
expr: syntax error
[root@xieweihuagogogo ~]# echo `expr substr "$vara" 12 6`
a lon
[root@xieweihuagogogo ~]# echo `expr substr "$vara" 13 6`
a long
[root@xieweihuagogogo ~]# echo `expr substr "$vara" 13`
expr: syntax error
[root@xieweihuagogogo ~]# echo `expr substr "$vara" 13 6`
a long
[root@xieweihuagogogo ~]# 
 

案例1

需求描述

用户输入数字1|2|3|4,可以执行对应的功能;输入q|Q退出交互模式

思路分析:

1、 将不同的功能模块划分,并编写函数

function print_tips
function len_of_string
function del_str
function rep_str_first
function rep_str_all
 

2、 实现第一步所定义的功能函数

function print_tips
 7 {
 8 echo "********************************"
 9 echo "(1) 打印string长度"
 10 echo "(2) 删除字符串中所有的the"
 11 echo "(3) 替换第一个life为code"
 12 echo "(4) 替换全部the为my"
 13 echo "********************************"
 14 
 15 }
 16 
 17 function len_of_string
 18 {
 19echo "${#string}"
 20 
 21 }
 22 
 23 function del_str
 24 {
 25echo ${string//the/}
 26 }
 27 
 28 function rep_str_first
 29 {
 30echo ${string/life/code}
 31 }
 32 function rep_str_all
 33 {
 34echo ${string//the/my}
 35 }
 

3、程序主流程的设计

while true
 39 do
 40echo "[string=$string]"
 41echo
 42print_tips
 43read -p "please input your choice(1|2|3|4|q|Q):" choice
 44 
 45case $choice in
 46 1) len_of_string ;;
 47 2) del_str ;;
 48 3) rep_str_first ;;
 49 4) rep_str_all ;;
 50 q|Q) exit ;;
 51 *) echo "error, input only in {1|2|3|4|q|Q}"
 52esac
 53 done
 

4、 测试结果:

[root@xieweihuagogogo example]# ls
example.sh
[root@xieweihuagogogo example]# sh example.sh 
[string=the life is a long river, the life is a short time]
********************************
(1) 打印string长度
(2) 删除字符串中所有的the
(3) 替换第一个life为code
(4) 替换全部the为my
********************************
please input your choice(1|2|3|4|q|Q):1
50
[string=the life is a long river, the life is a short time]
********************************
(1) 打印string长度
(2) 删除字符串中所有的the
(3) 替换第一个life为code
(4) 替换全部the为my
********************************
please input your choice(1|2|3|4|q|Q):2
life is a long river, life is a short time
[string=the life is a long river, the life is a short time]
********************************
(1) 打印string长度
(2) 删除字符串中所有的the
(3) 替换第一个life为code
(4) 替换全部the为my
********************************
please input your choice(1|2|3|4|q|Q):3
the code is a long river, the life is a short time
[string=the life is a long river, the life is a short time]
********************************
(1) 打印string长度
(2) 删除字符串中所有的the
(3) 替换第一个life为code
(4) 替换全部the为my
********************************
please input your choice(1|2|3|4|q|Q):4
my life is a long river, my life is a short time
[string=the life is a long river, the life is a short time]
********************************
(1) 打印string长度
(2) 删除字符串中所有的the
(3) 替换第一个life为code
(4) 替换全部the为my
********************************
please input your choice(1|2|3|4|q|Q):q
[root@xieweihuagogogo example]# 
 

命令替换

方法1 `command`
方法2 ${command}
 

案例2

示例:

这是我建立的一个用户信息数据
1 username-weihua
2 sex-man
3 other-likeCode
 

接着写一段 shell 代码

 1 #! /bin/ bash 
 2 
 3 index=1
 4 
 5 for user in `cat /opt/example/a | cut -d "-" -f 2`
 6 do
 7echo "this is $index user info:$user"
 8index=$(($index+1))
 9 done
 

测试结果:

[root@xieweihuagogogo example]# ll
total 12
-rw-r--r-- 1 root root 39 Oct 15 17:44 a
-rw-r--r-- 1 root root 871 Oct 15 16:59 example.sh
-rw-r--r-- 1 root root 146 Oct 15 17:42 userInfo.sh
[root@xieweihuagogogo example]# sh userInfo.sh 
this is 1 user info:weihua
this is 2 user info:man
this is 3 user info:likeCode
[root@xieweihuagogogo example]# 
 

案例3

查询今年是多少年,明年是多少年?

echo "that year is: $(date +'%Y')"
that year is: 2019
[root@xieweihuagogogo example]# echo "next year is:$(($(date +'%Y') + 1))"
next year is:2020
[root@xieweihuagogogo example]# 
 

案例4

根据系统时间获取今天还剩下多少个星期,已经过了多少个星期

[root@xieweihuagogogo example]# echo "how much week over:$(($(date +%j) / 7))"
how much week over:41
[root@xieweihuagogogo example]#
[root@xieweihuagogogo example]# echo "how much tommary has week:$(((365 - $(date +%j))/7))"
how much tommary has week:11
[root@xieweihuagogogo example]# 
 

案例5

判定 nginx 进程是否存在,若不存在则自动拉起该进程

 `[root@xieweihuagogogo example]# ps -ef | grep nginx
 root 9209 8988 0 Feb05 pts/9 00:00:00 grep --color=auto nginx
 root 12411 1 0 2018 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c conf/nginx.conf
 nobody 12412 12411 0 2018 ? 00:00:02 nginx: worker process
 [root@xieweihuagogogo example]# `
 

具体判定shell脚本

#!/bin/bash
 nginx_process_number=$(ps -ef | grep nginx | grep -v grep | wc -l)
 if [ $nginx_process_number -eq 0 ];then
 echo "nginx is close"
 # open nginx
 # systemctl start nginx
 else
 echo "nignx alread is start"
 fi
 

执行结果:

[root@xieweihuagogogo example]# sh controllerNginx.sh 
nignx alread is start
[root@xieweihuagogogo example]# 
 

笔者打算将shell编程分为几块将,从浅入深,后期会写一下具体线上案例。希望大家能多多支持我,给笔者一个关注就是笔者最大的写作动力。

文章来源:智云一二三科技

文章标题:解决Shell脚本编程90%工作问题之初遇shell

文章地址:https://www.zhihuclub.com/188289.shtml

关于作者: 智云科技

热门文章

网站地图