创建一个shell脚本,该脚本接收10个数,并显示最大数,最小数。求高手的linuxs,在网上查的那些都执行不了! 编写一个shell脚本,读入10个参数,输出最大值和最小值。...

作者&投稿:费独 (若有异议请与网页底部的电邮联系)
一般shell只接受$0~$9十个位置参数,其中$0表示脚本名称本身,也就是说只有$1~$9共9个参数。超过9个参数的话,比如你这里要10个数,需要用shift移位来获取后面的更多参数。

#!/bin/sh
if [ $# -ne 10 ]; then
echo -e "Wrong parameters!\nYou MUST input 10 digits."
exit 1
fi
min=$1
max=$1
i=1
while [ $i -lt 10 ]
do
shift 1
let i+=1
[ $1 -lt $min ] && min=$1
[ $1 -gt $max ] && max=$1
done
echo "Min=$min"
echo "Max=$max"
exit 0

#!/bin/bash

function usage()
{
echo "$0 N1 N2 ... N10" >&2
exit 1
}

[[ $# -eq 10 ]] || usage

max=$1
min=$1
shift
for i in $*
do
[[ $min -gt $i ]] && min=$i
[[ $i -gt $max ]] && max=$i
done

echo -e "Max=$max\tMin=$min"

csh脚本:
chengqi@chengqi-PC /cygdrive/d/test
$ cat ceshi.sh
#!/bin/csh
set first_num=$1
echo $1\\n$2\\n$3\\n$4\\n$5\\n$6\\n$7\\n$8\\n$9\\n$10 | awk 'BEGIN{max="'$first_num'";min="'first_num'"}{if($1>max){max=$1};if($1<min){min=$1}}END{pri
nt "the max number is "max;print "the min number is "min}'

执行结果:
chengqi@chengqi-PC /cygdrive/d/test
$ ./ceshi.sh 11 31 23 34 64 1 45 20 32 14
the max number is 64
the min number is 1

创建一个shell脚本,它从用户那里接收十个数,并显示已输入的最大数~

if [ $# -ne 3 ];then
echo error
else
echo "$1 $2 $3"
if

#!/usr/bin/ksh
cal_id=1
num_count=10
max_num=-99999
min_num=-99999
while [ $cal_id -le $num_count ]
do
read line
if [ $cal_id -eq 1 ] ; then
min_num=$line
fi
if [ $line -ge $max_num ] ; then
max_num=$line
fi
if [ $line -le $min_num ] ; then
min_num=$line
fi
cal_id=`expr $cal_id + 1`
done
clear
echo $max_num
echo $min_num

基本上没有问题了

如何编写一个shell脚本
答:当编辑好脚本时,如果要执行该脚本,还必须使其可执行。 要使脚本可执行: chmod +x filename 然后,您可以通过输入: ./filename 来执行您的脚本。 注释 在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。如果您使用了注释,那么即使相当长的时间内没有使用该脚本,...

创建一个shell脚本,接受20个数,显示最大数和最小数?如何编写?
答:2 read -p "enter:" p 3 echo "$p" >> ./1.tmp 4 until [ $p -eq 0 ]5 do 6 read -p "enter:" p 7 echo "$p" >> ./1.tmp 8 c=`cat ./1.tmp |wc -l`9 if [ $c -eq 20 ] ; then 10 sort -n ./1.tmp |tail -1 12 sort -n ./1.tmp |head -1 1...

创建一个shell 脚本,它从用户那里接收10个数,求和,并显示输出结果。
答:do read -p '请输入第'$i'个数:' num$i;let sum=sum+num$i;done echo "总和为:$sum"

编写一个显示以下模式的shell脚本,接收一个数字,使用while循环:(1打印1...
答:[root@localhost /]# cat test.sh !/bin/sh i=1 while [ $i -lt 100 ]do b=1 while [ $b -le $i ]do echo "$i" | tr -d "\n"b=`expr $b + 1`done echo ""i=`expr $i + 1`done [root@localhost /]# ./test.sh 1 22 333 4444 55555 666666 7777777 88888888 ...

创建一个shell脚本,它从用户那里接受十个数,并显示已输入的最大的数...
答:!/bin/sh if [ $# -ne 10 ]; then echo -e "Wrong parameters!\nYou MUST input 10 digits."exit 1 fi min=$1 max=$1 i=1 while [ $i -lt 10 ]do shift 1 let i+=1 [ $1 -lt $min ] && min=$1 [ $1 -gt $max ] && max=$1 done echo "Min=$min"echo "Max...

linux中设计一个shell脚本程序可以接受命令行输入的参数执行相应的菜单...
答:1、命令行参数 向shell脚本传递数据的最基本方式是使用命令行参数。1)读取参数 读取输入的参数的变量为位置参数,位置参数通过标准数字表示,其中$0为程序名称,$1为第一个参数,$2为第二个参数,依次类推,直到$9为第九个参数。shell脚本自动将命令行参数赋值给各个位置变量。同时输入多个参数可以是...

如何编写一个shell脚本
答:新建一个文件shell脚本一般用×.sh作为后缀当然勇气他的也可以。打开终端输入touch first.sh 新建一个名为first的shell脚本。编写一个简单的linuxshell脚本 使用vim 编辑first.sh也可以用其他的文本编辑器,推荐使用vim 使用命令 vim first.sh打开,输入i进入编辑模式。编写一个简单的linuxshell脚本 我们...

如何编写一个shell脚本
答:1、shell 快速入门:!/bin/sh cd ~mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch test_$i.txt done 创建一个文件夹, 并在里面新建10个文件 2、curl 使用curl请求网络, 并获取返回值 curl基本用法:curl www.sina.com 直接返回网页内容 curl -o [文件名] www.sina...

如何编写一个简单的shell脚本
答:1、首先打开编程界面,查看现在dog目录下的文件的权限信息。2、现在b.txt的用户权限是RW,组权限是RW,其他权限是R。现在我给用户权限加上X运行权限。3、同样的方法给组、其他人加上运行权限使用chmod u/g/o +x b.txt 。4、如果要取消对应的权限,使用“-”号即可。5、除了使用RWX字符后还可以...

编写SHELL脚本,匹配一个网页中的所有链接,并输出到文件,格式为类似htt...
答:输出到文件savefile grep 'href' file.html|sed 's/^.*href="//'|sed 's/\/".*//'>savefile 例如一个file.html网页的source file 文件片段 --- ...My Opera Opera news blog ...--- 输出:http://my.opera.com/community http://my.opera.com/chooseopera...