嵌入式学习-C高级-Day3

Day3

思维导图

在这里插入图片描述

作业

1在终端输入一个用户名

定义有参又返函数,返回用户在/etc/passwd文件中的行号

参数:用户名

返回:行号

定义有参又返函数,返回用户在.etc.passwd文件中的解析器

参数:用户名

返回:解析器

定义有参又返函数,返回用户的uid和gid

​ 参数:用户名

返回:uid gid

代码
#!/bin/bash
a=$1
lineid()
{
	b=`grep -n "$1" /etc/passwd | cut -d ":" -f 1`
	echo $b
}
lineid $a

bash()
{
	c=`grep "$1" /etc/passwd`
	d=${c:0-4}
	echo $d
}
bash $a

id()
{
	u=`grep "$1" /etc/passwd | cut -d ":" -f 3`
	g=`grep "$1" /etc/passwd | cut -d ":" -f 4`
	echo "uid=$u gid=$g"
}
id $a
运行结果

在这里插入图片描述

2

定义数组存储软件名(oneko bastet frozen-bubble)使用select…in循环数组,实现页面的交互

使用case in 判断软件是否存在(执行),正常运行则不下载,运行失败则下载并运行

ps查看进程

代码
#!/bin/bash

arr=(oneko bastet frozen-bubble)
select varname in ${arr[@]}
do
	case $varname in
		"oneko")
			if [ `find /usr/games/ -name $varname` ]
			then
				echo success
				oneko
			else
				echo unexist
				sudo apt-get install oneko
				oneko
			fi
			;;
		"bastet")
			if [ `find /usr/games/ -name $varname` ]
			then
				echo success
				bastet
			else
				echo unexist
				sudo apt-get install bastet
				bastet
			fi
			;;
		"frozen-bubble")
			if [ `find /usr/games/ -name $varname` ]
			then
				echo success
				frozen-bubble
			else
				echo unexist
				sudo apt-get install frozen-bubble
				frozen-bubble
			fi
			;;
		*)
			;;
	esac
done

运行结果

在这里插入图片描述在这里插入图片描述

3输入数组元素,计算数组的最大值

代码
#!/bin/bash
read -a arr
n=${#arr[*]}
max=${arr[0]}
for ((i=0;i<n;i++))
do
	if [ $max -lt ${arr[i]} ]
	then
		t=$max
		max=${arr[i]}
		arr[i]=$t
	fi
done
echo $max
运行结果

在这里插入图片描述

4打印图形

代码
#!/bin/bash
read n
for ((i=0;i<n;i++))
do
	for ((j=0;j<n;j++))
	do
		if [ $j -lt $i ]
		then 
			echo -n " "
		else
			echo -n "*"
		fi
	done
	echo
done
运行结果

在这里插入图片描述

5输入一个文件名

判断是否在当前目录下存在,

如果是一个目录,则直接输出是目录

如果是一个链接文件则输出文件的详细信息

如果存在则判断是否是一个普通文件,如果是普通文件则判断是否具备

可读可写可执行权限,如果具备权限,写入hello,不具备权限,则添加读写执行权限,写入hello

代码
#!/bin/bash

read file

if [ -e $file ]
then
	echo exist
else
	echo unexist
	exit
fi

if [ -d $file ]
then
	echo "dir"
elif [ -L $file ]
then
	a=`ls -lh $file`
	echo $a
fi
if [ -f $file ]
then
	if [ -r $file -a -w $file -a -x $file ]	
	then
		echo "hello" >> $file
	else
		chmod 777 $file
		echo "hello" >> $file
	fi
fi
运行结果

在这里插入图片描述

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值