当前位置:网站首页>shell函数数组作业
shell函数数组作业
2022-07-19 07:18:00 【MZdzt】
1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED
#!/bin/bash
color()
{
if [ $# -eq 0 ]
then
echo -e "\e[31m failed \e[0m"
else
echo -e "\e[32m ok \e[0m"
fi
}
read -p "place enter:" enter
color $enter
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
#!/bin/bash
pd()
{
if [ $# -eq 0 ]
then
echo "error"
fi
}
read -p "place enter:" enter
pd $enter
3、编写函数实现两个数字做为参数,返回最大值
#!/bin/bash
max()
{
if [ $num1 -gt $num2 ]
then
echo $num1
else
echo $num2
fi
}
read -p "place enter two numbers:" num1 num2
max $num1 $num2
4、编写函数,实现两个整数位参数,计算加减乘除
#!/bin/bash
js()
{
case $num in
1)
re=$(($num1+$num2))
echo "$num1+$num2="$re
;;
2)
re=$((num1-num2))
echo "$num1-$num2="$re
;;
3)
re=$((num1*num2))
echo "$num1*$num2="$re
;;
4)
re=$((num1/num2))
echo "$num1/$num2="$re
esac
}
read -p "place enter two numbers:" num1 num2
read -p "1、+ 2、- 3、* 4、/:" num
if [ $num -eq 4 -a $num1 -eq 0 ]
then
echo "0 not can be num1"
else
js $num $num1 $num2
fi
5、将/etc/shadow文件的每一行作为元数赋值给数组
#!/bin/bash
num=`wc -l < /etc/shadow`
for ((i=0;i<=$num;i++))
do
array[$i]=$(head -$i /etc/shadow | tail -1)
done
for i in ${array[*]}
do
echo $i
done
边栏推荐
猜你喜欢
Oracle trigger SQL error
2022年山东省安全员C证操作证考试题及答案
View the intranet mapping between host IP port and fast resolution
2022P气瓶充装考试题库及答案
如何利用快解析远程访问NAS、FTP、Web服务
虚拟展会结合AI数字人,帮助企业解决当下困局
Software testing - learning notes 4
2行代码生成纯色桌面背景
8000字超详解自定义结构体类型
OSError: exception: access violation writing 0x0000000000000000
随机推荐
(illustration) the context of FPN
215位“双一流”考生考研,被这所“双非大学”录取!
TortoiseSVN Error : Previous operation has not finished; run ‘cleanup‘ if it was interrupted异常解决办法
MySQL optimization to be improved
Will it be optimized if you add a condition to sort by half insertion?
Wechat applet bindinput and click the button to assign the content of the current text
About the recent "deja_vu3d feature set" column is not updated, the column is not updated
Redis data types and usage scenarios
MYSQL有类似ROWID虚拟列吗?
快解析结合绿盾文档加密软件
NLP pre training model for knowledge enhancement [introducing triplet vectors in the knowledge map into the pre training model]
MoViNets系列模型,手机端实时分类视频的好帮手
[cloud native kubernetes] binary build kubernetes cluster (Part 1) -- deploy etcd cluster and single master
Processing of paging data
一文看懂25个神经网络模型 - 人工神经网络的典型模型
JSON.parse出现\n时解析错误
Several ways of Intranet penetration
Supervisor usage understanding
软件开发设计流程
2022爱分析・银行数字化实践报告