无法从BASH中的WHILE中获取变量的值 [英] Can't get the value of variable out of WHILE in BASH

查看:81
本文介绍了无法从BASH中的WHILE中获取变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码:

od -An -t x1 <"$file" |       
while read line;do               
  for char in $line;do  
    if [ $needspace -eq 0 ]
    then 
      export  hexs=$hexs`printf "%.2s" "$char"` 
      needspace=1
    else
      export hexs=$hexs`printf "%.2s " "$char"`    
      needspace=0  
    fi
    if [ `printf "%d" "0x$char" ` -lt 32 ] || [ `printf "%d" "0x$char"` -gt 126 ]
    then
      text=$text.
    else
      text=$text`echo -e "\x$char"`  
    fi
    charsnow=$(($charsnow+1))
  done  
  sup=1   
  echo $sup
done
echo $sup

问题是:第一个echo $sup写入1 但是第二个echo $sup不写任何内容.为什么我不能从"while"中获得$ sup?

The problem is: first echo $sup writes 1 But second echo $sup writes nothing. Why can't I get $sup out of "while"?

推荐答案

您有一个子shell,因此不会发生副作用. http://mywiki.wooledge.org/BashFAQ/024

You have a subshell, so side-effects can't occur. http://mywiki.wooledge.org/BashFAQ/024

这篇关于无法从BASH中的WHILE中获取变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆