Last Updated: July 08, 2016
·
349
· dimitrismistriotis

XKCD password generator in fish shell

! /usr/bin/env fish

XKCD style password generator from shell.

Grabbed words from here: http://www.use-in-a-sentence.com/english-words/10000-words/the-most-frequent-10000-words-of-english.html

and placed them in "words.txt" which should be in the same location as this

script. Will consider adding it as a command line parameter in a future

version.

For argument parsing:

https://butterflyprogramming.neoname.eu/programming-with-fish-shell/

http://stackoverflow.com/questions/16048757/fish-function-option-param-parser

set number_postfix "f"

for item in $argv
switch "$item"
case -n
set number_postfix "t"
end
end

set currentdir (dirname (status --current-filename))
set separator "
" # One character or it breaks later

set philedata (cat $current_dir/words.txt)

echo $philedata

set data_length (count $philedata)

echo "data length $data_length"

set randomitems (shuf -i 1-$datalength -n 4)

echo "random items $random_items"

set stringagg ""
for position in $random
items
# echo $position
# echo $philedata[$position]
# echo "------------"
set stringagg "$stringagg""$philedata[$position]""$separator"
end

Hack to remove last separator:

set stringagg (echo $stringagg | rev | cut -c 2- | rev)

echo $string_agg

if [ $numberpostfix = "t" ]
echo "$string
agg""$separator"(shuf -i 1-100 -n 1)
else
echo "$string_agg"
end

1 Response
Add your response