eTechtips

Tech Tips and Insights

  • Perl: CPAN installing modules

    Sometimes it is necessary to install other modules into the Perl environment. Luckily there is a great repository of modules known as CPAN (The Comprehensive Perl Archive Network). This lets you install modules to help with encryption, XML,YAML, and many other things. There are a few ways to install a package and here are the ones that I use the most.

    Using the CPAN shell:

    perl -MCPAN -e shell

    This will open a command prompt that will let you search for CPAN modules with:

    i /PACKAGENAME/

    or install a new module:

    install /DateTime/

    The nice thing about installing with CPAN is that it will look for the dependencies and suggest installing them before the module you requested.

    NOTE:This can take some time and your dependencies could have dependencies.

    The other method I use is to skip the CPAN prompt and just directly execute the install of the module:

    perl -MCPAN -e 'install DateTime'

    This is just the most basic of usage, and there is probably a lot more you can do, but I have not had to use much more than this.

    0 Comments
  • Gallery of Hello World!

    Here is a list of the Hello World Programs for different languages:

    C

    #include <stdio.h>
    int main()
    {
    printf("Hello World!");
    }

    C++

    #include <iostream.h>
    int main()
    {
    cout << "Hello World!" << endl;
    }

    C#

    public class HelloWorld
      public static void Main()
      {
        System.Console.WriteLine("Hello World!");
      }

    Java

    class HelloWorld {
    static void main(String[] args)
    {
    System.out.println("Hello World!");
    }

    SHELL

    echo "Hello World"

    Python 2

    print "Hello World!\n"

    Python 3

    print ("Hello World!")

    Ruby

    puts "Hello World!"

    Perl

    print "Hello World!n";

    PHP

      <?php      
        print "Hello World!";
      ?>

    Rust

    fn main() {
        println("Hello World!");
    }

    This is the simplest form of Hello World for most of these languages.

    0 Comments
  • Using tar over ssh

    Every once in awhile, I need to backup a large directory from another computer. This can be done by creating a zip or tar of the files you are looking to save and then copying those zips to the other computer. But what it you have a very large directory and not enough room to store the extra file. Here is a way to use tar over ssh:

    ssh tar zcvf – /wwwdata | ssh root@backupserver.mydomain.com “cat > /backup/wwwdata.tar.gz”

    0 Comments
  • Setting timestamp with Touch

    Did you know? You can Set file timestamp using touch command. One way to do this is:
    touch -c -t YYMMDDhhmm filename.txt

    0 Comments
  • Best Learning sites(in progress)

    Learnable
    www.learnable.com
    Brought to you by Sitepoint. They have created a site to host their on-line classes and a place for people to create their own as
    well.

    Udemy
    www.udemy.com
    Just checking this one out, I found some interesting courses, but many seem to be placeholders for classes to come later.

    LiveMind
    www.livemind.com
    Just read about this one and have not looked thorugh it very much yet.

    0 Comments
  • Vi: Remove Ctrl-M characters from a document

    I regularly have to move scripts between Windows and Linux systems. The line endings from
    Windows can make running my scripts on Linux fail. I have been on a few systems where the
    dos2unix command was not available, so how do I get rid of the Ctrl-M at the end of the lines?

    Command: vi -b <filename>

    The -b sets vi into binary mode. This displays the extra control characters. In vi, I remove the
    ctrl-Ms.

    In vi, I use the following key sequence in command mode:

     :%s/<ctrl-v><enter>//g 

    The ctrl-v tells vi to take the next character and display it as a ctrl char sequence. And hitting the
    enter key displays the ctrl-M. The :%s///g is a command to replace the characters in the first // and replace
    it with the chars in the second // and the g says to do it to every line.

    0 Comments
  • Retrieve drive letter on Windows

    The other day I had to write a script to change to a working directory and back to the original directory. The problem was they were on different drives on Windows. So I had to find a way to capture the drive letter
    and switch back to that drive. When using the following command,

    cd F:somedir
    

    you are not moved to the “F:” drive without an explicit “F:” command.

    The cd Command on windows will show the current directory location. Using some string manipulation on that and capturing it to a variable allows us to go back to that drive.

    REM Capture the current drive letter
    set ORIGDRIVE=%cd:~0,2%
    
    F:
    REM some other scripting things here
    
    REM Return to the original drive letter
    ORIGDRIVE
    

    The first line captures the current drive letter by specifying the first character with 0 and the ,2 states to pick up the next 2 characters which would hold the “C:” or whatever drive you started on.

    0 Comments
  • Utility: Find files files older than a certain amount of days

    I had to write a backup script the other day and used the find command to find the files that were older than a given amount of days.

    DAYS_TO_KEEP=7
    find /path/to/files* -type f -name *.bak -mtime $DAYS_TO_KEEP -exec rm {} ;
    

    The -name argument uses the escaped asterisk so command completion does not replace it with all the file names.

    The -mtime argument takes a number that is the number of days to look at.

    The -exec argument takes a command and the {} is replaced with the current file name and the ; is the end of the command.

    This is a small part of the power of the find command.

    0 Comments
  • Windows 7: Create Library

    Windows 7 has introduced the concept of a Libary. This is a folder that can contain other folders and is available in the Windows Explorer. So you may have decided that the 4 defaults folders aren’t enough:

    • Documents
    • Music
    • Pictures
    • Video

    So to create a new library:

    1. Select : Start menu->All Programs->Accessories->Windows Explorer
      or
      Press + e
      and click on the Libraries folder in the left hand column.
    2. Right click in an empty space on the folder view and select New->Library
    3. Then name your Library
    4. Right-click your new library and select properties.
    5. Select Folders to include in your library.

    Each library can contain multiple folders. Now everytime you open the explorer your library will be available to you and you do not have to dig into your file hierarchy to find your most used files.

    Suggested Applications:

    • Work folders
    • Project folder
    0 Comments
  • Windows: Grep for windows

    If you don’t have access to a Unix environment on your windows box, you can still search files with findstr. The findstr command will allow you to search files close to the same way as grep.

    findstr /S "search string"

    will recursively find your string in the current directory and subdirectories.

    There are more options that you can find if you run the following command:

    findstr /?

    will show you the help message.

    0 Comments