2016-05-06 3 views
2

この を実行したときに問題が発生した場合、コンソールはこれを何度も何度も繰り返します。bashシェルスクリプト、32ビット小数点からバイナリコンバータへの問題

これは、bashが算術演算によって変数を再定義する方法と関係がありますか?

#!/bin/bash 

echo "This script converts a user's number into an IP address." 
echo -n "Input your number: " 
read user 

if [ $user -lt 4294967296 ] 
then 
    exp=$((32)) 
    num=$((0)) 
    ipb="" 
    while [ $exp -gt 0 ] 
    do 
    bit=expr 2 ** $exp 
    exp=expr $exp - 1 
    if [ $bit+$num -le $user ] 
    then 
     $ipb="${ipb}1" 
     num=expr $num + $bit 
    else 
     $ipb="${ipb}0" 
fi 
done 
echo $ipb 
echo "done" 
fi 

これと同じですが、コメントを付けて説明してください。

#!/bin/bash 

echo "This script converts a user's number into an IP address." 
echo -n "Input your number: " 
read user 

#check if number is larger than 32bits 
if [ $user < 4294967296 ] 
then 
#var exp is exponent that will be used to redefine var bit each loop cycle 
#var num is var used to rebuild the user number with corresponding bits added to - 
#var ipb is IP binary (not yet converted to 4 octet integers) 
    exp=$((32)) 
    num=$((0)) 
    ipb="" 
#while the exponent is greater than 0 (exponent is 1 less as per binary order) 
    while [ $exp > 0 ] 
    do 
#(Re)define bit var for line 23 
    bit=expr 2**$exp 
#go to next lowest exponent 
    exp=expr $exp - 1 
#If the current bit is added to our num var,will it be 
#less than or equal to the user number? 
    if [ $bit + $num -le $user ] 
    then 
#If so, redefine the ipb string var with a 1 on the end 
#and redefine the num integer var added with the current 
#iteration of the bit integer var's value 
     $ipb="${ipb}1" 
     num=expr $num + $bit 
    else 
#if not, redefine the ipb string var with a 0 on the end 
     $ipb="${ipb}0" 
fi 
done 
#output the IP binary 
echo $ipb 
echo "done" 
fi 

EDIT: shellcheckからいくつかのグーグルとの助けた後、私はそれが動作するようになりました。私のLinuxミントのバージョンで何らかの理由でを指数操作として正しく取ったのはletコマンドだけでした。好奇心をそそる人のためのコードは次のとおりです。

echo "This script converts a user's number into the 32 bit equivalent." 
echo -n "Input a number below 4294967296: " 
read user 
echo "" 
if [ $user -lt 4294967296 ] 
then 
    exp=$((31)) 
    num=$((0)) 
    ipb="" 
    while [ $exp -ge 0 ] 
    do 
    let bit=2**$exp 
    let exp-=1 
    if (($bit + $num <= $user)) 
     then 
     ipb="${ipb}1" 
     num=$(($num + $bit)) 
     else 
     ipb="${ipb}0" 
    fi 
    done 
fi 
echo $ipb 

ターミナルでスクリプトを実行するときに代わりにshまたは./bashを使用してください。

+2

http://www.shellcheck.net/からの提案でスクリプトを修正し、問題が続くかどうか確認してください。 – Sundeep

+0

if [$ bit + $ num -le $ user] '数値比較は '-le/-ge'で行います(ただし、if条件自体はまだ間違っていますが...)while [$ exp> 0]'を使用していますか? – anishsane

答えて

2

スクリプトにはいくつか問題があります。これは何のエラーも投げられません。

#! /bin/bash 

echo "This script converts a user's number into an IP address." 
echo -n "Input your number: " 
read user 

if [ $user -lt 4294967296 ] 
then 
    exp=$((32)) 
    num=$((0)) 
    ipb="" 
    while [ $exp -gt 0 ] 
    do 
    bit=$((2 ** $exp)) 
    exp=$(expr $exp - 1) 
    if ((bit+num < user)) 
    then 
     ipb="${ipb}1" 
     num=$(expr $num + $bit) 
    else 
     ipb="${ipb}0" 
fi 
done 
echo $ipb 
echo "done" 
fi 

大きな問題の1つは、exprは電力を計算できないということです。また、exprの結果を変数に代入しようとしているときに、コマンド置換を使用しないでください。
もう一つの大きな問題は、ifブランチでしようとすると、[ ]括弧の中で数学を行うことができないということです。代わりに(())を使用してください。
実際に割り当てたい変数を拡大するなど、他の問題がいくつかありました(例:$ipb="${ipb}1")。

関連する問題