Archive for April, 2012

Changing a service’s startup policy via CLI

April 16, 2012 Leave a comment

I recently needed to automate an installation of a service but ran into a problem that it was starting automatically by default (which we did not want).

So I found a simple CLI to change this behavior –

reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\my-service-name /v Start /t REG_DWORD /d 3 /f

What it does is:

  1. Go to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\my-service-name entry in the registry
  2. Sets the value of Start (/v Start) to be the 32bit value (/t REG_DWORD) of 3 (/d 3)
  3. It overrides the current value if exists (/f)
All valid values are:
  • 2 = Automatic
  • 3 = Manual
  • 4 = Disabled
I have to give credit to this Microsoft article:

Easy pasting of strings into Java code using Eclipse

April 8, 2012 Leave a comment

Ever tried to copy a PATH to your java code and end up needing to go into it and replacing all ‘\’ with ‘\\’ ?

Well – a coworker showed me a cool feature in Eclipse that automatically escapes text when pasting it , just go to:
Window –> Preferences –> Java –> Editor –> Typing and check the ‘Escape text when pasting into a string literal’ option. (See below image)
Now whenever you paste text into a string literal (anything that starts with “ and ends with a “) – Eclipse will automatically escape it for you.

This also works for ‘\n’ and any other escape character.

Example of what it does:
Lets say you want to print this text –

Hello everyone!
 this is a "sample" of pasting a '"' and a '\' in a string

In the code it will look like:

String text = "Hello everyone!\n" + "this is a \"sample\" of pasting a '\"' and a '\\'  in a string"

Note: This was written for Eclipse Indigo (3.7), future/past versions may differ.

Categories: Eclipse, Tools Tags: , , ,

Manipulating the values of environment variables in windows batch files

April 3, 2012 Leave a comment

Lets say you have an environment variable that contains a certain value and you want to change it in some manner.

set AAA=abc_123
set BBB=%AAA:~0,3%%AAA:~4,3%
echo %BBB%

The output will be abc123
The syntax is: %var:~<offset>,<count>%
echo %AAA:~0,1% – will print “a” – Start at offset 0 and count 1 character.
echo %AAA:~1,2% – will print “bc” – Start at offset 1 and count 2 characters.
echo %AAA:~3,3% – will print “_12” – Start at offset 3 and count 3 characters.
echo %AAA:~1,-2% – will print “bc_1” – Start at offset 1 and count 2 characters from the end.

Another option is

set AAA=abc_123
set BBB=%AAA:abc=ABC%
echo %BBB%

The output will be ABC_123
The syntax is: %var:<string to find>=<alternate value>%
echo %AAA:a=A% – will print “Abc_123”.
echo %AAA:_=-% – will print “abc-123”

There are more options – just run help set on the command line to find them out.

Calling a shell script from another shell script in the same directory

April 3, 2012 Leave a comment

Often when creating a shell script you need to invoke another shell script located in the same directory.

Since your script may be invoked from any place on the file-system, the reference to the other script must be absolute – not relative.

Here is how to do it:

SCRIPT_DIR=`dirname $0`
. ${SCRIPT_DIR}/other_script.ksh

The call to dirname $0 returns the path to the currently running script ($0). The path is relative to the present working directory (PWD).

Categories: Shell Scripting, UNIX
%d bloggers like this: