On my First Script

Well as promised it is time to explain parts of my script. Well I shall start at the beginning.  The !#/bin/bash tell the OS where the interpretor for the script can be found. The following line is just a place where most of the work will be done.  This should actually be changed to cd ~ if I were planning on sharning this script (which I am).

Now for the more “interesting” aspects.

ls -a | grep “^\.” > temp

This line will first list all the fils found in a directory (ls -a). Then it will find those that begin with a ‘.’ (dot) and print them to a file called temp.  This is done through the use of grep “^\.” grep works with regx, and in the one used here it will match ‘phrases’ that begin with a ‘.’ (dot).  Pretty much grep can do a lot of things (or so I have read) but they rely on regx so needless to say I am currently working on learning regx.  Anyway back to the code. After this I do a search to find a file called .myPw, this is where the password will be stored.  So if the file doesn’t exist we shall create it, along with a password to place into it.

After this I basically use some basic loops to test the user’s input to see if it matches the password.  I won’t talk about the loop structure since 1: this isn’t a tutorial on bash, and 2: it is easy enough to figure out from just reading the code.  I won’t explain what echo does, if you don’t know just click on the man link at the top of this blog and then click on echo.  It’s actually a pretty “simple” command.

One last thing, I would like to point out that after making the file temp I deleted it once I no longer needed it. This turns out to be somewhat optinal (I’ll explain in a moment), but I think it’s just “polite” to delete temporary files you put on someone else’s system.  Now for why this is optional.  The way the code is set up, if there already exist a file called temp it will overwrite it.  If however I had used

ls -a | grep “^\.” >> temp

It would append the output to the end of the file (in which case who knows what was in there before and how much you will have to seach through while looking for .myPw).  So there is a BIG difference between > and >>, both direct output to a file but in differnt fashions.

Advertisements

2 Responses to On my First Script

  1. Miorel says:

    By the way, when working with temporary files you shouldn’t assume that, say, “mytemp” is a safe name because the user might already be storing something there.

    Use mktemp or tempfile instead. I’ll let you read the respective man pages on your own 😉

  2. putnam120 says:

    oh thanks for the advice.

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: