将linux变量设置为命令行输出

时间:2013-06-26 20:03:25

标签: linux hex dd

我正在使用QNX修改某些文件,然后在这些文件中输出一些信息。该过程类似于以下内容: 在目录中查找某些文件 如果名称小于值   perfomr dd命令   输出文件 - 信息 - staus

find $DIR -type n \( ! -iname "*sub*" \) -exec basename {} |
while read fname
do
    if [ $(printf '%d\n' 0x$fname) -lt 31 ] #if file is < 31
    then 
        dd bs=1 skip=67 count=1 if=/path/$fname/random of=/tmp/$fname
        echo -n " "$fname"      "  #output file name
        TEMP=$(</tmp/$fname)       #temporary file is in 
        hd -A x /tmp/$fname | gawk '{printf $2}'

        if [ $fname == 06 ]; then
            COM="Testing on 06" # this is temporary
        elif [ $fname == 08 ]; then
            case "?? hex value of $2 ?? in
                00) COM="DOWN" ;; #again, not sure what works
                0x01) COM="UP" ;;
                02) COM="Pass Through Mode" ;;
                *) COM="ERROR" ;;
            esac
        else 
            if [[ $STATE = 0x00 ]]; then #doesn't work either
                COM="DOWN"
            elif [ "(hd -A x /tmp/$fname | gawk '{printf $2}')" == 0x01 ]; then
                COM="UP"
            else
                COM="ERROR $STATE "
            fi
        fi

        echo "        " $COM



    fi
done

我需要进行比较才能在dd输出的十六进制值和十六进制数字之间工作,甚至整数比较。

2 个答案:

答案 0 :(得分:0)

您可以将结果存储到变量中,通过将其括在引号中来从特定命令发送到stdout:

linecount = `cat text.txt|wc -l`

答案 1 :(得分:0)

使用反引号将unix命令的输出捕获到unix变量中,即

   t=`/bin/date`
   echo the time is $t

你可能会做这样的事情

   firstByte = `dd bs=1 skip=67 count=1 if=/path/$fname/random of=- | hd -A x`

虽然你需要一些额外的hd参数来抑制地址信息(我认为这是你试图用gawk步骤做的事情。我不知道hd够好 - - 阅读手册页。