免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7543 | 回复: 13

shell中变量自加导致崩溃,请教大家 [复制链接]

论坛徽章:
0
发表于 2011-03-02 23:37 |显示全部楼层
本帖最后由 虫虫2003 于 2011-03-08 08:56 编辑

我这边有个shell脚本,里面会反复调用函数,像这样:
  1. set +e

  2. ECHO "unpacking source/lib/ncurses-5.5"
  3. mkdir -pv source/lib
  4. run_command_progress_float "tar -xvzf pub/tarball/ncurses-5.5.tgz -C source/lib/" 0         "tar -tzf pub/tarball/ncurses-5.5.tgz | wc -l"

  5. ECHO "unpacking source/lib/expat-1.95.8"
  6. mkdir -pv source/lib
  7. run_command_progress_float "tar -xvzf pub/tarball/expat-1.95.8.tgz -C source/lib/" 0         "tar -tzf pub/tarball/expat-1.95.8.tgz | wc -l"

  8. .......
复制代码
函数run_command_progress_float是这样的:
  1. # $1: command
  2. # $2: total
  3. # $3: command to calc totals
  4. run_command_progress_float()
  5. {
  6.         local readonly RCP_RANGE=50
  7.         local rcp_lines=0
  8.         local rcp_nextpos=1
  9.         local rcp_total=0
  10.         local progress_bar=
  11.         local rcp_prog=0
  12.         local rcp_tmp=0
  13.         local prog_bar_base=
  14.         local rcp_percent=0

  15.         ECHO "run_command_progress_float: '$1'"

  16.         if [ -n "$3" ] ;
  17.         then
  18.                 echo -n "Initializing progress bar ..."
  19.                 rcp_total=`eval $3`;
  20.                 echo -n "\r"
  21.                 [ -z "$rcp_total" ] && rcp_total=1
  22.         else
  23.                 [ -n "$2" ] && rcp_total=$2
  24.         fi

  25.         [ -z "$rcp_total" ] && rcp_total=1
  26.         [ $rcp_total -le 0 ] && rcp_total=1

  27.         prog_bar_base="[    ]"

  28.         while [ $rcp_tmp -lt $RCP_RANGE ]
  29.         do
  30.                 prog_bar_base="$prog_bar_base-"

  31.                 ((rcp_tmp++))
  32. #                ((rcp_tmp+=1))
  33. #                rcp_tmp=$[rcp_tmp+1]

  34. #                echo "rcp_tmp=$rcp_tmp"
  35.         done
  36.         prog_bar_base="${prog_bar_base}|"
  37.         printf "\r$prog_bar_base\r"

  38.         set +e
  39.         eval $1 | while read line
  40.         do
  41.                 ((rcp_lines++))

  42.                 if [ $rcp_lines -ge $rcp_nextpos ]
  43.                 then
  44.                         rcp_percent=`expr \( $rcp_lines \* 101 - 1 \) / $rcp_total `
  45.                         rcp_prog=`expr \( $rcp_lines \* \( $RCP_RANGE + 1 \) - 1 \) / $rcp_total `
  46.                         [ $rcp_prog -gt $RCP_RANGE ] && rcp_prog=$RCP_RANGE
  47.                         rcp_nextpos=`expr \( \( $rcp_percent + 1 \) \* $rcp_total \) / 100`
  48.                         [ $rcp_nextpos -gt $rcp_total ] && rcp_nextpos=$rcp_total

  49.                         rcp_tmp=0
  50.                         progress_bar=""
  51.                         while [ $rcp_tmp -lt $rcp_prog ]
  52.                         do
  53.                                 progress_bar="$progress_bar#"
  54.                                 ((rcp_tmp++))
  55.                         done
  56.                         printf "\r$prog_bar_base\r[%3d%%]$progress_bar\r" $rcp_percent
  57.                 fi
  58.         done
  59.         set -e

  60.         echo ""
  61. }
复制代码
现在问题就出在while [ $rcp_tmp -lt $RCP_RANGE ]中的((rcp_tmp++))这句上,run_command_progress_float只能成功执行一次,第二次执行时跑到((rcp_tmp++))就会崩溃,退出脚本,而我把这句改为((rcp_tmp+=1))或者rcp_tmp=$[rcp_tmp+1]就没有问题。
我是在ubuntu下运行的,好象是从10以后的版本出现这个问题的,以前用9.10是不会出错的。
不知到是怎么回事,请大家帮忙看看,应该怎么写。
谢谢!

论坛徽章:
0
发表于 2011-03-02 23:40 |显示全部楼层
另外还想问下,的set +e、set -e、printf引号中的\r分别是意思?

论坛徽章:
0
发表于 2011-03-03 09:30 |显示全部楼层
请大家帮忙看看

论坛徽章:
0
发表于 2011-03-03 09:31 |显示全部楼层
请大家帮忙看看。。。

论坛徽章:
0
发表于 2011-03-03 09:40 |显示全部楼层
请大家帮忙看看。。。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-03-03 09:41 |显示全部楼层
我这边有个shell脚本,里面会反复调用函数,像这样:函数run_command_progress_float是这样的:现在问题就出 ...
虫虫2003 发表于 2011-03-02 23:37
bash当中似乎没有这种自加的语法。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-03-03 09:45 |显示全部楼层
另外还想问下,的set +e、set -e、printf引号中的\r分别是意思?
虫虫2003 发表于 2011-03-02 23:40
这个你看bash的帮助页就知道了。
       set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]
              Without options, the name and value of each shell  variable  are
              displayed in a format that can be reused as input for setting or
              resetting the currently-set variables.  Read-only variables can‐
              not  be  reset.  In posix mode, only shell variables are listed.
              The output is sorted according  to  the  current  locale.   When
              options  are specified, they set or unset shell attributes.  Any
              arguments remaining after option processing are treated as  val‐
              ues for the positional parameters and are assigned, in order, to
              $1, $2, ...  $n.  Options,  if  specified,  have  the  following
              meanings:
              -a      Automatically  mark  variables  and  functions which are
                      modified or created for export  to  the  environment  of
                      subsequent commands.
              -b      Report  the status of terminated background jobs immedi‐
                      ately, rather than before the next primary prompt.  This
                      is effective only when job control is enabled.
              -e      Exit  immediately  if a pipeline (which may consist of a
                      single simple command),  a subshell command enclosed  in
                      parentheses,  or one of the commands executed as part of
                      a command list enclosed by  braces  (see  SHELL  GRAMMAR
                      above) exits with a non-zero status.  The shell does not
                      exit if the command that fails is part  of  the  command

论坛徽章:
0
发表于 2011-03-03 09:50 |显示全部楼层
bash当中似乎没有这种自加的语法。
L_kernel 发表于 2011-03-03 09:41


那正确的应该怎么写?

论坛徽章:
0
发表于 2011-03-03 13:47 |显示全部楼层
本帖最后由 南极雨 于 2011-03-03 13:50 编辑
这个你看bash的帮助页就知道了。
L_kernel 发表于 2011-03-03 09:45


我的shell为什么没有 sed + 这类的帮助啊 ?
还有,这段的意思是不是这个:每个shell变量的名称和值会格式化并打印出来,并且能作为输入(设置或者重置当前的变量)重新利用,只读的变量不能设置。在posix系统环境中,只输出shell变量,并根据当前的语言环境排序。一些特殊选项来设置或者重置shell环境变量。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-03-04 20:11 |显示全部楼层
那正确的应该怎么写?
虫虫2003 发表于 2011-03-03 09:50
改为通常意义上的rcp_tmp+1就可以了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP