Command (Random number between 0 to 120,000,000):
$ MIN=0; MAX=120000000; while rnd=$(cat /dev/urandom | LC_CTYPE=C tr -dc 0-9 | fold -w${#MAX} | head -1 | sed 's/^0*//;'); [ -z $rnd ] && rnd=0; (( $rnd < $MIN || $rnd > $MAX)); do :; done; echo $rnd;
Result:
74774521
Tuesday, June 6, 2017
macOS Sierra: Bash (Unix shell): Command to Test for Int64 or Long Integer Support
Command:
$ ((x=(2**63)-1)); echo $x; ((x++)); echo $x;
Result:
9223372036854775807
-9223372036854775808
Interesting note:
9,223,372,036,854,775,807
is
nine quintillion two hundred twenty three quadrillion three hundred seventy two trillion thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred seven
is
九百二十二京三千三百七十二兆三百六十八億五千四百七十七万五千八百七
$ ((x=(2**63)-1)); echo $x; ((x++)); echo $x;
Result:
9223372036854775807
-9223372036854775808
Interesting note:
9,223,372,036,854,775,807
is
nine quintillion two hundred twenty three quadrillion three hundred seventy two trillion thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred seven
is
九百二十二京三千三百七十二兆三百六十八億五千四百七十七万五千八百七
Monday, June 5, 2017
Saturday, June 3, 2017
macOS Sierra: Generating Integers with Arbitrary Size
Command (Testing with 15 digits):
$ rnd=$(cat /dev/urandom | LC_CTYPE=C tr -dc 0-9 | fold -w15 | head -1 | sed 's/^0*//;'); echo $rnd; echo $(($rnd / 2));
Result (Second line is the number half the size of the first line to test if arithmetic operation works):
585206567422781
292603283711390
$ rnd=$(cat /dev/urandom | LC_CTYPE=C tr -dc 0-9 | fold -w15 | head -1 | sed 's/^0*//;'); echo $rnd; echo $(($rnd / 2));
Result (Second line is the number half the size of the first line to test if arithmetic operation works):
585206567422781
292603283711390
Friday, June 2, 2017
macOS Sierra: Bash (Unix shell): Associative Array Example
Command:
$ cat associative_array.sh
Result:
#!/bin/bash
aa=(
'スライム::100'
'キメラ::10'
'犬::20'
'猫::80'
)
total=0
for k in "${aa[@]}"
do
value="${k##*::}"
total=$((total + value))
done
echo "Total: $total"
index=$1
current=0
for k in "${aa[@]}"
do
value="${k##*::}"
current=$((current + value))
if [ $index -le $current ]
then
name="${k%%::*}"
echo "Index $index: $name"
break
fi
done
Commands & Results:
$ ./associative_array.sh 0
Total: 210
Index 0: スライム
$ ./associative_array.sh 1
Total: 210
Index 1: スライム
$ ./associative_array.sh 10
Total: 210
Index 10: スライム
$ ./associative_array.sh 99
Total: 210
Index 99: スライム
$ ./associative_array.sh 100
Total: 210
Index 100: スライム
$ ./associative_array.sh 101
Total: 210
Index 101: キメラ
$ ./associative_array.sh 110
Total: 210
Index 110: キメラ
$ ./associative_array.sh 111
Total: 210
Index 111: 犬
$ ./associative_array.sh 130
Total: 210
Index 130: 犬
$ ./associative_array.sh 131
Total: 210
Index 131: 猫
$ ./associative_array.sh 200
Total: 210
Index 200: 猫
$ ./associative_array.sh 209
Total: 210
Index 209: 猫
$ ./associative_array.sh 210
Total: 210
Index 210: 猫
$ ./associative_array.sh 211
Total: 210
$ cat associative_array.sh
Result:
#!/bin/bash
aa=(
'スライム::100'
'キメラ::10'
'犬::20'
'猫::80'
)
total=0
for k in "${aa[@]}"
do
value="${k##*::}"
total=$((total + value))
done
echo "Total: $total"
index=$1
current=0
for k in "${aa[@]}"
do
value="${k##*::}"
current=$((current + value))
if [ $index -le $current ]
then
name="${k%%::*}"
echo "Index $index: $name"
break
fi
done
Commands & Results:
$ ./associative_array.sh 0
Total: 210
Index 0: スライム
$ ./associative_array.sh 1
Total: 210
Index 1: スライム
$ ./associative_array.sh 10
Total: 210
Index 10: スライム
$ ./associative_array.sh 99
Total: 210
Index 99: スライム
$ ./associative_array.sh 100
Total: 210
Index 100: スライム
$ ./associative_array.sh 101
Total: 210
Index 101: キメラ
$ ./associative_array.sh 110
Total: 210
Index 110: キメラ
$ ./associative_array.sh 111
Total: 210
Index 111: 犬
$ ./associative_array.sh 130
Total: 210
Index 130: 犬
$ ./associative_array.sh 131
Total: 210
Index 131: 猫
$ ./associative_array.sh 200
Total: 210
Index 200: 猫
$ ./associative_array.sh 209
Total: 210
Index 209: 猫
$ ./associative_array.sh 210
Total: 210
Index 210: 猫
$ ./associative_array.sh 211
Total: 210
Thursday, June 1, 2017
Subscribe to:
Posts (Atom)