linux shell综合编程

1、编写函数,实现打印绿色OK和红色FAILED

判断是否有参数,存在为Ok,不存在为FAILED

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 06:26
# * Filename      : question1.sh
#! /bin/bash

function_1() {
        if [ $# -ne 0 ];then
                echo -e "\033[32m ok \033[0m"
        else
                echo -e "\033[31m failed \033[0m"
        fi
}
function_1 $*


2、编写函数,实现判断是否无位置参数,如无参数,提示错误

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 06:26
# * Filename      : question2.sh
#! /bin/bash

function_1() {
	if [ $# -ne 0 ];then
		echo $*
	else
		echo -e "no arguments.error"
	fi
}
function_1 $*

3、编写函数实现两个数字做为参数,返回最大值

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 06:59
# * Filename      : question3.sh
# * Description   : 
# **********************************************************
fun(){
read -p "Please enter then first digit: " num1
read -p "Please enter then second digit: " num2
if [ -z $num1 ];then
	echo "please enter number1"
	exit 9
fi
if [ -z $num2 ];then
	echo "please enter number2"
	exit 9
fi

echo $num1$num2 | egrep -e ^[0-9]+$ &>/dev/null
state=$?
if [ $state -eq 0 ];then
	max=$num1
	for i in $num1 $num2
		do
		if [ $max -lt $i ];then
			max=$i
		fi	
		done
	echo $max
else
	echo "please enter the number"
fi
}
fun 

4、编写函数,实现两个整数位参数,计算加减乘除。

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 07:27
# * Filename      : question4.sh
# * Description   : 
# **********************************************************
fun(){
	echo "$1+$2=$(($1+$2))"
	echo "$1-$2=`let r=$1-$2;echo $r`"
	echo "$1*$2=$(($1*$2))"
	echo "$1/$2=$(($1/$2))"
	echo "$1%$2=$(($1%$2))"
}
fun $1 $2

5、将/etc/shadow文件的每一行作为元数赋值给数组

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 07:35
# * Filename      : question5.sh
# * Description   : 
# **********************************************************
file=/etc/shadow
num=`wc -l < $file`
for i in `seq $num`
do
	arr[$i]=`head -$i $file | tail -1`
done

for i in ${arr[*]}
do 
	echo $i
done

6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 07:47
# * Filename      : question6.sh
# * Description   : 
# **********************************************************
declare -A arr
while read line
do
	type=`echo $line | cut -d : -f 7`
	let arr[$type]++
done < /etc/passwd

for i in ${!arr[*]}
do
echo "$i 的数量为 ${arr[$i]}"
done

7、使用关联数组按扩展名统计指定目录中文件的数量

猜你喜欢

转载自blog.csdn.net/m0_51828898/article/details/128527359