Register
It is currently Wed Aug 20, 2014 6:40 am

Fetch random string through HTTPS


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sun Feb 21, 2010 4:36 am   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
Code:
#!/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!


Top
 Profile  
 PostPosted: Sun Feb 21, 2010 10:34 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
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:
#!/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


Top
 Profile  
 PostPosted: Mon Feb 22, 2010 2:07 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 534
e633 will never get num and len, unless zenity returns an error code that is not 0.

try this to understand:
Code:
#!/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.


Top
 Profile  
 PostPosted: Mon Feb 22, 2010 3:41 am   
User avatar

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


Top
 Profile  
 PostPosted: Mon Feb 22, 2010 10:40 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
You could tee the last part then to get a copy of it in a file.

Code:
# 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


Top
 Profile  
 PostPosted: Mon Feb 22, 2010 11:23 am   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
thobbs wrote:
You could tee the last part then to get a copy of it in a file.

Code:
# 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!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP