Thursday Jul 30, 2009

Using Cygwin to manage script Virtual Box tasks

The Virtual Box GUI doesn't do everything one needs and so I have been experimenting with using cygwin, a free to use bash shell library. Having installed CYGWIN the first thing to do is add the Virtual Box program folder to the PATH, in my case,

    export PATH=/cygdrive/c/Program\\ Files/Sun/xVM\\ VirtualBox:${PATH}

I want this placed in the .bashrc so its always invoked, and thus need to test if is already in the path. I use this code,

    type -p VBoxManage.exe > /dev/null
    case $? in
    0)   # Its already there
         : ;;
    1)   # Add Path
         export PATH=/cygdrive/c/Program\\ Files/Sun/xVM\\ VirtualBox:${PATH} ;;
    esac

Now I need to associate a script file type with an execution program. I have tried to use the distributed batch file and binary and neiter of these work, so I have copied cygwin.bat to bash.bat and amended it so that it reads as follows

    @echo off

    :: Dave Levy (c) 2009         Is this GPL?
    :: bash.bat                   Version 1.0

    :: Wrapper to run \*sh scripts from windows explorer/desktop

    c:\\ksh\\bin\\bash --login %\*

I have deleted the -i flag from the bash line, I have made the bash program call absolute, %\* is the DOS batch command file syntax equivilant to $\* i.e. all the command line arguments, so the command line invokes bash.exe in login mode to force the execution of .bash_profile and .bashrc and appends all the other command line parameters including the script file name. :: is a neater comment delimiter than REM. I have associated my batch file with the file type .ksh as the open method using explorer.

Much of CYGWIN is distributed under the GPL and I am unclear if the batch file is included, If so, its GPL since I created the file by copying it.

keywords: howto invoke cygwin scripts from the windows desktop, write a dos batch file to invoke shell

tags:

Monday Feb 16, 2009

A bitty week

Last week was a pretty bitty week, dominated by the need to complete some mandatory, examined web training. I have recently completed similar training in Sun's Unified Storage Products which I found useful. This one is not so focused on technology and while useful in that I learned a couple of things, I really wonder if it was a good use of my time.

Prior to getting stuck in, I made some progress on my web estate including Laconica, planet and glassfish. I got a copy of mingle running on one of my Linux images but made no progress on installing Glassfish. I have taken advice and been recommended to use the Sun installer, but I am being stubborn and want to see if I can use the Ubuntu package installer.

Mind you I got fed up with bash & sh and installed the Korn Shell. I was able to use the package manager which is cool. I was expecting that some Linux religous view, or over zealous conformance with the various licenes would prohibit its inclusion, but it works fine. Since I shall only be using it for scripts, I don't need to write a global .kshrc and install it where ever it would need to be. What broke the Camel's back? I felt I needed $(). I had been lectured by Chris Gerhard about using this syntax a while ago because it supports nesting and finally came across a case where it was needed, or at least, coding speed and my skills meant that I decided to move over to it. (It does mean that anything I write may be harder for other Linux users to adopt.) So I'll write in ksh, and port to sh.

I ended the week on Sunday at the Chichester Smith and Western, where the atmosphere, decor and music are fun and the portions enormous.

tags:

Wednesday Aug 09, 2006

Shell Scripting and PATHs

Everseen a PATH like this....

/opt/sybase/ASE-12.5/bin:/opt/sybase/ASE-12.5/install:/usr/local/bin:/opt/sybase/ASE-12.5/bin\\
:/opt/sybase/ASE-12.5/install:/usr/local/bin:/usr/bin:/bin

I have over the last couple of weeks been installing Users on UNIX systems and considering the issue of the PATH variable. The PATH above is caused by not checking if the directory is in the PATH, before adding it. This is particulalry a problem for those shells that have one file executed on a login and one file executed each time a new shell is invoked, such as ksh & bash. Also, for users created to own shared services (such as sybase in this case), people may enter the user from several entry points, including login, su \* su -. The problem is exacerbated when the environment definitions are written by a third party, in this case Sybase, but other packages have their own problems.

I have decided to undertake a test prior to amending the path. I'd like to write a function 'prepend2path, but came to the conclusion that the level of indirection required was to much, for me at least. I use a function contains

contains()
{
    #contains $PATH $directory
    /usr/bin/echo $1 | /usr/bin/grep $2 > /dev/null
    return $?
}

This allows me to test if a $PATH contains a given $directory

  if [ -d ${directory} ]
  then
      contains $PATH $directory
      if [ $? !=0 ]
      then
          PATH=${directory}:${PATH}
      fi
  fi

These statements should probably be placed in the shell read configuration file for the default login shell for the user, however they may be located elsewhere for good reason, such as in a script so that it can be run from wherever and the script becomes responsible for obtaining its own environment. The function can be placed in an external functions file and then invoked from wherever should this seem sensible.

This code model can be used for LD_LIBRARY_PATH and MANPATH. Both these variables may be unset so the contains function needs to OR'd with -z test. Also we can iterate the test.

  for directory in ${STEM}/install ${STEM}/bin
  do
    if [ -d ${directory} ]
    then
        contains $PATH $directory
        if [ $? !=0 ]
        then
          PATH=${directory}:${PATH}
        fi
    fi
  done

As I write this article, it becomes clear I should put the -d test inside the contains function.

contains()
{
    #contains $PATH $directory
    if [ -d $directory];then
        /usr/bin/echo $1 | /usr/bin/grep $2 > /dev/null
        return $?
    else
        return 99
    fi
}

This leaves the path assignment statement looking like this.

  for directory in ${STEM}/install ${STEM}/bin
  do
    contains $PATH $directory
    if [ $? !=0 ]
    then
      PATH=${directory}:${PATH}
    fi
  done

Of course there are those that say if you don't echo $PATH, you don't have the problem either!

tags:

Wednesday Aug 02, 2006

Determining UNIX service states

On both my Qube (Linux) and Laptop (Solaris), I have been installing shared services, such as Postgres, Sybase and snipsnap, and thus considering how to manage them. I have wanted the excuse to write an SMF(5) compliant script and so have been working on one.

One of the key methods required is the status method which I have after discussion with colleagues, decided has three states. These correspond to a Jackson Backtracking problem, since one state is an error state. This can only be defined as a not running, not stopped state. The diagram below shows the JSP structure diagram.

Jackson Backtracking Structure for Service Status

The reason it's a backtracking problem is that you can't tell if its an error state until after you have tested for the other two good states.

tags:

About

DaveLevy

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today