之前看到你回答网友的shell程序中运用发现问题,求解答
出现这个问题怎么解?附上你给出的代码:#!/bin/bashtrap""2315MYDATE=`date"+%D%T"`THIS_HOST=`hostname`USER=...
出现这个问题怎么解?
附上你给出的代码:
#!/bin/bash
trap "" 2 3 15
MYDATE=`date "+%D %T"`
THIS_HOST=`hostname`
USER=`whoami`
#function:打开即时响应模式,读取用户点击的键值
read_char()
{
SAVESTTY=`stty -g`
stty raw
stty -echo
dd if=/dev/tty bs=1 count=1 2>/dev/null
stty -raw
stty echo
stty $SAVESTTY
}
#function:颜色模式
colour()
{
case $1 in
black_green)
echo '^[[40;32m'
;;
black_yellow)
echo '^[[40;33m'
;;
black_white)
echo '^[[40;37m'
;;
black_cyan)
echo '^[[40;36m'
;;
red_yellow)
echo '^[[41;33m'
;;
white_black)
echo '^[[47;30m'
;;
esac
}
#退出动作
my_exit()
{
colour black_white
exit 0
}
while :
do
tput clear
colour black_green
cat <<MAYDAY
--------------------------------------------------------------------------------
USER:$USER HOST:$THIS_HOST DATE:$MYDATE
--------------------------------------------------------------------------------
1:计算1到n的奇数之和
2:计算1到n的阶乘
3:计算1到n的所有质数
Q:退出程序
--------------------------------------------------------------------------------
MAYDAY
colour black_cyan
echo "Your Choice[1,2,3,Q]>"
read CHOICE
CHOICE=`read_char`
case $CHOICE in
1) uy=`expr $n % 2`sum=1if [ "$uy" -eq "0" ]thenn=`expr $n - 1`fifor((i=1;i<=$n;i++))doi=`expr $i + 2`sum=`expr $sum + $i`done
;;
2) for((i=0;i<$num;i++))doi=`expr $i + 1`sum=`expr $sum \* $i`done
;;
3) for i in `seq 1 $n`; do factor $i | awk '{ if (NF == 2) print $2 }' ; done
;;
Q|q)my_exit;;
*)echo "unknow choice!!";;
esac
echo "Hit the ENTER key to return continue!"
read XXAA
done 展开
附上你给出的代码:
#!/bin/bash
trap "" 2 3 15
MYDATE=`date "+%D %T"`
THIS_HOST=`hostname`
USER=`whoami`
#function:打开即时响应模式,读取用户点击的键值
read_char()
{
SAVESTTY=`stty -g`
stty raw
stty -echo
dd if=/dev/tty bs=1 count=1 2>/dev/null
stty -raw
stty echo
stty $SAVESTTY
}
#function:颜色模式
colour()
{
case $1 in
black_green)
echo '^[[40;32m'
;;
black_yellow)
echo '^[[40;33m'
;;
black_white)
echo '^[[40;37m'
;;
black_cyan)
echo '^[[40;36m'
;;
red_yellow)
echo '^[[41;33m'
;;
white_black)
echo '^[[47;30m'
;;
esac
}
#退出动作
my_exit()
{
colour black_white
exit 0
}
while :
do
tput clear
colour black_green
cat <<MAYDAY
--------------------------------------------------------------------------------
USER:$USER HOST:$THIS_HOST DATE:$MYDATE
--------------------------------------------------------------------------------
1:计算1到n的奇数之和
2:计算1到n的阶乘
3:计算1到n的所有质数
Q:退出程序
--------------------------------------------------------------------------------
MAYDAY
colour black_cyan
echo "Your Choice[1,2,3,Q]>"
read CHOICE
CHOICE=`read_char`
case $CHOICE in
1) uy=`expr $n % 2`sum=1if [ "$uy" -eq "0" ]thenn=`expr $n - 1`fifor((i=1;i<=$n;i++))doi=`expr $i + 2`sum=`expr $sum + $i`done
;;
2) for((i=0;i<$num;i++))doi=`expr $i + 1`sum=`expr $sum \* $i`done
;;
3) for i in `seq 1 $n`; do factor $i | awk '{ if (NF == 2) print $2 }' ; done
;;
Q|q)my_exit;;
*)echo "unknow choice!!";;
esac
echo "Hit the ENTER key to return continue!"
read XXAA
done 展开
展开全部
之前写的代码BUG太多,而且我是做两段回答的,中间少了很多代码,运行了的确报了很多错误。
下面是我修改过的代码,你可以运行试试:
#!/bin/bash
trap "" 2 3 15
MYDATE=`date "+%D %T"`
THIS_HOST=`hostname`
USER=`whoami`
#function:打开即时响应模式,读取用户点击的键值
read_char()
{
SAVESTTY=`stty -g`
stty raw
stty -echo
dd if=/dev/tty bs=1 count=1 2>/dev/null
stty -raw
stty echo
stty $SAVESTTY
}
#退出动作
my_exit()
{
trap 2 3 15
exit 0
}
while :
do
clear
cat <<MAYDAY
--------------------------------------------------------------------------------
USER:$USER HOST:$THIS_HOST DATE:$MYDATE
--------------------------------------------------------------------------------
1:计算1到n的奇数之和
2:计算1到n的阶乘
3:计算1到n的所有质数
Q:退出程序
--------------------------------------------------------------------------------
MAYDAY
echo "Your Choice[1,2,3,Q]>"
#read CHOICE
CHOICE=`read_char`
case $CHOICE in
1)
echo "Input a num(>1):"
read n
uy=`expr $n % 2`
if [ "$uy" -eq "0" ]
then
p=`expr $n - 1`
else
p=$n
fi
m=3
sum=1
while [ "$m" -le "$p" ]
do
sum=`expr $sum + $m`
m=`expr $m + 2`
done
echo "1到$n的奇数之和为$sum"
;;
2)
echo "Input a num(>1):"
read num
sum=1
for((i=1;i<=$num;i++))
do
sum=`expr $sum \* $i`
done
echo "$num的阶乘为$sum"
;;
3)
echo "Input a num(>1):"
read n
echo "--------------------------------------------------"
for i in `seq 1 $n`; do factor $i | awk '{ if (NF == 2) print $2 }' ; done
;;
Q|q)my_exit;;
*)echo "unknow choice!!";;
esac
echo "Hit the ENTER key to return continue!"
read XXAA
done
对比之前的代码,有不懂的可以继续问我。
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询