shell的基本应用

三、函数的参数传递
1、使用变量的方式传递固定值
[root@manager-61~/cxp]# cat function.lx02.sh

!/usr/bin/bash

fun_1(){
echo "$num"
}
num=10
fun_1

[root@manager-61~/cxp]# sh function.lx02.sh
10
2、使用变量的方式传递可变的值
[root@manager-61~/cxp]# cat function.lx03.sh

!/usr/bin/bash

fun_1(){
echo "$num"
}
num=$1 #执行脚本时将脚本的位置变量的第一个参数传给num
fun_1

[root@manager-61~/cxp]# sh function.lx03.sh 6
6
[root@manager-61~/cxp]# sh function.lx03.sh hello
hello
3、函数传参示例,传递多个位置参数
方式一
[root@manager-61~/cxp]# cat function.lx04.sh

!/usr/bin/bash

fun_1(){
echo "$1"
}
fun_1 $1
fun_1 $2
fun_1 $3

[root@manager-61~/cxp]# sh function.lx04.sh 11 22 33
11
22
33

[root@manager-61~/cxp]# cat function.lx05.sh

!/usr/bin/bash

fun_1(){
echo "$1" "$2" "$3"
}
fun_1 $1 $2 $3
[root@manager-61~/cxp]# sh function.lx04.sh 10 20 30
10
20
30

  • 案例实例
    [root@manager-61~/cxp]# cat function.lx05.sh

!/usr/bin/bash

cale (){
case $2 in
+)
echo $1 + $3 = $[ $1 + $3 ]
;;
-)
echo $1 - $3 = $[ $1 - $3 ]
;;
x)
echo $1 * $3 = $[ $1 * $3 ]
;;
/)
echo $1 / $3 = $[ $1 / $3 ]
;;
%)
echo $1 % $3 = $[ $1 % $3 ]
esac
}
cale $1 $2 $3
[root@manager-61~/cxp]# sh function.lx05.sh 20 + 30
20 + 30 = 50
[root@manager-61~/cxp]# sh function.lx05.sh 20 / 30
20 / 30 = 0
[root@manager-61~/cxp]# sh function.lx05.sh 20 % 30
20 % 30 = 20
[root@manager-61~/cxp]# sh function.lx05.sh 20 x 30
20 * 30 = 600
四、函数状态返回
shell 的函数返回值,也算是退出的状态,在shell中只有 echo 、return 两种方式

1、使用return 返回值:只能返回1-255 的整数,函数使用return 返回值,通常只是用来供其他地方调用状态,因此通常返回0/1;0表示成功,1表示失败,
2、使用echo 返回值:使用 echo 可以返回任何字符结果,通常用来返回数据,比如一个字符串值,或者 列表值
1、shell 函数echo 、return的返回值
[root@manager-61~/cxp]# cat function.lx06.sh

!/usr/bin/bash

fun_echo_return(){
echo 100
return 1
}
hh=fun_echo_return
echo "函数的状态码是 $?"
echo "函数的返回内容 $hh"

[root@manager-61~/cxp]# sh function.lx06.sh
函数的状态码是 1
函数的返回内容 100
2、shell 函数echo 、return的使用场景
[root@manager-61~/cxp]# cat function.lx07.sh

!/usr/bin/bash

file=/etc/passwd
tt_file(){
if [ -f $file ];then
return 0
else
return 1
fi
}
tt_file
if [ $? -eq 0 ];then
echo "$file 存在"
elif [ $? -eq 1 ];then
echo "$file 不存在"
fi

[root@manager-61~/cxp]# sh function.lx07.sh
/etc/passwd 存在

案例实例
需求一:猜数字游戏

1、如果用户输入的数值大于0小于10,则返回0

2、如果用户的输入数值大于等于10且小于20,则返回1

3、如果用户输入的数值大于等于20且小于30,则返回2

4、输入其他数字则返回3

root@manager-61~/cxp]# cat function.lx08.sh

!/usr/bin/bash

Num(){

read -p "请输入你要输入的数字:" Action
if [[ $Action =~ [1]+$ ]];then

if [ $Action -ge 0 -a $Action -lt 10 ];then
return 0
elif [ $Action -ge 10 -a $Action -lt 20 ];then
return 1
elif [ $Action -ge 20 -a $Action -lt 30 ];then
return 2
else
return 3
fi
else
echo "请输入纯数字,不要乱输:"
fi
}

Num

if [ $? -eq 0 ];then
echo "你输入的数值是:$Action ,大于0且小于10"
elif [ $? -eq 1 ];then
echo "你输入的数值是:$Action ,大于等于10且小于20"
elif [ $? -eq 2 ];then
echo "你输入的数值是:$Action ,大于等于20且小于30"
else
echo "你是输入的数字不在游戏范围"
exit
fi

Day-linux - shell 脚本的 数组
一、数组的基本概述
1、什么是数组
数组其实也是变量,传统的变量只能是存储一个值,但数组可以存储多个值

2、数组的分类

  1. 普通数组:只能使用整数作为数组索引

  2. 关联数组:可使用字符串作为数组索引

[root@manager-61~/cxp]# unset infor
[root@manager-61~/cxp]# declare -A infor
[root@manager-61~/cxp]# infor=([name]=cxp [age]=18 [skill]=linux)
[root@manager-61~/cxp]# echo ${infor[name]}
cxp
[root@manager-61~/cxp]# echo ${infor[age]}
18
[root@manager-61~/cxp]# echo ${infor[@]}
cxp 18 linux
[root@manager-61~/cxp]# echo ${!infor[@]}
name age skill
[root@manager-61~/cxp]#

二、数组的基本使用
1、普通数组仅能使用整数来作为索引
1)普通数组的复制
方式一 针对每个索引进行赋值 # (数组名[索引]=变量值)
[root@manager-61~/cxp]# array1[0]=linux
[root@manager-61~/cxp]# array1[1]=k8s
[root@manager-61~/cxp]# echo ${array2[1]}
k8s

方式二 一次赋多个值# (数组名=(多个变量))
[root@manager-61~/cxp]# array3=(tom jack alice)
[root@manager-61~/cxp]# declare -a |grep "array3"
declare -a array3='([0]="tom" [1]="jack" [2]="alice")'

2)如何查看普通数组的赋值与访问数组的内容

查看普通数组的

[root@manager-61~/cxp]# declare -a

查看访问数组中

[root@manager-61~/cxp]# array3=(tom jack alice)
[root@manager-61~/cxp]# echo ${array3[@]}
tom jack alice
[root@manager-61~/cxp]# echo ${array3[]}
tom jack alice
[root@manager-61~/cxp]# echo ${!array3[
]}
0 1 2
[root@manager-61~/cxp]# echo ${!array3[@]}
0 1 2
[root@manager-61~/cxp]# echo ${#array3[@]}
3
2、关联数组能使用字符串的方式作为索引
1)关联数组的赋值

1.必须先声明这是一个关联数组

[root@manager-61~/cxp]# declare -A info

2.方式一 关联数组的赋值 (数组名[索引]=变量)

[root@manager-61~/cxp]# declare -A info
[root@manager-61~/cxp]# info[index1]=pare
[root@manager-61~/cxp]# info[index2]=apple
[root@manager-61~/cxp]# info[index3]=linux
[root@manager-61~/cxp]# echo ${info[index1]}
pare

3.方式二 关联数组的赋值 (数组名=([索引1]=变量值1 [索引2]=变量值2))

[root@manager-61~/cxp]# declare -A info3
[root@manager-61~/cxp]# info3=([index1]=linux [index2]=cxp [index3]=dsb)
[root@manager-61~/cxp]# echo ${info3[index2]}
cxp

4.查看关联数组

[root@manager-61~/cxp]# declare -A |grep "info"
2)如何访问关联数组中的值
[root@manager-61~/cxp]# echo ${info[index1]}
pare
[root@manager-61~/cxp]# echo ${info3[index2]}
cxp

[root@manager-61~/cxp]# echo ${info3[index2]}
cxp
[root@manager-61~/cxp]# echo ${info3[@]}
linux cxp dsb
[root@manager-61~/cxp]# echo ${!info3[@]}
index1 index2 index3
[root@manager-61~/cxp]# echo ${#info3[@]}
3
三、数组的遍历与循环
1.遍历通过数组元数的个数进行

2.通过数组元数的索引进行遍历

将统计的对象作为数组的索引仅针对关联数据

1、普通数组的赋值与遍历示例
示例一:读取/etc/hosts文件进行遍历赋值

[root@manager-61~/cxp]# cat shuzu.lx01.sh

!/usr/bin/bash

批量的给数组进行赋值(普通数组)

while read line
do
hosts[i++]=$line
done</etc/hosts

遍历数组的数据

for i in ${!hosts[@]}
do
echo "hosts 数组的索引:$i hosts 数组的值:${hosts[$i]}"
done

[root@manager-61~/cxp]# sh shuzu.lx01.sh
hosts 数组的索引:0 hosts 数组的值:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
hosts 数组的索引:1 hosts 数组的值:::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
示例二:使用关联数组统计文件中的m 和 f 性别
[root@manager-61~/cxp]# cat sex.txt
java f
alice f
tom m
rose m
dsb f
gdx m
cxp f
wang f
root@manager-61~/cxp]# cat shuzu.lx02.sh

!/usr/bin/bash

declare -A sex

循环读入sex.txt的文件

while read line
do

将每行进行切割,保留需要统计的

type=$(echo $line|awk '{print $2}')
#赋值 统计
let sex[$type]++
done<sex.txt

遍历整个数组

for i in ${!sex[@]}
do
echo "索引的名称 $i 索引的次数${sex[$i]} "
done

[root@manager-61~/cxp]# sh shuzu.lx02.sh
索引的名称 f 索引的次数5
索引的名称 m 索引的次数3

示例三 : 统计 、etc/passwd下的登录的shell
[root@manager-61~/cxp]# awk -F ":" '{Shell[$7]++} END { for (i in Shell) print i, Shell[i]}' /etc/passwd
/bin/sync 1
/bin/bash 10
/sbin/nologin 25
/sbin/halt 1
/sbin/shutdown 1

[root@manager-61~/cxp]# cat shuzu.lx03.sh

!/usr/bin/bash

declare -A Shell
while read line
do
Dl=$(echo $line |awk -F ":" '{print $NF}')
let Shell[$Dl]++
done</etc/passwd
for i in ${!Shell[@]}
do
echo "索引的名称 $i 索引的次数 ${Shell[$i]} "
done

[root@manager-61~/cxp]# sh shuzu.lx03.sh
索引的名称 /sbin/nologin 索引的次数 25
索引的名称 /bin/sync 索引的次数 1
索引的名称 /bin/bash 索引的次数 10
索引的名称 /sbin/shutdown 索引的次数 1
索引的名称 /sbin/halt 索引的次数 1
Day62-linux - Sed 文本处理
一、正则表达式
1572915211486

3.正则表达式注意事项
1..正则表达式应用非常厂泛,存在于各种编程语言中。
2.正则表达式和L inux的通配符以及特殊字符是有区别的。
3.要想学好grep、sed、 awk首先就需要对正则表达式有-定的了解。只有了解了规则,才能灵活
的运用。

[root@manager-61~/scripts/grep]# ifconfig eth0 |grep "inet "|egrep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
10.0.0.61
255.255.255.0
10.0.0.255

[root@manager-61~/scripts/grep]# egrep -v "$|#|[2]+#" /etc/nginx/nginx.conf

[root@manager-61~/scripts/grep]# egrep "HTTP/[1|2|3].[0|1|2|3]" nginx.log -o |sort


  1. 0-9 ↩︎

  2. [:space:] ↩︎

posted on 2019-11-09 15:02  ☆夢醒ず歲月風塵  阅读(129)  评论(0)    收藏  举报

导航

欢迎与我联系