First Complete Script

Here is my first “real” script. It does what it was designed to do, but not as nicely as I would have liked. So needless to say there will be some updating.  As for the script itself it is supposed to prompt the user for a password before opening firefox.  If a password doesn’t currently exist it will ask them to make a new one, that will be saved into a “hidden” file. Really it’s pretty easy to get around but it should be enough to keep your computer illiterate friends away/busy at least.

Here is the script:

#!/bin/bash

cd <path> #location of password file
there=false
ls -a | egrep '^\.' > mytemp

#chek if file with password exists
while read f
do
	if [ "$f" = ".myPw" ]; then
		there=true;
	fi
done < mytemp
rm mytemp 

#if not make it
if [ "$there" = "false" ]; then
   echo "Enter a password:"
   read newPass
   echo $newPass > .myPw
   chmod 777 .myPw #make possible to read and write to file (needed for later on)
fi

input=:
correct=false
j=:

#get actuall password
while read j
do
	PW=$j
done < .myPw

#read until correct pw is given
while [ "$correct" = "false" ]
do
	echo "Enter password or ^C (Ctrl + C) to quit:"
	read input
	if [ "$input" = "$PW" ]
	then
		correct=true
	else
		echo "Incorrect password"
	fi
done
firefox&

while :
do
   ii=2
done

Sorry it’s not in the best font but at least the formatting is there. Anyway the part I don’t like is the infinite loop at the end. However, I haven’t found a way to keep firefox open if it’s not there. With out it, once the correct password is entered the script will end but firefox won’t start.

Well in my next post I’ll try to explain what each line does. Should be a good exercise in talking about terminal commands.

Advertisements

One Response to First Complete Script

  1. Miorel says:

    Forgot to keep up with your new blog, it’s got some good stuff! Anyway, you may want to look into the nohup command. I believe “nohup firefox >& /dev/null &” will fix your infinite loop problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: