6.26shell习题

统计家目录加.c文件的个数

ls ~ *.c |wc -w

终端输入一个.sh文件,判断文件是否由可执行权限,如果有可执行权限运行脚本,没有可执行权限添加可执行权限后,再运行脚本   

#!/bin/bash
read -p "please enter .sh file" file
touch $file
if [ -x $file ]
then
	./$file
else
	chmod +x $file
	./$file
fi

写一个函数,输出当前用户uid和gid,并使用变量接收结果

#!/bin/bash
fun()
{
uid=`grep ubuntu /etc/passwd|cut -d ":" -f "3"`
gid=`grep ubuntu /etc/passwd|cut -d ":" -f "4"`
}
fun
echo $uid
echo $gid
ubuntu@ubunt

终端输入年月,判断该月有多少天,考虑闰平年的情况

#!/bin/bash
read -p "please enter year" year
read -p "please enter month" month
case $month in
	1)
		echo "there are 31 days in this month"
		;;
	2)
		if [ $((year%4)) -eq 0 -a $((year%100)) -ne 0 -o $((year%400)) -eq 0]
		then
			echo "there are 29 days in this month"
		else
			echo "there are 28 days in this month"
		fi
		;;
	3)
		echo "there are 31 days in this month"
		;;
	4)
		echo "there are 30 days in this month"
		;;
	5)
		echo "there are 31 days in this month"
		;;
	6)
		echo "there are 30 days in this month"
		;;
	7)
		echo "there are 31 days in this month"
		;;
	8)
		echo "there are 31 days in this month"
		;;
	9)
		echo "there are 30 days in this month"
		;;
	10)
		echo "there are 31 days in this month"
		;;
	11)
		echo "there are 30 days in this month"
		;;
	12)
		echo "there are 31 days in this month"
		;;
	*)
		echo "error"
esac

使用for循环,输出九九乘法表

#!/bin/bash
i=0
j=0
for ((i=1;i<=9;i++))
do
	for((j=1;j<=$i;j++))
	do
		echo -n  $i "*" $j "=" $((i*j))
	done
	echo
done

使用for循环,找到家目录下的所有.c文件,如果文件有内容编译该文件,如果文件中没有内容,删除文件

#!/bin/bash

cd ~

for file in *.c
do
	if [ -s $file ]
	then
		gcc -o "${file%.c}" "$file"
	else
		rm $file
	fi
done
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值