<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eTechTips &#187; Scripting</title>
	<atom:link href="http://www.etechtips.com/articles/programming/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.etechtips.com</link>
	<description>Your Technical resource</description>
	<lastBuildDate>Wed, 01 Feb 2012 21:38:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Python: error when using the input() function</title>
		<link>http://www.etechtips.com/2012/01/20/python-error-when-using-the-input-function/</link>
		<comments>http://www.etechtips.com/2012/01/20/python-error-when-using-the-input-function/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 17:38:24 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=610</guid>
		<description><![CDATA[I was writing a basic guessing game and when I tried to take user input I was getting the following: Code: guess = input() d Error: Traceback (most recent call last): File "", line 1, in File "", line 1, in NameError: name 'd' is not defined The problem is actually with the version of [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing a basic guessing game and when I tried to take user input I was<br />
getting the following:</p>
<p>Code:</p>
<pre>
guess = input()
d
</pre>
<p>Error:</p>
<pre>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'd' is not defined
</pre>
<p>The problem is actually with the version of Python that I was using.  If you<br />
are using Python 2.x, you need to use the raw_input() function. And with 3.x<br />
you can use the input() function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2012/01/20/python-error-when-using-the-input-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: if/else Statements</title>
		<link>http://www.etechtips.com/2012/01/20/python-ifelse-statements/</link>
		<comments>http://www.etechtips.com/2012/01/20/python-ifelse-statements/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 16:25:47 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=601</guid>
		<description><![CDATA[There comes a time when you are writing a program and you need to branch based on input to a program. This is the format of the if/else statement in Python. if &#60;some true/false statement&#62; : &#60;some statements that run if the if statement is true&#62; else: &#60;some other statements that run if the if [...]]]></description>
			<content:encoded><![CDATA[<p>There comes a time when you are writing a program and you need to branch based on input to a program.<br />
This is the format of the if/else statement in Python.  </p>
<pre>
if &lt;some true/false statement&gt; :
  &lt;some statements that run if the if statement is true&gt;
else:
  &lt;some other statements that run if the if statement are false&gt;
</pre>
<p>There may be times when you have more than one value set to test against.  In that case you can write a many if/else<br />
statements but you can chain the if/else statements with an elif.  So the chain becomes if/elif/else.</p>
<pre>
if x < 5:
  &lt;some statements that run if the if statement is true&gt;
elif x > 5 and x < 10:
  &lt;some statements that run if the elif statement is true&gt;
else:
  &lt;if none of the if checks are true, run the statements in the else case.&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2012/01/20/python-ifelse-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: Allow for empty if else blocks</title>
		<link>http://www.etechtips.com/2012/01/20/python-allow-for-empty-if-else-blocks/</link>
		<comments>http://www.etechtips.com/2012/01/20/python-allow-for-empty-if-else-blocks/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 16:15:28 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=605</guid>
		<description><![CDATA[I was toying with a prime number finder and was creating a set of if/else statements for a horribly failed attempt and wanted to skip to the next number on any of the if statements that were true. I didn&#8217;t want to put an empty print statement as that would disrupt the output I was [...]]]></description>
			<content:encoded><![CDATA[<p>I was toying with a prime number finder and was creating a set of if/else statements for a horribly failed attempt<br />
and wanted to skip to the next number on any of the if statements that were true.  I didn&#8217;t want to put an empty print<br />
statement as that would disrupt the output I was expecting from the program.  I found the pass statement.<br />
Here is an example from my not very successful attempt at finding primes:</p>
<pre>
if inp > 2 and inp % 2 == 0:
  pass
elif inp > 3 and inp % 3 == 0:
  pass
elif inp > 7 and inp % 7 == 0:
  pass
elif inp > 9 and inp % 9 == 0:
  pass
else:
  print inp, " is a prime"
</pre>
<p>Yes, this does not accurately find the primes, but it would skip any number that is divisible by 2,3,5,7, or 9.  And<br />
anything that does not match this will be listed as a prime, this is incorrect and would be made to add new elif<br />
statements as each prime was found.   </p>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2012/01/20/python-allow-for-empty-if-else-blocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl: Format date strings</title>
		<link>http://www.etechtips.com/2011/12/07/perl-format-date-strings/</link>
		<comments>http://www.etechtips.com/2011/12/07/perl-format-date-strings/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 19:48:57 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=586</guid>
		<description><![CDATA[The other day I had a requirement to fill in a date value while creating an entity in ClearQuest. The field that was required was a date and was formatted as &#8220;mm/dd/yyyy hh:MM::SS AM&#124;PM&#8221;. I used the localtime function to return the date and used sprintf to format the date to the proper output. The [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I had a requirement to fill in a date value while creating an entity in ClearQuest.  The field that was required was a date and was formatted as &#8220;mm/dd/yyyy hh:MM::SS AM|PM&#8221;.  I used the localtime function to return the date and used sprintf to format the date to the proper output.<br />
The reason I used sprintf was that for hours, months and days less 10 the output was 1 digit instead of two and the input was not allowed.  The %02d states that the digits used will be a minumum of 2 in this case.</p>
<p>Here is the sample program I wrote to test the expected output:</p>
<pre>
 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;

# formatted just by concatenating output
my $datestring = $mon . "/" . $mday . "/" . $year . "  " . $hour . ":" . $min.
":" . $sec  ;

# formatted using sprintf to ensure proper digit counts are output
my $datestring2 = sprintf("%02d/%02d/%04d %d:%02d:%02d %s",$mon,$mday,1900 +$yea
r,$hour,$min,$sec, $hour >= 12? "PM" : "AM");

 print $datestring;
  print $datestring2;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2011/12/07/perl-format-date-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DOS: Check for empty environment variable</title>
		<link>http://www.etechtips.com/2011/11/16/dos-check-for-empty-environment-variable/</link>
		<comments>http://www.etechtips.com/2011/11/16/dos-check-for-empty-environment-variable/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 20:52:57 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[DOS]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=527</guid>
		<description><![CDATA[I was writing a batch file the other day and needed to change the argumets to a command based on if the user had specified his password on the command line. SET PASS=somepassword : or :SET PASS= if x%PASS%= X ( SET PASSARG=--password somepassword ) else ( SET PASSARG= ) commandtorun %PASSARG% Now if the [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing a batch file the other day and needed to change the argumets to a command based on if the user had specified his password on the command line.</p>
<pre>
SET PASS=somepassword
: or
:SET PASS=
if x%PASS%= X (
  SET PASSARG=--password somepassword
) else
(
  SET PASSARG=
)

commandtorun %PASSARG%
</pre>
<p>Now if the user comments out his password, the script will make the PASSARG environment variable be empty or if they set a password the passarg variable will be filled in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2011/11/16/dos-check-for-empty-environment-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java: Command line argument processing</title>
		<link>http://www.etechtips.com/2011/11/06/java-command-line-argument-processing/</link>
		<comments>http://www.etechtips.com/2011/11/06/java-command-line-argument-processing/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 04:44:15 +0000</pubDate>
		<dc:creator>techtipse</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=71</guid>
		<description><![CDATA[In each language that I have learned I have found it useful to know how to process command line arguments and here is a simple framework that I use for Java. I usually wrap the java command line in a bat or shell file to make it easier to run so that the user does [...]]]></description>
			<content:encoded><![CDATA[<p>In each language that I have learned I have found it useful to know how to process command line arguments and here is a simple framework that I use for Java.  I usually wrap the java command line in a bat or shell file to make it easier to run so that the user does not have to worry about classpath or path issues.</p>
<p>The following is the basic code to handle the command line arguments.</p>
<pre>
class CmdArgs
{

  public static void main(String[] args)
  {
      int argsize = args.length;
      String curarg;

      if (argsize > 0 )
      {

      }

      for(int i = 0; i < argsize; i++)
      {
          if (args[i].equals("--host")
          {
             args++;
             host = args[i];
           }
          else if (args[i].equals("--debug")
          {
               debug = 1;
          }
          else
          {
              System.out.println("Unknown option: " + args[i]);
              System.exit(1);
          }
       }

   }
}
</pre>
<p>The bat file for Dos/Windows might look like this:</p>
<pre>
@echo off

java -cp c:\javabin;somejar.jar  com.etechtips.CmdArgs %*
</pre>
<p>The shell based file might look like:</p>
<pre>

java -cp /home/ecdown/javabin:somejar.jar com.etechtips.CmdArgs $*
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2011/11/06/java-command-line-argument-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux:Script to use find to search contents of files</title>
		<link>http://www.etechtips.com/2011/11/03/linuxscript-to-find-in-contents-of-files/</link>
		<comments>http://www.etechtips.com/2011/11/03/linuxscript-to-find-in-contents-of-files/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 19:39:43 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[OS]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=501</guid>
		<description><![CDATA[I use find and grep quite a bit. I found that I was regularly searching for strings in file and was typing commands like: find . -exec grep "somestring" {} \; --print This find command will search from the current directory and for each file will run grep on it and if it finds a [...]]]></description>
			<content:encoded><![CDATA[<p>I use find and grep quite a bit. I found that I was regularly searching for strings in file and was typing commands like:</p>
<pre>
find . -exec grep "somestring" {} \; --print
</pre>
<p>This find command will search from the current directory and for each file will run grep on it and if it finds a matching string, it will print the string and then the filename.  So I took this and moved it into a script.</p>
<pre>
#!/bin/sh

if [ $# -lt 1 ]; then
  echo 1>&#038;2 Usage: $0 "<search term>"
  exit 127
fi
icase=
search=

while [ $# -ge 1 ]; do
  case $1 in
     -i) icase=$1;;
      *) search=$1 ;;
  esac
  shift
done

find . -type f -print0 | xargs -0 grep $icase $search
</pre>
<p>This allows me to run a command such as</p>
<pre>
efind -i Super
</pre>
<p>This command will search all files starting from the current directory and recurse directories and the &#8220;-i&#8221; will tell it to ignore case.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2011/11/03/linuxscript-to-find-in-contents-of-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl: Open and read file line by line</title>
		<link>http://www.etechtips.com/2011/09/28/perl-open-and-read-file-line-by-line/</link>
		<comments>http://www.etechtips.com/2011/09/28/perl-open-and-read-file-line-by-line/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 16:29:42 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=459</guid>
		<description><![CDATA[I regularly have to process Comma Seperated Value or CSV files, usually for user lists. So the following perl code is a framework for processing a file line by line. #!/usr/bin/perl use strict; my $line; open(FILEIN, "filename.txt"); while(&#60;FILEIN&#62;) { chomp(); $line = $_; # Process $line for data # Example seperating line by commas: my [...]]]></description>
			<content:encoded><![CDATA[<p>I regularly have to process Comma Seperated Value or CSV files, usually for user lists.  So the following perl code is a framework for processing a file line by line.</p>
<pre>
#!/usr/bin/perl
use strict;
my $line;

open(FILEIN, "filename.txt");

while(&lt;FILEIN&gt;)
{
  chomp();
  $line = $_;
  # Process $line for data
  # Example seperating line by commas:
  my ($username,$firstname,$lastname,$email) = split(/,/,$line);
}

close(FILEIN);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2011/09/28/perl-open-and-read-file-line-by-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl: Hashes</title>
		<link>http://www.etechtips.com/2011/09/07/perl-hashes/</link>
		<comments>http://www.etechtips.com/2011/09/07/perl-hashes/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 13:00:16 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=295</guid>
		<description><![CDATA[Here are some basics about Perl hashes that can be helpful to be used when an you need to associate a label with some value such as for names and telephone numbers or account numbers with amounts owed. Create a hash variable. my %hashvar; Create a hash reference my $refhash = {}; Add a value [...]]]></description>
			<content:encoded><![CDATA[<p>Here are some basics about Perl hashes that can be helpful to be used when an you need to associate a label with some value such as for names and telephone numbers or account numbers with amounts owed.</p>
<p>Create a hash variable.</p>
<pre>
my %hashvar;
</pre>
<p>Create a hash reference </p>
<pre>
my $refhash = {};
</pre>
<p>Add a value to a hash variable.</p>
<pre>
$hashvar{'somekey'} = 'someval';
</pre>
<p>Add a value to a hash reference.</p>
<pre>
$hashref->{'somekey'} = 'someotherval';
</pre>
<p>Access a value in a hash.</p>
<pre>
print $hashvar{'somekey'};
</pre>
<p>Access a value in a hash reference.</p>
<pre>
print $hashref->{'somekey}';
</pre>
<p>Access all Keys in a hash variable.<br />
foreach my $k (keys(%hashvar))<br />
{<br />
   print $hashvar{$k};<br />
}
</pre>
<p>Access all Keys in a hash reference.</p>
<pre>
foreach my $k (keys(%$hashref))
{
  print $k . "\n";
}
</pre>
<p>Access all Keys and values in a hash.</p>
<pre>
while (my ($key,$value) = each %hashvar)
{
  print "Key: " . $key . " has value:" . $value . "\n";
}
</pre>
<p>Access all Keys and values in a hash.</p>
<pre>
while (my ($key,$value) = each %$refhash)
{
  print "Key: " . $key . " has value:" . $value . "\n";
}
</pre>
<p>Assigning multiple values to a hash</p>
<pre>
%hasvar = (
  'test2' => 'myval',
  'test3' => 'myval3'
);
</pre>
<p>Note: This will clear the existing hash.</p>
<p>Delete a value from a hash</p>
<pre>
delete $hashvar{'key1'};
</pre>
<p>Delete a value from a hash reference.</p>
<pre>
delete $hashref->{'key1'};
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2011/09/07/perl-hashes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl: Arrays</title>
		<link>http://www.etechtips.com/2011/09/03/perl-arrays/</link>
		<comments>http://www.etechtips.com/2011/09/03/perl-arrays/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 13:20:55 +0000</pubDate>
		<dc:creator>ecdown</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.etechtips.com/?p=437</guid>
		<description><![CDATA[I use perl arrays in some of my programs, and just started to look at the regular things that I do with these arrays and thought it would be useful to keep a list of frequently used items. Create a variable specified as an array. my @vararray Add an item to the beginning of an [...]]]></description>
			<content:encoded><![CDATA[<p>I use perl arrays in some of my programs, and just started to look at the regular things that I do with these arrays and thought it would be useful to keep a list of frequently used items.</p>
<p>Create a variable specified as an array.</p>
<pre>
my @vararray
</pre>
<p>Add an item to the beginning of an array</p>
<pre>
unshift (@vararray, "File2");
</pre>
<p>Add an item to the end of an array</p>
<pre>
push (@vararray, "File1");
</pre>
<p>Remove an item from the beginning of an array</p>
<pre>
my $var = shift(@array);
</pre>
<p>Remove an item from the end of an array</p>
<pre>
pop(@array)
</pre>
<p>Clear an array </p>
<pre>
@vararray = ();
</pre>
<p>Address an item in an array</p>
<pre>
print $vararray[0] ;
</pre>
<p>Get the size of an array</p>
<pre>
my $arraysize = @array;
</pre>
<p>Remove an item based on the index</p>
<pre>
delete @vararray[1];
</pre>
<p>Iterate over an array</p>
<pre>
foreach my $var (@vararay)
{
  print "$var \n";
}

# or
my $size = @vararray;
my $i;
for ($i = 0 ; $i < @size; $i++)
{
  print "$vararray[$i]\n";
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.etechtips.com/2011/09/03/perl-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

