Showing posts with label One-liner program. Show all posts
Showing posts with label One-liner program. Show all posts

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

Monday, October 17, 2016

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, 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