- Code: Select all
#!/bin/bash
###############################################################
# Fetch a random string from www.random.org through HTTPS,
# write it to file and copy it to clipboard.
#
# Script by e633
# DEPENDENCIES: curl, xclip, zenity
###############################################################
num=5
len=10
outfile=~/Desktop/random-temp
LCKoutfilename="$outfile.lck"
#Parameters customization
if zenity --question --title "Random.org string fetcher" --text "Personalize parameters?
NOTE: the amount of randomness you can fetch in 24hrs is limited." #Else proceed with default values
then
if num="$(zenity --entry --title="How many" --text="How many strings? (max 10000)" --entry-text "$num")"
then $DONOTHING
else num=5
fi
if len="$(zenity --entry --title="Length" --text="Length of each string? (max 20)" --entry-text "$len")"
then $DONOTHING
else len=10
fi
fi
if [ -f "$LCKoutfilename" ]; then #Check if file is already in use
zenity --error --title "Race condition detected" --text "File already in use."
exit
else
touch $outfile
mv $outfile $LCKoutfilename #Lock file
echo "`curl --silent "https://www.random.org/strings/?num=$num&len=$len&digits=on&upperalpha=on&loweralpha=on&unique=off&format=plain&rnd=new"`" >> $LCKoutfilename # Alphanumeric, uppercase & lowercase
tr -d '\012' < $LCKoutfilename > ~/Desktop/randomString #Create new file without newlines
rm $LCKoutfilename #remove old file
outfile=~/Desktop/randomString
cat $outfile | xclip -selection clipboard #Copy random string to clipboard
if zenity --question --title "Delete?" --text "Random string already copied to clipboard.
Delete temporary file?"
then rm $outfile
fi
fi
I'd like not to use another file when deleting newline chars (line 40). Any idea?
Enjoy!

