Skip to content

Instantly share code, notes, and snippets.

@toshi0383
Created April 16, 2014 08:57
Show Gist options
  • Save toshi0383/10836442 to your computer and use it in GitHub Desktop.
Save toshi0383/10836442 to your computer and use it in GitHub Desktop.
if文内で">"を使うと恐ろしいことが ref: http://qiita.com/toshi0383/items/b721b48fa780fe57e7c1
# 間違い!
#!/bin/bash
n1=${1:?}
n2=${2:?}
n3=${3:?}
if [ $n1 > $n2 ];then
tmp=$n1
n1=$n2
n2=$tmp
fi
if [ $n2 > $n3 ];then
tmp=$n2
n2=$n3
n3=$tmp
fi
if [ $n1 > $n2 ];then
tmp=$n1
n1=$n2
n2=$tmp
fi
echo $n1 $n2 $n3
# 正しい
#!/bin/bash
n1=${1:?}
n2=${2:?}
n3=${3:?}
if [ $n1 -gt $n2 ];then
tmp=$n1
n1=$n2
n2=$tmp
fi
if [ $n2 -gt $n3 ];then
tmp=$n2
n2=$n3
n3=$tmp
fi
if [ $n1 -gt $n2 ];then
tmp=$n1
n1=$n2
n2=$tmp
fi
echo $n1 $n2 $n3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment