批处理脚本简介

时间:2011-08-05 06:14:03

标签: windows linux shell batch-file

这是我的shell脚本

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

我对批处理脚本完全不熟悉。如何编写等效的.bat文件?

3 个答案:

答案 0 :(得分:2)

您需要阅读以下两件事:

  • 如何提示用户输入:请参阅set命令,尤其是set /p。您可以在提示符下键入help set来获取有关此信息。
  • 如何测试字符串相等性:if命令有一些信息,在提示符下键入help if以获取一些详细信息。

当你有这些东西时,你应该能够复制bash脚本的行为。

答案 1 :(得分:1)

考虑在Windows上运行bash脚本而不是移植脚本。见bash-shell-for-windows。这可能更容易。

答案 2 :(得分:0)

看看如何获​​取输入: Batch File Input

修改:链接内容(不再可用)为:

    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.