Fetch random string through HTTPS

All Scripts go here. In progress, finished, etc.

Fetch random string through HTTPS

Postby e633 » Sun Feb 21, 2010 4:36 am

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!
User avatar
e633
 
Posts: 16
Joined: Sun Nov 01, 2009 8:59 am
Location: Try to guess!

Re: Fetch random string through HTTPS

Postby thobbs » Sun Feb 21, 2010 10:34 pm

You can pipe directly from curl to tr to xclip, so don't have to use a temp file or a lock file.

touch is atomic, so there's no need to touch a different file and move it to the lock file.

You can also negate your conditions so that you don't have a useless then clause followed by the else that you actually want.

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

#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."
then
  if !(num="$(zenity --entry --title="How many" --text="How many strings? (max 10000)" --entry-text "$num")"); then
      num=5
  fi

  if !(len="$(zenity --entry --title="Length" --text="Length of each string? (max 20)" --entry-text "$len")"); then
      len=10
  fi
fi

curl --silent "https://www.random.org/strings/?num=$num&len=$len&digits=on&upperalpha=on&loweralpha=on&unique=off&format=plain&rnd=new" | \
  tr -d '\012' | \
  xclip -selection clipboard


- thobbs
User avatar
thobbs
 
Posts: 73
Joined: Sat Jun 13, 2009 8:53 pm
Location: Texas!

Re: Fetch random string through HTTPS

Postby Watael » Mon Feb 22, 2010 2:07 am

e633 will never get num and len, unless zenity returns an error code that is not 0.

try this to understand:
Code: Select all
#!/bin/bash

if ! ( n=6 ); then
n=3
fi
echo $n
prints nothing, because n=6 is done in a sub-shell, thus the script will never be aware of it.
And n will not be 3, because n=6 has error code equal to 0.
Watael
 
Posts: 232
Joined: Mon Mar 02, 2009 3:03 am

Re: Fetch random string through HTTPS

Postby e633 » Mon Feb 22, 2010 3:41 am

Thanks for your corrections, I badly need to correct my style!
Anyway i do want to use a temp file because i don't want to risk to lose the string, as i usually use them as passwords.
BTW... damn subshells! It's the only thing i hate in bash scripting so far... x_x
User avatar
e633
 
Posts: 16
Joined: Sun Nov 01, 2009 8:59 am
Location: Try to guess!

Re: Fetch random string through HTTPS

Postby thobbs » Mon Feb 22, 2010 10:40 am

You could tee the last part then to get a copy of it in a file.

Code: Select all
# Other code goes here ...

curl --silent "https://www.random.org/strings/?num=$num&len=$len&digits=on&upperalpha=on&loweralpha=on&unique=off&format=plain&rnd=new" | \
  tr -d '\012' | \
  tee "$tempfile" | \
  xclip -selection clipboard
User avatar
thobbs
 
Posts: 73
Joined: Sat Jun 13, 2009 8:53 pm
Location: Texas!

Re: Fetch random string through HTTPS

Postby e633 » Mon Feb 22, 2010 11:23 am

thobbs wrote:You could tee the last part then to get a copy of it in a file.

Code: Select all
# Other code goes here ...

curl --silent "https://www.random.org/strings/?num=$num&len=$len&digits=on&upperalpha=on&loweralpha=on&unique=off&format=plain&rnd=new" | \
  tr -d '\012' | \
  tee "$tempfile" | \
  xclip -selection clipboard


Perfect, thanks again, you guys are invaluable!
User avatar
e633
 
Posts: 16
Joined: Sun Nov 01, 2009 8:59 am
Location: Try to guess!


Return to The Sandbox

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron