Solved

Windows batch script  from unix bash

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


LOCKFILE=/tmp/$LOCKFILE
case "$1" in
start)
    touch $LOCKFILE
    poweron.sh;;
    exit 0;
stop)
    rm $LOCKFILE  
    poweroff.sh;;
    exit 0;
    ;;
monitor)
    if [ -a $LOCKFILE ]
    then
        exit 110
    else
        exit 100
    fi ;;
*)
    echo "Usage: $0 {start|stop|monitor} lock_file
    exit 1
    ;;
esac
0
Comment
Question by:mokkan
  • 3
  • 2
6 Comments
 
LVL 76

Expert Comment

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

What does poweron.sh and poweroff.sh do?
0
 

Author Comment

by:mokkan
Comment Utility
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\\filename.id";

if (-f "$LockFile") {
      exit(500);
} else {
      exit(600);
}
0
 

Author Comment

by:mokkan
Comment Utility
any help
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 42

Expert Comment

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

A rough untested conversion might be:
$LOCKFILE=$args[1]
switch ($args[0]) {
    "start" {
        set-content -Path ($LOCKFILE) -Value ($null)
        ./poweron.ps1
    }
    "stop" {
        Remove-Item $LOCKFILE   
        ./poweroff.ps1
    }
    "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.
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
>>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:
http://support.microsoft.com/kb/65994

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

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

Author Closing Comment

by:mokkan
Comment Utility
Thank  you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now