Mailinglist Archive: opensuse (2271 mails)

< Previous Next >
Re: [SLE] Bat files in Linux
  • From: Nick LeRoy <nleroy@xxxxxxxxxxx>
  • Date: Thu, 29 Apr 2004 09:07:29 -0500
  • Message-id: <200404290907.29667.nleroy@xxxxxxxxxxx>
On Thu April 29 2004 8:51 am, Steve Reynolds wrote:
> I have a few commands that I would like to run at the command prompt.
>
> In Windows I do this with a bat file that looks like the sample below. How
> do I do a similar thing with Linux?
>
> For those not familiar with windows bat files the snippet below presents
> the user with a menu at the command prompt.
>
> In windows I have a shortcut to a text file called backup.bat, when I
> double click that shortcut I launch a DOS window with the menu below
> already loaded for the user. I would like to do a similar thing with Linux.

Linux has much more powerful scripting languages. A simple replacement would
be writing a "shell" script; "man bash" for more details.

You may be better served, however, with a something like perl, which is an
extremely powerful scripting language.

In perl, your script would probably look something like this (quick hack, not
verified, etc):

#! /usr/bin/env perl
use strict;
use warnings;

$|=1;
while( 1 )
{
print " Backup Menu";
print " ===========";
.....
print " Q. Quit";

print "Enter your choice: ";
<>; chomp;
if ( /a/ )
{
# Do something here...
}
elsif ( /b/ )
{
# Handle b here...
}
}

Hope this helps. Google for perl; there are a lot of perl resources out
there.

-Nick

> <begin bat file snippet>
>
> @ECHO OFF
> CLS
>
> :LOOP
>
> ECHO Backup Menu
> ECHO ===========
> ECHO A. Flash Drive Backup J:
> ECHO B. Main Backup to CD
> ECHO C. Main Backup to F Drive
> REM ECHO D. Archive Backup
> ECHO E. Allofmp3 Backup to CD
> ECHO F. Flash Drive Backup I:
> REM ECHO G. REVERSE Flash Drive Backup From Linux:
> ECHO H. Photos Backup to iPod
> ECHO Q. Quit
>
> :: SET /P prompts for input and sets the variable
> :: to whatever the user types
>
> SET Choice=
>
> SET /P Choice=Type the letter and press Enter:
> :: The syntax in the next line extracts the substring
> :: starting at 0 (the beginning) and 1 character long
>
> IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
> ECHO.
>
> :: /I makes the IF comparison case-insensitive
>
> IF /I '%Choice%'=='A' GOTO ItemA
> IF /I '%Choice%'=='B' GOTO ItemB
> IF /I '%Choice%'=='C' GOTO ItemC
> IF /I '%Choice%'=='D' GOTO ItemD
> IF /I '%Choice%'=='E' GOTO ItemE
> IF /I '%Choice%'=='F' GOTO ItemF
> IF /I '%Choice%'=='G' GOTO ItemG
> IF /I '%Choice%'=='H' GOTO ItemH
> IF /I '%Choice%'=='Q' GOTO End
> ECHO "%Choice%" is not valid. Please try again.
> ECHO.
> GOTO Loop
>
> :ItemA
>
> <snip>
>
> <end bat file snippet>

--
<<< Why, oh, why, didn't I take the blue pill? >>>
/`-_ Nicholas R. LeRoy The Condor Project
{ }/ http://www.cs.wisc.edu/~nleroy http://www.cs.wisc.edu/condor
\ / nleroy@xxxxxxxxxxx The University of Wisconsin
|_*_| 608-265-5761 Department of Computer Sciences

< Previous Next >
References