A. 在Linux系统下,编写一个Shell脚本,从键盘读入5个整数,然后显示最大数、最小数、和平均数。
#!/bin/bash
#任意输入5个数,判断最大值,最小值,总和
s=0
read -p "please input:" num
s=$(($s+$num))
max=$num
min=$num
avg=$(($s/5))
for i in `seq 4`
do
read -p "please input:" num
s=$(($s+$num))
if [ $num -le $min ];then
min=$num
fi
if [ $num -ge $max ];then
max=$num
fi
done
echo max:$max min:$min avg:$avg
(1)用shell脚本写一个BMI计算器扩展阅读:
for循环:
一般格式为:
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
写成一行:
for var in item1 item2 ... itemN; do command1; command2… done;
B. 如何用php做一个bmi计算器
就是在Eclipse里面新建一个Android工程,根据BMI的计算公式来计算,简单的布局一下,可以进行身高体重的输入,有确定按钮根据输入值进行计算就可以了。
C. 刚学shell脚本写了个小 计算器,加减除都可以,乘法不行,哪儿错了
你的问题在:elif [ $fangfa = " \* " ]; 这句并不能判断到“*“乘号;
正确的结果如下:
#!/bin/bash
echo "first number"
read a
echo "fangfa"
read fangfa
echo "second number"
read b
if [ "$fangfa" = "+" ];then
echo $(($a+$b))
elif [ "$fangfa" = "-" ];then
echo $(($a-$b))
elif [ "$fangfa" = "*" ];then
echo $(($a*$b))
elif [ "$fangfa" = "/" ];then
echo $(($a/$b))
fi
===============================================
脚本优化版本:
#!/bin/bash
read -p "input first number: " num1
read -p "input operator: " operator
read -p "input second number: " num2
if [ "$operator" == "+" ];then
echo " num1 + num2 = $(($num1+$num2))"
elif [ "$operator" == "-" ];then
echo "num1 - num2 = $(($num1-$num2))"
elif [ "$operator" == "*" ];then
echo "num1 * num2 = $(($num1*$num2))"
elif [ "$operator" == "/" ];then
echo "num1 / num2 = $(($num1/$num2))"
fi
D. 编写一个shell脚本,完成以下功能,请高手进来!
#!/bin/sh
# vim: set sw=4 ts=4 et:
ver="0.1"
help()
{
cat < rotatefile -- rotate the file name
USAGE: rotatefile [-h] filename
OPTIONS: -h help text
EXAMPLE: rotatefile out
This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1
and create an empty out-file
The max number is 10
version $ver
HELP
exit 0
}
error()
{
echo "$1"
exit 1
}
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;;
--) break;;
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done
# input check:
if [ -z "$1" ] ; then
error "ERROR: you must specify a file, use -h for help"
fi
filen="$1"
# rename any .1 , .2 etc file:
for n in 9 8 7 6 5 4 3 2 1; do
if [ -f "$filen.$n" ]; then
p=`expr $n + 1`
echo "mv $filen.$n $filen.$p"
mv $filen.$n $filen.$p
fi
done
# rename the original file:
if [ -f "$filen" ]; then
echo "mv $filen $filen.1"
mv $filen $filen.1
fi
echo touch $filen
touch $filen
E. 用C语言编写程序:输入体重和身高,输出体重指数BMI
例子如下:
知识扩展:
BMI指数(即身体质量指数,简称体质指数又称体重,英文为Body Mass Index,简称BMI),是用体重公斤数除以身高米数平方得出的数字,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。主要用于统计用途,当我们需要比较及分析一个人的体重对于不同高度的人所带来的健康影响时,BMI值是一个中立而可靠的指标。
BMI值原来的设计是一个用于公众健康研究的统计工具。当我们需要知道肥胖是否对某一疾病的致病原因时,我们可以把病人的身高及体重换算成BMI值,再找出其数值及病发率是否有线性关联。不过,随着科技进步,现时BMI值只是一个参考值。要真正量度病人是否肥胖,还需要利用微电力量度病人的阻抗,以推断病者的脂肪厚度。因此,BMI的角色也慢慢改变,从医学上的用途,变为一般大众的纤体指标。
F. 使用gedit编写特定功能的shell脚本,熟悉shell脚本的变量、表达式和流程控制。
以下代码已经经过测试,可以满足你的所有要求:
#!/bin/bash
echo-e[1].listdirectory\
echo-e[1].deletedirectory\
echo-e[3].displayfilecontent\
echo-e[4].deletefile\
printf'inputthe0exitthisprogam. Pleaseinputyourchoice:'
read-t11num
case$numin
0)
exit
;;
1)
read-p':'-t11dir
if[-d$dir];
then
ls$dir
else
echo-e"the$dirnotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
1)
read-p':'-t11dir
if[-d$dir];
then
rmdir$dir
else
echo-e"the$dirnotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
3)
read-p':'-t11ffile
if[-f$fflie];
then
cat$ffile
else
echo-e"the$ffilenotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
4)
read-p':'-t11ffile
if[-f$fflie];
then
rm$ffile
else
echo-e"the$ffilenotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
*)
echo'idonotknowwhatyouinput!'
sleep1
sh/home/testshell/custom.sh
;;
esac
exit0
G. 如何用shell编写一个简单的计算器
如果只做四个简单的运算这应应该可以
shell脚本代码如下:
#!/bin/bash
read -p "input num1:" a
read -p "input num2:" b
read -p "input operator:" o
case $o in
+) let "res=a + b"
echo $res;;
-) let "res=a - b"
echo $res;;
/) awk 'BEGIN{printf "%.2f\n",'$a'/'$b'}';;
*) let "res=a * b"
echo $res;;
esac
H. 如何用shell脚本实现下列功能
楼上两个真经典, 受教受教. 我菜鸟冒昧解释解释两位的命令
cat log.txt| gawk -F'#| ' '{print $5}'|sort|uniq -c|gawk '{print $2,$1}'
-F'#| ' 利用#或者空格分隔, 第一次碰到学习了
sort 排序
uniq -c 合并重复, 统计个数
awk -F"[# ]" '{a[$5]=a[$5]"z"}END{for( i in a )print i"\n", length(a[i])}' temp.log
太nb了, 佩服佩服awk用的如此之娴熟
-F"[# ]" 同样用#或者空格分隔, 学习学习
a[$5]=a[$5] awk关联数组, 太经典了, a[$5]用字符串做数组a的下标, 而字符串本身就是IP地址, =a[$5]"z"太nb了, 看了半天才明白, 数字内部保存的只是z, 如果遇到重复的下标只是保存了zz, NB呀高手. 后面结束的时候循环只是打印出来了下标i(IP地址), 而数组的内容实际上是a[i](也就是1个或者多个z), 然后用内置函数length取出来数字内有多少个z就是重复了几次. 高!
厚颜无耻的补充一下排序加上sort就行了
awk -F"[# ]" '{a[$5]=a[$5]"z"}END{for( i in a )print i" ", length(a[i]) | "sort"}' datafile
to 楼上
谦虚谦虚了
看了优化命令
awk -F"#| " '{a[$5]++}END{for(i in a)print i"\n"a[i]}' temp.log 1>aaa.txt
不错, 我又没有想到!!! 直接自增1 不用length统计了直接打印. 太棒了