Showing posts with label Bash (Unix shell). Show all posts
Showing posts with label Bash (Unix shell). Show all posts

Thursday, August 15, 2019

macOS Mojave: Bash: Copying SFML (Simple and Fast Multimedia Library) to Xcode

Command:

$ pwd

Result:

/Users/USERNAME/Downloads/SFML-2.5.1-macos-clang

Command:

$ sudo cp -r Frameworks/* /Library/Frameworks/
$ sudo cp -r lib/* /usr/local/lib
$ sudo cp -r extlibs/* /Library/Frameworks/
$ sudo mkdir /Library/Developer/Xcode
$ sudo mkdir /Library/Developer/Xcode/Templates
$ sudo cp -r templates/* /Library/Developer/Xcode/Templates/

Monday, July 9, 2018

macOS High Sierra: textutil: Converting .webarchive Format to HTML and Images

Command:

$ textutil -convert html ~/Desktop/channels.webarchive


Result:

Wednesday, August 9, 2017

Bash (Unix shell): Converting Bitcoin Cash Amount to Yen

Command:

$ cat ./cryptocurrency_price_bittrex.sh


Result:

#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 PAIR AMOUNT" >&2
exit 1
fi

pair=$1;
amount=$2;
yenrate=$(curl -s "https://coincheck.com/api/rate/BTC_JPY" | python -c "import sys, json; print json.load(sys.stdin)['rate']");
curl -s "https://bittrex.com/api/v1.1/public/getticker?market=$pair" | python -c "import sys, json; print float(json.load(sys.stdin)['result']['Last']) * float(sys.argv[1]) * float(sys.argv[2])" $amount $yenrate


Command (How many Yen for 0.19 Bitcoin Cash):

$ ./cryptocurrency_price_bittrex.sh BTC-BCC 0.19


Result:

7461.446566

Tuesday, August 1, 2017

Bash (Unix shell): Getting Cryptocurrency Price Using API

Command:

$ cat cryptocurrency_price.sh


Result:

#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 PAIR AMOUNT" >&2
exit 1
fi
pair=$1; amount=$2; curl -s "https://coincheck.com/api/rate/$pair" | python -c "import sys, json; print float(json.load(sys.stdin)['rate']) * float(sys.argv[1])" $amount


Command:

$ ./cryptocurrency_price.sh


Result:

Usage: ./cryptocurrency_price_jpy.sh PAIR AMOUNT


Command:

$ ./cryptocurrency_price.sh btc_jpy 1


Result:

318146.0


Command:

$ ./cryptocurrency_price_jpy.sh eth_jpy 13.5


Result:

303218.972773


Command:

$ ./cryptocurrency_price_jpy.sh xem_jpy 1000


Result:

18703.59758

Friday, June 9, 2017

macOS Sierra: Bash (Unix shell): Associative Array Example: Reading from CSV into Array

Command:

$ cat associative_array3.sh


Result:

#!/bin/bash

aa=()

while IFS=, read -ra line; do if [ -n "${line[0]}" ]; then aa+=("${line[0]}::${line[3]}");fi;done < List\ of\ organisms\ by\ population.csv

total=0

for k in "${aa[@]}"
do
value="${k##*::}"
total=$((total + value))
done

echo "Total population: $total"

LC_CTYPE=C; MIN=1; MAX=$total; while rnd=$(cat /dev/urandom | tr -dc 0-9 | fold -w${#MAX} | head -1 | sed 's/^0*//;'); [ -z $rnd ] && rnd=0; (( $rnd < $MIN || $rnd > $MAX)); do :; done;

echo "Random index: $rnd"

index=$rnd

current=0

for k in "${aa[@]}"
do
value="${k##*::}"
current=$((current + value))

if [ $index -le $current ]
then
name="${k%%::*}"
population=$value
break
fi
done

echo "$name (population: $population)"
printf "Probability: "
echo "$population/$total" | bc -l


Command:

$ ./associative_array3.sh


Result:

Total population: 13958220136
Random index: 8955227583
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 4227209587
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 7805864096
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 8511144278
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 3892081176
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 2589979307
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 7946872317
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 372258826
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 13908122684
Short-eared owl (population: 2000000)
Probability: .00014328474408006714

Result:

Total population: 13958220136
Random index: 7247815301
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 9352855300
Domestic sheep (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 9642135454
Domestic sheep (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 11711491958
Cat (population: 600000000)
Probability: .04298542322402014307

Result:

Total population: 13958220136
Random index: 3990365909
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 272907397
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 9041133545
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 7777621570
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 9017444984
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 4118816981
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 3797333834
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 12061584807
Mourning dove (population: 475000000)
Probability: .03403012671901594660

Result:

Total population: 13958220136
Random index: 12170053267
Mourning dove (population: 475000000)
Probability: .03403012671901594660

Result:

Total population: 13958220136
Random index: 531916373
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 10657946193
Domestic goat (population: 850000000)
Probability: .06089601623402853602

Result:

Total population: 13958220136
Random index: 13481628295
Collared trogon (population: 50000000)
Probability: .00358211860200167858

Result:

Total population: 13958220136
Random index: 9130914826
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 7627418145
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 12225242832
Mourning dove (population: 475000000)
Probability: .03403012671901594660

Result:

Total population: 13958220136
Random index: 3731595661
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 11796334858
Cat (population: 600000000)
Probability: .04298542322402014307

Result:

Total population: 13958220136
Random index: 7477886491
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 760089938
Human (population: 7350000000)
Probability: .52657143449424675272

Thursday, June 8, 2017

macOS Sierra: Bash (Unix shell): Associative Array Example: Random Bird (using List of birds by population)

Command:

$ cat associative_array2.sh


Result:

#!/bin/bash

aa=(
'Mallard::17000000'
'Chimney swift::15000000'
'European nightjar::6000000'
'Thick-billed murre::22000000'
'White-faced ibis::1200000'
'Rock dove::260000000'
'Rainbow bee-eater::1000000'
'Common cuckoo::100000000'
'Common Kestrel::5000000'
'Willow ptarmigan::40000000'
'Pacific loon::1600000'
'American coot::6000000'
'Red-footed booby::1000000'
'Lesser flamingo::3240000'
'Great spotted woodpecker::216000000'
'Black-necked grebe::42000000'
'Short-tailed shearwater::23000000'
'Red-tailed black cockatoo::100000'
'Macaroni penguin::18000000'
'Short-eared owl::2000000'
'Southern brown kiwi::29800'
'Little tinamou::4999999'
'Collared trogon::50000000'
'Laysan duck::521'
'Juan Fernández firecrown::3000'
'Puerto Rican nightjar::2000'
'Shore dotterel::250'
"Storm's stork::500"
'Socorro dove::100'
'Narcondam hornbill::340'
'Black-hooded coucal::70'
'Madagascar fish eagle::360'
'Bornean peacock-pheasant::2499'
'Yellow-billed loon::32000'
'Lord Howe woodhen::230'
'Flightless cormorant::1679'
'Andean flamingo::38000'
'Ivory-billed woodpecker::50'
'New Zealand grebe::2000'
'New Zealand storm petrel::50'
'Kakapo::126'
'Galapagos penguin::1800'
'Forest owlet::400'
'Little spotted kiwi::1200'
'Slaty-breasted tinamou::49999'
'Javan trogon::1500'
)

total=0

for k in "${aa[@]}"
do
value="${k##*::}"
total=$((total + value))
done

echo "Total population: $total"

LC_CTYPE=C; MIN=1; MAX=$total; while rnd=$(cat /dev/urandom | tr -dc 0-9 | fold -w${#MAX} | head -1 | sed 's/^0*//;'); [ -z $rnd ] && rnd=0; (( $rnd < $MIN || $rnd > $MAX)); do :; done;

echo "Random index: $rnd"

index=$rnd

current=0

for k in "${aa[@]}"
do
value="${k##*::}"
current=$((current + value))

if [ $index -le $current ]
then
name="${k%%::*}"
population=$value
break
fi
done


echo "$name (population: $population)"
printf "Probability: "
echo "$population/$total" | bc -l


Command:

$ ./associative_array2.sh


Result:

Total population: 835308473
Random index: 763607797
Macaroni penguin (population: 18000000)
Probability: .02154892543511886536

Result:

Total population: 835308473
Random index: 442019183
Willow ptarmigan (population: 40000000)
Probability: .04788650096693081191

Result:

Total population: 835308473
Random index: 600337171
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435

Result:

Total population: 835308473
Random index: 542347304
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435

Result:

Total population: 835308473
Random index: 399097554
Common cuckoo (population: 100000000)
Probability: .11971625241732702979

Result:

Total population: 835308473
Random index: 602221971
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435

Result:

Total population: 835308473
Random index: 31407682
Chimney swift (population: 15000000)
Probability: .01795743786259905446

Result:

Total population: 835308473
Random index: 471711629
American coot (population: 6000000)
Probability: .00718297514503962178

Result:

Total population: 835308473
Random index: 127100259
Rock dove (population: 260000000)
Probability: .31126225628505027746

Result:

Total population: 835308473
Random index: 380012920
Common cuckoo (population: 100000000)
Probability: .11971625241732702979

Result:

Total population: 835308473
Random index: 394531514
Common cuckoo (population: 100000000)
Probability: .11971625241732702979

Result:

Total population: 835308473
Random index: 78048210
Rock dove (population: 260000000)
Probability: .31126225628505027746

Result:

Total population: 835308473
Random index: 33341909
European nightjar (population: 6000000)
Probability: .00718297514503962178

Result:

Total population: 835308473
Random index: 211802308
Rock dove (population: 260000000)
Probability: .31126225628505027746

Result:

Total population: 835308473
Random index: 94119789
Rock dove (population: 260000000)
Probability: .31126225628505027746

Result:

Total population: 835308473
Random index: 359620354
Common cuckoo (population: 100000000)
Probability: .11971625241732702979

Result:

Total population: 835308473
Random index: 429592312
Willow ptarmigan (population: 40000000)
Probability: .04788650096693081191

Result:

Total population: 835308473
Random index: 199643906
Rock dove (population: 260000000)
Probability: .31126225628505027746

Result:

Total population: 835308473
Random index: 656519406
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435

Result:

Total population: 835308473
Random index: 448395870
Willow ptarmigan (population: 40000000)
Probability: .04788650096693081191

Result:

Total population: 835308473
Random index: 392079604
Common cuckoo (population: 100000000)
Probability: .11971625241732702979

Result:

Total population: 835308473
Random index: 61798025
Rock dove (population: 260000000)
Probability: .31126225628505027746

Result:

Total population: 835308473
Random index: 778932643
Short-eared owl (population: 2000000)
Probability: .00239432504834654059

Result:

Total population: 835308473
Random index: 393661406
Common cuckoo (population: 100000000)
Probability: .11971625241732702979

Result:

Total population: 835308473
Random index: 88104221
Rock dove (population: 260000000)
Probability: .31126225628505027746

Result:

Total population: 835308473
Random index: 623477130
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435

Result:

Total population: 835308473
Random index: 401873132
Common cuckoo (population: 100000000)
Probability: .11971625241732702979

Result:

Total population: 835308473
Random index: 680185574
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435

Result:

Total population: 835308473
Random index: 724901495
Black-necked grebe (population: 42000000)
Probability: .05028082601527735251

Result:

Total population: 835308473
Random index: 281804665
Rock dove (population: 260000000)
Probability: .31126225628505027746

Tuesday, June 6, 2017

macOS Sierra: Bash (Unix shell): Generating Random Number in Arbitrary Range

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

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

九百二十二三千三百七十二三百六十八五千四百七十七五千八百七

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

Sunday, May 21, 2017

Cron: Copying all Labels from Blog to a File (Example)

Command:

$ cat smashingtheory_label_urls.sh


Result:

if [ $# -eq 1 ]; then
curl -s https://smashingtheory.blogspot.jp | sed -n "s/.*dir='ltr' href='https:\/\/smashingtheory.blogspot.jp\/search\/label\/\\([^']*\\).*/\1/p" > $1
else
echo "Usage: smashingtheory_label_urls.sh FILENAME" >&2
fi


Note:

Double backslashes before the round brackets should be single backslash. I did this to escape Mathjax's behavior.

Monday, November 14, 2016

Ubuntu: Dialog: Installation and Hello World

Command:

$ sudo apt-get install dialog


Result:

[sudo] password for USERNAME:
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  dialog
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 215 kB of archives.
After this operation, 1,138 kB of additional disk space will be used.
Get:1 http://jp.archive.ubuntu.com/ubuntu yakkety/universe amd64 dialog amd64 1.3-20160424-1 [215 kB]
Fetched 215 kB in 0s (1,258 kB/s)
Selecting previously unselected package dialog.
(Reading database ... 88450 files and directories currently installed.)
Preparing to unpack .../dialog_1.3-20160424-1_amd64.deb ...
Unpacking dialog (1.3-20160424-1) ...
Setting up dialog (1.3-20160424-1) ...
Processing triggers for man-db (2.7.5-1) ...


Command:

$ dialog --title "Hello" --msgbox 'Hello World!' 6 20


Graphical output:

Bash dialog Hello World!

Monday, October 17, 2016

Bash (Unix shell): Select Menu Example: Variable

Command:

$ select item in {a..z}; do echo $item ":" $REPLY; done


Result:

1) a    3) c   5) e   7) g   9) i  11) k  13) m 15) o 17) q  19) s  21) u  23) w  25) y
2) b    4) d   6) f   8) h  10) j  12) l  14) n 16) p 18) r  20) t  22) v  24) x  26) z
#?


User input:

1


Result:

a : 1

Bash (Unix shell): Changing Command Prompt

Command:

$ PS1="( ^ω^ ) "


Result:

( ^ω^ )


Command:

( ^ω^ ) PS1="$ "


Result:

$

Bash (Unix shell): Select Menu Example: PS3

Command:

$ PS3="please select item > "; select item in "item1" "item2" "item3"; do if [ $REPLY = "q" ]; then break; fi; done;


Result:

1) item1
2) item2
3) item3
please select item >


User input:

1


Result:

please select item >


User input:

q

Bash (Unix shell): Command prompt (コマンドプロンプト)

Command:

$ read -p "Do something? "; if [ $REPLY == "y" ]; then echo yay; fi


Result:

Do something?


User input:

y


Result:

yay

Tuesday, September 13, 2016

macOS Sierra: Bash Version

Command:

$ bash --version


Result:

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.

Ubuntu: Bash Version

Command:

$ bash --version


Result:

GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Tuesday, August 30, 2016

Ruby: Print Out Multiple Lines

Command:

$ printf "1,name1,address1\n2,name2,address2" | ruby -e "while status = STDIN.gets do = STDIN.gets do
print status
end"


Result:

1,name1,address1
2,name2,address2

Friday, July 1, 2016

Bash: Collatz Conjecture

Command:

$ cat ./collatz.sh

Result:

#!/bin/bash

collatz () {
        echo $1

        if (($1 != 1)) ; then
                if (($1 % 2 == 0)) ; then

                        collatz $(($1 / 2))
                else
                        collatz $((3 * $1 + 1))
                fi
        fi
}

collatz $1


Command:

$ ./collatz.sh 123

Result:

123
370
185
556
278
139
418
209
628
314
157
472
236
118
59
178
89
268
134
67
202
101
304
152
76
38
19
58
29
88
44
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1


Thursday, June 9, 2016

Bash: Posts Per Hour

Command (for this year's post per hour):

$ posts=`./smashingtheory_posts_this_year.sh`;doy=`date +%j`;hour=`date +%H`; echo "scale=4;$posts / ($doy * 24 + $hour)" | bc -l | tr '\n' ' '

Result:

.1248

Command (for this month's post per hour):

$ posts=`./smashingtheory_posts_this_month.sh`;dom=`date +%e`;hour=`date +%H`;echo "scale=4;$posts / ($dom * 24 + $hour)" | bc -l | tr '\n' ' '

Result:

.1710