百度
360搜索
搜狗搜索

linux shell编程题,Linux编程练习:编写shell程序,求各操作数平方的累加和。详细介绍

本文目录一览: 在Linux系统下,编写一个Shell脚本,从键盘读入5个整数,然后显示最大数、最小数、和平均数。

楼上的程序我执行到第“5”行那就出错了,更别说结果了;我专门写了简单易懂的,程序如下:
#!/bin/bash
echo " Please input 5 word "
echo -n " word 1: "
read word1
echo -n " word 2: "
read word2
echo -n " word 3: "
read word3
echo -n " word 4: "
read word4
echo -n " word 5: "
read word5
if [ "$word1" -ge "$word2" -a "$word1" -ge "$word3" -a "$word1" -ge "$word4" -a "$word1" -ge "$word5" ]
then
echo -n " Max: $word1"
elif [ "$word2" -ge "$word1" -a "$word2" -ge "$word3" -a "$word2" -ge "$word4" -a "$word2" -ge "$word5" ]
then
echo -n " Max: $word2"
elif [ "$word3" -ge "$word2" -a "$word3" -ge "$word1" -a "$word3" -ge "$word4" -a "$word3" -ge "$word5" ]
then
echo -n " Max: $word3"
elif [ "$word4" -ge "$word2" -a "$word4" -ge "$word3" -a "$word4" -ge "$word1" -a "$word4" -ge "$word5" ]
then
echo -n " Max: $word4"
else
echo -n " Max: $word5"
fi
if [ "$word1" -le "$word2" -a "$word1" -le "$word3" -a "$word1" -le "$word4" -a "$word1" -le "$word5" ]
then
echo -n " Min: $word1"
elif [ "$word2" -le "$word1" -a "$word2" -le "$word3" -a "$word2" -le "$word4" -a "$word2" -le "$word5" ]
then
echo -n " Min: $word2"
elif [ "$word3" -le "$word2" -a "$word3" -le "$word1" -a "$word3" -le "$word4" -a "$word3" -le "$word5" ]
then
echo -n " Min: $word3"
elif [ "$word4" -le "$word2" -a "$word4" -le "$word3" -a "$word4" -le "$word1" -a "$word4" -le "$word5" ]
then
echo -n " Min: $word4"
else
echo -n " Min: $word5"
fi
echo " Ave: expr '($word1 \+ $word2 \+ $word3 \+ $word4 \+ $word5)\/5'"
本程序在我电脑是可以执行出结果的,要是在你的系统执行出错,请告知我。谢谢。
1 #!/bin/bash
2 echo "**********please iput 5 number***********************"
3 echo "you must input one and press enter until input 5 number"
4 read x
5 max=$x
6 min=$x
7 avg=$x
8 i=1
9 while [ $i -lt "5" ]
10 do
11 let "i+=1"
12 read x
13 if [ "$x" -gt "$max" ]
14 then max=$x
15 fi
16 if [ "$x" -le "$min" ]
17 then min=$x
18
19 fi
20 let "avg=$avg+$x"
21 done
22 avg=$(echo "scale=3;$avg/5"|bc -l) #scale控制平均数的精度
23 echo "***********the result***************"
24 echo "max=$max "
25 echo "min=$min "
26 echo "avg=$avg"
  
#!/bin/bash
read num
max=$num
min=$num
for((i<1;i<10;i++))
do
read num
if test $max -le $num
then
max=$num
fi
if test $min -ge $num
then
min=$num
fi
done
echo "max=$max,min=$min"
#!/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:" nums=$(($s+$num))
if [ $num -le $min ];thenmin=$numfi
if [ $num -ge $max ];thenmax=$numfi
done
echo max:$max min:$min avg:$avg
扩展资料:
for循环:
一般格式为:
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
写成一行:
for var in item1 item2 ... itemN; do command1; command2… done;

阅读更多 >>>  linux下安装软件病毒吗

linux(shell)脚本编程题目求教(200分请教)

1. 用vi打开一个空文件,文件名随意,然后在第一行写:
#!/bin/bash
另起一行跟上要使用的命令,比如ls date之类的,一条命令一行。
退出vi后,在命令行下输入 chmod +x 文件名 给文件增加运行权限
最后用 ./文件名 执行这个文件就可以了。
2. 环境变量用 env 和 set 2个命令可以看到,在命令行下直接输入就可以了,至于具体内容,太多,你得自己去查
3.
for 循环:
i=1
for i in 1..100; do
i=$(($i + 1))
done
while循环
i=1
while [ $i -le 100 ]; do
i=$(($i + 1))
done
父进程安装一个SIGCHLD处理程序,在子进程终止时执行
1. 用vi打开一个空文件,文件名随意,然后在第一行写:
#!/bin/bash
另起一行跟上要使用的命令,比如ls date之类的,一条命令一行。
退出vi后,在命令行下输入 chmod +x 文件名 给文件增加运行权限
最后用 ./文件名 执行这个文件就可以了。
2. 环境变量用 env 和 set 2个命令可以看到,在命令行下直接输入就可以了,至于具体内容,太多,你得自己去查
3.
for 循环:
i=1
for i in 1..100; do
i=$(($i + 1))
done
while循环
i=1
while [ $i -le 100 ]; do
i=$(($i + 1))
done
算了,这些都能做,但做起来太麻烦,放弃了

linux的shell脚本编程,求两数字间所有偶数的和,我是小白,不太懂。

错的地方太多了
每个运算符和数字/变量之间应该有空格,如“ 1 > 2 ”
小于等于不能用“<=”表示,应该用 -le
使用“>”“<”等运算符时,必须用“[[ ]]”符号扩起来以进行转义(while那里就应该改成 while ( [[ $c -le $b ]] )
我把改好后的贴出来给你看看吧(虽然按照你输入的,一直没运算出结果)
#@/bin/basha=$1b=$2c=$1d=0while ( [[ $c -le $b ]] )doif [[ $c%2 == 0 ]]thend=$c+$dc=$c+1fidoneecho $d
这部分主要讨论数学相关的shell脚本编程。
加法运算
新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。
复制代码代码如下:
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(expr "$a" + "$b")
echo $a + $b = $x
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Additions.sh
[root@tecmint ~]# chmod 755 Additions.sh
[root@tecmint ~]# ./Additions.sh
“Enter the First Number: ”
12
“Enter the Second Number: ”
13
12 + 13 = 25
减法运算
复制代码代码如下:
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(($a - $b))
echo $a - $b = $x
注意:这里我们没有像上面的例子中使用“expr”来执行数学运算。
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Substraction.sh
[root@tecmint ~]# chmod 755 Substraction.sh
[root@tecmint ~]# ./Substraction.sh
“Enter the First Number: ”
13
“Enter the Second Number: ”
20
13 - 20 = -7
乘法运算
复制代码代码如下:
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a * $b = $(expr $a \* $b)"
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Multiplication.sh
[root@tecmint ~]# chmod 755 Multiplication.sh
[root@tecmint ~]# ./Multiplication.sh
“Enter the First Number: ”
11
“Enter the Second Number: ”
11
11 * 11 = 12
除法运算
复制代码代码如下:
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a / $b = $(expr $a / $b)"
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Division.sh
[root@tecmint ~]# chmod 755 Division.sh
[root@tecmint ~]# ./Division.sh
“Enter the First Number: ”
12
“Enter the Second Number: ”
3
12 / 3 = 4
数组
下面的这个脚本可以打印一组数字。
复制代码代码如下:
#!/bin/bash
echo “Enter The Number upto which you want to Print Table: ”
read n
i=1
while [ $i -ne 10 ]
do
i=$(expr $i + 1)
table=$(expr $i \* $n)
echo $table
done
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Table.sh
[root@tecmint ~]# chmod 755 Table.sh
[root@tecmint ~]# ./Table.sh
“Enter The Number upto which you want to Print Table: ”
29
58
87
116
145
174
203
232
261
290
你可以从这里下载这个例子的代码
判断奇偶数
复制代码代码如下:
#!/bin/bash
echo "Enter The Number"
read n
num=$(expr $n % 2)
if [ $num -eq 0 ]
then
echo "is a Even Number"
else
echo "is a Odd Number"
fi
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi EvenOdd.sh
[root@tecmint ~]# chmod 755 EvenOdd.sh
[root@tecmint ~]# ./EvenOdd.sh
Enter The Number
12
is a Even Number
1
2
3
4
5
[root@tecmint ~]# ./EvenOdd.sh
Enter The Number
11
is a Odd Number
Factorial数
复制代码代码如下:
#!/bin/bash
echo "Enter The Number"
read a
fact=1
while [ $a -ne 0 ]
do
fact=$(expr $fact \* $a)
a=$(expr $a - 1)
done
echo $fact
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Factorial.sh
[root@tecmint ~]# chmod 755 Factorial.sh
[root@tecmint ~]# ./Factorial.sh
Enter The Number
12
479001600
你可以从这里下载这个例子的代码
判断Armstrong数
Armstrong数:在三位的正整数中,例如abc,有一些可能满足(a^3)+(b^3)+(c^3)=abc,即各个位数的立方和正好是该数的本身。这些数即称为Armstrong数。
复制代码代码如下:
#!/bin/bash
echo "Enter A Number"
read n
arm=0
temp=$n
while [ $n -ne 0 ]
do
r=$(expr $n % 10)
arm=$(expr $arm + $r \* $r \* $r)
n=$(expr $n / 10)
done
echo $arm
if [ $arm -eq $temp ]
then
echo "Armstrong"
else
echo "Not Armstrong"
fi
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Armstrong.sh
[root@tecmint ~]# chmod 755 Armstrong.sh
[root@tecmint ~]# ./Armstrong.sh
Enter A Number
371
371
Armstrong
1
2
3
4
5
6
[root@tecmint ~]# ./Armstrong.sh
Enter A Number
123
36
Not Armstrong
判断质数
复制代码代码如下:
#!/bin/bash
echo “Enter Any Number”
read n
i=1
c=1
while [ $i -le $n ]
do
i=$(expr $i + 1)
r=$(expr $n % $i)
if [ $r -eq 0 ]
then
c=$(expr $c + 1)
fi
done
if [ $c -eq 2 ]
then
echo “Prime”
else
echo “Not Prime”
fi
输出结果:
复制代码代码如下:
[root@tecmint ~]# vi Prime.sh
[root@tecmint ~]# chmod 755 Prime.sh
[root@tecmint ~]# ./Prime.sh
“Enter Any Number”
12
“Not Prime”

阅读更多 >>>  linux安装盘怎么刻

linux的shell编程题

#!/bin/bash
i=0
if [ $# == 0 ];then
echo "usage:max num1,num2,num3 ..."
else
max=$1
min=$1
for number in $*
do
if [ $number -gt $max ]; then
max=$number
fi
# if [ $number -lt $min ]; then
# min=$number
# fi
done
echo "max number is: $max"
fi
[as@hunt1574 script ]$ sh max.sh 23 54 9 4 66 111 34
max number is: 111
再补充一下:
#!/bin/bash
for number in $*
do
echo $number >>/tmp/number.txt
done
cat /tmp/number.txt | sort -nr | head -1 && rm -f /tmp/number.txt >>/dev/null

linux命令 SHELL编程:从键盘输入一个数,若大于0,则输出该数;若小于或等于0,则输出0值。程序怎么写

#! /bin/bash
read num
#判断是否是数字
if [ `echo $num | grep - P '^-?\d+'$ |wc - l ` == 1 ] ; then
#判断是否大于0
if [ $num >0 ] ; then
echo $num
else
echo 0
fi
else
echo "不是数字"
fi
1、在shell编程中,输入一个数可以通过read命令来完成。
2、大于0输出该数,小于或等于0,输出0值,可以借助于if结构来判断。
3、示例如下
//read从键盘读取一个数保存到变量i中//并同时输出:“输入一个数”的字符提示read -p "输入一个数:" i;//如果用户输入的数i大于0就输出i的值if ((i > 0));then echo $i; //否则就直接用echo命令输出0else echo 0;fi //这里if语句结束4、执行效果如下:

Linux编程练习:编写shell程序,求各操作数平方的累加和。

各操作数保存为一个文件 到 /opt/1.txt
脚本如下:
#!/bin/bash
while read line
do
line2=0
let line2=$line*$line
echo "$line的平方为 $line2"
done < /opt/1.txt
lidd=0
while read lid
do
let lidd=$lidd+$lid
done < /opt/1.txt
echo "所有数的总和为 $lidd"
运行结果如下:

关于linux下的shell编程问题

1.
#!/usr/bin/bash
date
if [ "$1" -ne null ]
then
who | grep "$1" > /dev/null && echo "$1 has logged on"
mail $1
exit 0
fi
2. alias fire="./fire.sh"
#!/usr/bin/bash
echo $(date)
echo $(pwd)
who | wc -w
3. @City = qw/Sydney Osaka Beijing........./
for($i=0,i<10;i++)
{
echo "$city[i]"
}

急!两道linux shell awk编程题请教!

二楼是sb.
# 第一题楼下 overawe 写的很漂亮,没话讲!
# 我写第二题,可以 handle 2个文件中有不同的第一列的值:
# 先给你看我的 test data:(/tmp/f1 and /tmp/f2)
$ cat /tmp/f1
C|1
A|2
D|3
E|4
G|5
K|6
X|8
$ cat /tmp/f2
A|aa
C|bb
D|cc
E|dd
K|ee
G|ff
Z|zz
# shell script:
cat /tmp/x
#!/bin/sh
sed "s/|/|01|/g" /tmp/f1 > /tmp/f1.new
sed "s/|/|02|/g" /tmp/f2 > /tmp/f2.new
cat /tmp/f2.new /tmp/f1.new | sort | awk -F"|" '
BEGIN { rec1_fld1="" ; rec2_fld1="" }
{
if (rec1_fld1=="") { rec1_fld1=$1; rec1_fld2=$2; rec1_fld3=$3; next ;}
if (rec1_fld1!="") { # alredy read in record 1 data
rec2_fld1=$1 ; rec2_fld2=$2 ; rec2_fld3=$3;
if ( rec1_fld1 == rec2_fld1 ) { # 1st fld matched
printf("%s: %s=%s ..... (OK)\n",rec1_fld1,rec2_fld3,rec1_fld3);
rec1_fld1="";
} else { # something is wrong, print out whatever in rec1
printf("%s: %s ..... (NOT OK)\n",rec1_fld1,rec1_fld3);
rec1_fld1=rec2_fld1 ;
rec1_fld2=rec2_fld2 ;
rec1_fld3=rec2_fld3 ;
}
}
}
END {
# handle the last record if error
if(rec1_fld1!="") {
printf("%s: %s ..... (NOT OK)\n",rec1_fld1,rec1_fld3);
}
}'
# 执行结果:
$ /tmp/x
A: aa=2 ..... (OK)
C: bb=1 ..... (OK)
D: cc=3 ..... (OK)
E: dd=4 ..... (OK)
G: ff=5 ..... (OK)
K: ee=6 ..... (OK)
X: 8 ..... (NOT OK)
Z: zz ..... (NOT OK)
谢谢!
1 第一题 data.tmp是文件名
awk -F'&&' '{a[$1]+=$2}END{for(i in a)print i,a[i]}' data.tmp
2 先去吃早点 等等在作
awk -F'|' 'NR==FNR{a[$1]=$2}NR>FNR{if($1 in a)print $0,a[$1]}' f1 f2
这样就可以了, 但是只限于这个特例, 如果第一列会有重复出现, 就有bug了。 或者在第二个文件的第一列中的数值在第一个文件中没有出现过,也会有bug

阅读更多 >>>  linux命令lnsort

linux shell 编程

程序内容很简单,如下:
#!/bin/sh
filename=`date +%y``date +%m``date +%d`
tar czvf /root/bak/$filename-etc.tgz /etc
最后使用crontab建立一个计划作业可,使得该程序可以在每月的第一天下午六点执行。作业内容如下:
0 18 1 * * /usr/bin/fileback
为什么没有人用
date +%y%m%d
(1)编写shell程序fileback:
#!/bin/sh
DIRNAME=`ls /root | grep bak`
if [ -z "$DIRNAME" ] ; then
mkdir /root/bak
cd /root/bak
fi
YY=`date +%y`
MM=`date +%m`
DD=`date +%d`
BACKETC=$YY$MM$DD_etc.tar.gz
tar zcvf $BACKETC /etc
echo "fileback finished!"
(2)编写任务定时器:
echo "0 0 1 * * /bin/sh /usr/bin/fileback" >; /root/etcbakcron
crontab /root/etcbakcron
或使用crontab -e 命令添加定时任务:
0 1 * * * /bin/sh /usr/bin/fileback

网站数据信息

"linux shell编程题,Linux编程练习:编写shell程序,求各操作数平方的累加和。"浏览人数已经达到19次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:linux shell编程题,Linux编程练习:编写shell程序,求各操作数平方的累加和。的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!