Windows batch script  from unix bash

Posted on 2014-02-25
Last Modified: 2014-08-11
I am not a windows guy and I need to convert the following script to windows batch.

case "$1" in
    touch $LOCKFILE;;
    exit 0;
    rm $LOCKFILE;;
    exit 0;
    if [ -a $LOCKFILE ]
        exit 110
        exit 100
    fi ;;
    echo "Usage: $0 {start|stop|monitor} lock_file
    exit 1
Question by:mokkan
  • 3
  • 2
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39888596
What you posted just looks like an init script for two other scripts.

What does and do?

Author Comment

ID: 39901117
I am trying to learn the batch syntax

Here is a smiple perl script. How do I do it in windows batch?

$LockFile = "C:\\scripts\\";

if (-f "$LockFile") {
} else {

Author Comment

ID: 39901552
any help
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39907792
I would recommend not even looking at batch files and going right for a powershell or vbs script.

A rough untested conversion might be:
switch ($args[0]) {
    "start" {
        set-content -Path ($LOCKFILE) -Value ($null)
    "stop" {
        Remove-Item $LOCKFILE   
    "monitor" {
        if (Test-Path $LOCKFILE ) {
            exit 110
        else {
            exit 100
    default {
        "Usage: $0 {start|stop|monitor} lock_file
        exit 1

Open in new window

What context does the script run?  It looks as slightwv says as being an init script.
LVL 76

Accepted Solution

slightwv (䄆 Netminder) earned 500 total points
ID: 39908002
>>Here is a smiple perl script. How do I do it in windows batch?

The -f just checks if a file exists and takes appropriate action.  In the case of the code you posted, it exits with a different return value.

To check if a file exists in a BAT file, it is almost as simple.

An example of this can be found on Microsoft's site at:

None of this will really help unless you answer the question I asked in the first post:
What does and do?

These seem to be the important pieces to the script you posted.

Author Closing Comment

ID: 40254640
Thank  you

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. Please see for the updated article. It is avail…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question