1. Write a function to print green OK and red FAILED
Determine whether there is a parameter, if it exists it means Ok, if it does not exist it means 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. Write a function to determine whether there are no positional parameters. If there are no parameters, an error will be prompted.
#!/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. Write a function to implement two numbers as parameters and return the maximum value
#!/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. Write a function to implement two integer bit parameters and calculate addition, subtraction, multiplication and division.
#!/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. Assign each line of the /etc/shadow file as an element to the array.
#!/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. Use an associative array to count the number of different types of shells used by users in the file /etc/passwd
#!/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. Use an associative array to count the number of files in a specified directory by extension.