hari
hari

Reputation: 1377

Introduction to batch scripting

This is my shell script

echo "Name"
read name
if [ "$name" == "abcd" ]; then
   echo "correct name"
else
    echo "wrong name"
fi

echo "Password"
read password
if [ "$password" == "pwd" ]; then
    echo "Correct password"
else
    echo "Wrong password"
fi

echo "City"
read city
if [ "$city" == "bangalore" ]; then
    echo "correct city"
else
    echo "wrong city"
fi

I'm totally new to batch scripting.How do i write an equivalent .bat file?

Upvotes: 0

Views: 709

Answers (3)

Gerrat
Gerrat

Reputation: 29690

Have a look here for how to get input: Batch File Input

Edit: Link contents (no longer available) was:

    The following method will work on Windows 2000 and XP:

set INPUT=
set /P INPUT=Type input: %=%
echo Your input was: %INPUT%

If user hits ENTER without typing anything, the variable (in this case, %input%) keeps it value. So, the first line is to reset its value, so that if nothing is entered the variable will have no value at all, instead of some senseless value.

As you can see, that accepts blank inputs. You can make it to don't accept:

:input
set INPUT=
set /P INPUT=Type input: %=%
if "%INPUT%"=="" goto input
echo Your input was: %INPUT%

So, after getting user's input and saving it in a variable, you can use it as you will.

Upvotes: 1

Michał Šrajer
Michał Šrajer

Reputation: 31182

Consider running bash script on windows instead of porting script. See bash-shell-for-windows. It might be easier approach.

Upvotes: 1

Anders Lindahl
Anders Lindahl

Reputation: 42870

There are two things you need to read up on:

  • How to prompt for user input: see the set command and especially set /p. You can get information about this by typing help set at the prompt.
  • How to test string equality: The if command has some info on this, type help if at the prompt to get some details.

When you have these things in place, you should be able to replicate the behavior of your bash script.

Upvotes: 2

Related Questions