Linux 实验五 shell编程实验二

一、实验目的

综合Linux常用命令和vi编译器的使用,熟练掌握shell脚本编程

二、实验内容

1.编写一个脚本,求斐波那契数列的前10项及总和。

#!/bin/bash
n1=1
n2=1
echo -n "$n1+$n2"
sum=2
for((i=1;i<=8;i++))
do
tmp=`expr $n1 + $n2`
echo -n "+$tmp"
((n1=n2))
((n2=tmp))
sum=`expr $sum + $tmp`
done
echo "=$sum"

2、编写一个脚本,求一个数的逆序

#!/bin/bash
echo -n
read n
count=0
while [ $n -gt 0 ]
do
n1=`expr $n % 10` 
n2=`expr $n / 10` 
n=$n2
while [ $n2 -gt 0 ]
do
n3=`expr $n2 % 10`
n2=`expr $n2 / 10`
if [ $n1 -lt $n3 ]
then
count=`expr $count + 1`
fi
done
done
echo $count


3、设计一个shell程序,在用户主目录下建立一个userdata目录,在此目录下再建立5个目录,即user1~user5,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行(注意:最后删除自己所建立的,目录,恢复原样,以便后面同学做此实验)

#!/bin/bash
mkdir -m 754 /home/lyly/userdata
i=1
temp=/home/lyly/userdata/user
while [ $i -le 5 ]
do
mkdir -m 754 $temp$i
i=`expr $i + 1`
done

4、用shell编程,判断一文件是不是普通文件,如果是是将其拷贝到 /home/student/test 目录下,用命令查看test目录内容,最后再删除test目录。

#!/bin/bash
mkdir -m 777 /home/lyly/test
echo -n "input filename:"
read filename
if test -f "$filename"
then
  echo "The $filename is a regular file!"
  cp $filename /home/lyly/test
  ls /home/lyly/test
  cd test
  rm $filename
  echo
  ls /home/lyly/test
else
  echo "The $filename is not a regular file!"
fi

5、编写脚本,统计指定目录下普通文件及目录文件的数目,要求不能使用wc统计命令

#!/bin/bash
echo "input directory name:"
read name
cd $name
count=0
for files in *
do
  if[ -f "$files" ]
  	then count=`expr $count +1`
elif [ -d "$files" ]
	then count=`expr $count + 1`
fi
done
echo "There are $count files in `pwd`"
  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值