我有一个输出yileds 2列的变量: echo" $ SIZE_TO_SOCKET"
A 256
B 256
我的全球基本上是比较2个数字并打印按摩
类似:如果匹配:成功:A(256)与B(256)相同
或错误:如果数字不是euqal,则A(256)不是B(256)的唯一
我的代码是:
while IFS= read -r i;do
SIZE=$(echo "$i"|awk '{print $2}')
SOCKET=$(echo "$i"|awk '{print $1}')
if [[ "$SOCKET" = "A" ]] ; then
SOCKET_A="$SIZE"
elif [[ "$SOCKET" = "B" ]] ; then
SOCKET_B="$SIZE"
fi
if [[ $SOCKET_A -ne $SOCKET_B]];then
echo "error: SOCKET_A is not equal to SOCKET_B"
elif [[ $SOCKET_A -eq $SOCKET_B]];then
cho "success: SOCKET_A is equal to SOCKET_B"
fi
done <<< "$SIZE_TO_SOCKET"
代码不会产生任何输出,无论如何都有更短更优雅的方式
答案 0 :(得分:0)
input="A 256\nB 256"
arr=($(echo -e "$input"))
if [[ "${arr[1]}" -eq "${arr[3]}" ]]
then
echo "success: ${arr[0]}(${arr[1]}) is equal to ${arr[2]}(${arr[3]})"
else
echo "error: ${arr[0]}(${arr[1]}) is not equal to ${arr[2]}(${arr[3]})"
fi
用输入替换input
变量,此脚本输出
success: A(256) is equal to B(256)
两个相同的数字或
error: A(257) is not equal to B(256)
两个不同的数字。
答案 1 :(得分:0)
这是我最终解决的非常简单的解决方案:
line1=$(echo "$SIZE_TO_SOCKET" | awk 'NR == 1')
line2=$(echo "$SIZE_TO_SOCKET" | awk 'NR == 2')
key1=$(echo "$line1" | cut -d ' ' -f 1)
value1=$(echo "$line1" | cut -d ' ' -f 2)
key2=$(echo "$line2" | cut -d ' ' -f 1)
value2=$(echo "$line2" | cut -d ' ' -f 2)
if [[ $value1 -eq $value2 ]]; then
echo "Success: $key1($value1) is equal to $key2($value2)"
else
echo "Error: $key1($value1) is not equal to $key2($value2)"
fi
答案 2 :(得分:0)
您的代码有什么问题?:
您的代码“已更正”将是:
print
您需要在不同的地方使用空格,例如SIZE_TO_SOCKET=$(printf '41203235360a42203235360a' | xxd -p -r)
while IFS= read -r i;do
SIZE=$(echo "$i"|awk '{print $2}')
SOCKET=$(echo "$i"|awk '{print $1}')
if [[ "$SOCKET" = "A" ]] ; then
SOCKET_A="$SIZE"
elif [[ "$SOCKET" = "B" ]] ; then
SOCKET_B="$SIZE"
fi
if [[ $SOCKET_A -ne $SOCKET_B ]] ; then
echo "error: SOCKET_A is not equal to SOCKET_B"
elif [[ $SOCKET_A -eq $SOCKET_B ]] ; then
echo "success: SOCKET_A is equal to SOCKET_B"
fi
done <<< "$SIZE_TO_SOCKET"
{原始代码为$SOCKET_B ]] ; then
)
“已更正”代码的输出为:
$SOCKET_B]];then
如果您使用error: SOCKET_A is not equal to SOCKET_B
success: SOCKET_A is equal to SOCKET_B
,则会获得:
bash -x YOUR_SCRIPT
仔细阅读;你的逻辑错误。
这可能是您的代码的更正版本:
++ printf 41203235360a42203235360a
++ xxd -p -r
+ SIZE_TO_SOCKET='A 256
B 256'
+ IFS=
+ read -r i
++ echo 'A 256'
++ awk '{print $2}'
+ SIZE=256
++ echo 'A 256'
++ awk '{print $1}'
+ SOCKET=A
+ [[ A = \A ]]
+ SOCKET_A=256
+ [[ 256 -ne '' ]]
+ echo 'error: SOCKET_A is not equal to SOCKET_B'
error: SOCKET_A is not equal to SOCKET_B
+ IFS=
+ read -r i
++ echo 'B 256'
++ awk '{print $2}'
+ SIZE=256
++ echo 'B 256'
++ awk '{print $1}'
+ SOCKET=B
+ [[ B = \A ]]
+ [[ B = \B ]]
+ SOCKET_B=256
+ [[ 256 -ne 256 ]]
+ [[ 256 -eq 256 ]]
+ echo 'success: SOCKET_A is equal to SOCKET_B'
success: SOCKET_A is equal to SOCKET_B
+ IFS=
+ read -r i
输出是:
SIZE_TO_SOCKET=$(printf '41203235360a42203235360a' | xxd -p -r)
while IFS= read -r i;do
SIZE=$(echo "$i"|awk '{print $2}')
SOCKET=$(echo "$i"|awk '{print $1}')
if [[ "$SOCKET" = "A" ]] ; then
SOCKET_A="$SIZE"
elif [[ "$SOCKET" = "B" ]] ; then
SOCKET_B="$SIZE"
fi
done <<< "$SIZE_TO_SOCKET"
if [[ $SOCKET_A -ne $SOCKET_B ]] ; then
echo "error: SOCKET_A is not equal to SOCKET_B"
elif [[ $SOCKET_A -eq $SOCKET_B ]] ; then
echo "success: SOCKET_A is equal to SOCKET_B"
fi
如果您使用success: SOCKET_A is equal to SOCKET_B
,则会获得:
bash -x YOUR_CORRECTED_SCRIPT