1
到
100
内偶数之和的
shell
编写方法
解:
法一:
[root@station16 tmp]# !v
vim sum.sh
#! /bin/bash
echo "1-100 even number sum....."
let SUM=0
let I=0
for I in {1..50}
do
SUM=$[$SUM+$I*2]
done
echo "the SUM is :$SUM"
~
"sum.sh" 14L , 138C written
[root@station16 tmp]# !b
bash sum.sh
1-100 even number sum.....
the SUM is :2550
[root@station16 tmp]#
法二:
[root@station16 tmp]# vi sum.sh
#! /bin/bash
echo "1-100 even number sum....."
let SUM=0
for (( i=2; i<=100;i=i+2 ))
do
SUM=$(($SUM+$i))
done
echo "the SUM is :$SUM"
~
~
"sum.sh" 12L , 140C written
[root@station16 tmp]# !b
bash sum.sh
1-100 even number sum.....
the SUM is :2550
[root@station16 tmp]#
2.写个脚本,满足以下条件
A.通过参数传递一个路径给脚本,判断此路径是否为目录,是则切换工作目录至此路径下
B.逐个判断文件,如果为普通文件,则说为普通文件,如为目录,则说明为目录
解:
[root@station16 tmp]# !v
vi file.sh
#! /bin/bash
if test -d $1 ; then
cd $1
for FILE in $1/*
do
if test -d $FILE ; then
echo "$FILE is a directory...."
else
echo "$FILE is a regular file...."
fi
done
elif test -f S1; then
echo "$1 is a regular file..."
else
echo "$1 is not exist...."
fi
~
~
~
"file.sh" 20L , 296C written
[root@station16 tmp]# !b
bash -n file.sh
例如:
[root@station16 tmp]# bash file.sh /var/ftp
/var/ftp is not exist....
[root@station16 tmp]#
3写一个脚本
A. 让用户传递两个整数至此脚本
B. 显示一个菜单,选项有1,加 2,减 3,乘 4,除
C. 在用户做出选择后,进行运算,并还回值;
D. 如果用户是错误选项,则表示愤怒。
解:
[root@station16 tmp]# !v
vim ys.sh
#! /bin/bash
echo "**********************************"
echo "1:mean+
2:mean-
3:mean*
#! /bin/bash
echo "**********************************"
echo "
1:mean+
2:mean-
3:mean*
4:mean/
please input (1|2|3|4) to select you want to opeartor..... "
echo "**********************************"
read A
if [ $A -eq 1 ];then
echo "$1+$2=$[$1+$2]"
elif [ $A -eq 2 ];then
echo "$1-$2=$[$1-$2]"
elif [ $A -eq 3 ];then
echo "$1*$2=$[$1*$2]"
elif [ $A -eq 4 ];then
echo "$1/$2=$[$1/$2]"
else
echo "your inputing system donot konwn ...."
fi
例如:
[root@station16 tmp]# bash ys.sh 3 2
**********************************
1:mean+
2:mean-
3:mean*
4:mean/
please input (1|2|3|4) to select you want to opeartor.....
**********************************
4
3/2=1
[root@station16 tmp]# bash ys.sh 3 2
**********************************
1:mean+
2:mean-
3:mean*
4:mean/
please input (1|2|3|4) to select you want to opeartor.....
**********************************
3
3*2=6
[root@station16 tmp]# bash ys.sh 3 2
**********************************
1:mean+
2:mean-
3:mean*
4:mean/
please input (1|2|3|4) to select you want to opeartor.....
**********************************
2
3-2=1
[root@station16 tmp]# bash ys.sh 3 2
**********************************
1:mean+
2:mean-
3:mean*
4:mean/
please input (1|2|3|4) to select you want to opeartor.....
**********************************
1
3+2=5
[root@station16 tmp]# bash ys.sh 3 4
**********************************
1:mean+
2:mean-
3:mean*
4:mean/
please input (1|2|3|4) to select you want to opeartor.....
**********************************
2
3-4=-1
[root@station16 tmp]#
~
~