作业九 Shell脚本编程

● 编写脚本程序备份用户指定的文件,将文件备份到目录名 _backup中(若目录不存在则自动建立),备份文件的文件名格式为文件名_bak_年月日_时分秒。

代码:

#!/bin/bash
# eg6-2.sh
if [ ! -d ~/_backup ]
then
	mkdir ~/_backup
fi
if  [  $#  -eq  0  ]
then
	echo "出错:未输入指定备份的文件名"
elif  [  $#  -eq  1  ]
then
	ba1=`date +%Y%m%d_%H%M%S`
	cp $1 ~/_backup/$1_bak_$ba1
	echo "$1	backup successful"
else
	for file in $* 
	do
		ba2=`date +%Y%m%d_%H%M%S`
		cp $file ~/_backup/${file}_bak_$ba2
		echo "$file	backup successful"
	done 
fi

● 编写一个shell脚本程序,它能根据输入的命令行参数采取不同的动作:如果是目录,则列出该目录中的文件;如果是可执行的文件,则用shell执行之;如果是可读的文件,则分屏显示其内容。

代码:

#!/bin/bash
#ex6-3.sh
echo "请输入:"
read file
if [ -d $file ]
then 
	echo "文件列表:"
	ls $file/
elif [ -x $file ]
then 
	echo "运行结果:"
	./$file
elif [ -f $file ]
then
	echo "文件内容:"
	more $file
else
	echo "wdnmd"
fi

● 写一个脚本文件,该脚本运行时带用户名作为参数,在/etc/passwd文件中查找用户,如有,则输出“ in the /etc/passwd”;否则输出” no such user on our system”。

代码:

#!/bin/bash
#ex6-4.sh
echo "Please input:"
read name
rs=`cut -f 1 -d: /etc/passwd|grep ^${name}$`
if [ $rs ]
then
	echo " <${name}> in the /etc/passwd "
else
	echo " no such user on your system "
fi

● 设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。

代码:

#!/bin/bash
#ex6-6.sh
echo "Please input:"
read name
rs=`cut -f 1 -d: /etc/passwd|grep ^${name}$`
if [ $rs ]
then
	echo " <${name}> in the /etc/passwd "
else
	echo " no such user on your system "
fi
[ssy@localhost ~]$ cat ex6-6.sh 
#!/bin/bash
#ex6-6.sh
for((i=1;i<=50;i++))
do
	mkdir -p ~/userdata/user$i
	chmod 754 ~/userdata/user$i
done
echo "finished"

● 给当前目录里的所有文件加后缀名“ .html”。

代码:

#!/bin/bash
#ex6-7.sh
to=`ls ~/test/`
for file in $to 
do
	mv ~/test/$file ~/test/${file}.html
done

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只水熊虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值