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