统计家目录加.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