Windows Batch Echo and Control Constructs
@echo off
echo set msg=Hello World! > hello.bat
echo echo %%msg%% >> hello.bat
hello.bat
This would cause "set msg=Hello World!" to be echoed and redirected to the file hello.bat, "echo %msg%" is then echoed and redirected to the file hello.bat but this time appended to the end. Notice the double '%' in the program-listing so that the '%' character is echoed and not interpreted as being the opening of a variable name. The final line executes hello.bat causing it output "Hello World!".
3.2.2.2. Command Line Arguments
Command line arguments are treated as special variables within the batch script, the reason I am calling them variables is because they can be changed with the shift command. The command line arguments are enumerated in the following manner %0, %1, %2, %3, %4, %5, %6, %7, %8 and %9. %0 is special in that it corresponds to the name of the batch file itself. %1 is the first argument, %2 is the second argument and so on. To reference after the ninth argument you must use the shift command to shift the arguments 1 variable to the left so that $2 becomes $1, $1 becomes $0 and so on, $0 gets scrapped because it has nowhere to go, this can be useful to process all the arguments using a loop, using one variable to reference the first argument and shifting until you have exhausted the arguments list.
3.2.3. Control Contructs
The flow of control within batch scripts is essentially controlled via the if construct.
3.2.3.1. If
This construct takes the following three generic form, the parts enclosed within '[' and ']' are optional:
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
When a Windows command exits it exits with what is known as an error level, this indicates to anyone who wants to know the degree of success the command had in performing whatever task it was supposed to do, usually when a command executes without error it terminates with an exit status of zero. An exit status of some other value would indicate that some error had occurred, the details of which would be specific to the command. This is what the error-level if is for.
@echo off
if "%1"=="1" echo The first choice is nice
if "%1"=="2" echo The second choice is just as nice
if "%1"=="3" echo The third choice is excellent
if "%1"=="" echo I see you were wise enough not to choose, You Win!
What this example does is compare the first command line argument with the strings "1", "2" and "3" using == which compares two strings for equality, if any of them match it prints out the corresponding message. If no argument is provided it prints out the final case. If you want to include more than one command, enclose it within braces:
@echo off
if exist hello.bat (
echo Removing hello.bat...
del hello.bat
echo hello.bat removed!
)
@echo off
echo set msg=Hello World! > hello.bat
echo echo %%msg%% >> hello.bat
hello.bat
This would cause "set msg=Hello World!" to be echoed and redirected to the file hello.bat, "echo %msg%" is then echoed and redirected to the file hello.bat but this time appended to the end. Notice the double '%' in the program-listing so that the '%' character is echoed and not interpreted as being the opening of a variable name. The final line executes hello.bat causing it output "Hello World!".
3.2.2.2. Command Line Arguments
Command line arguments are treated as special variables within the batch script, the reason I am calling them variables is because they can be changed with the shift command. The command line arguments are enumerated in the following manner %0, %1, %2, %3, %4, %5, %6, %7, %8 and %9. %0 is special in that it corresponds to the name of the batch file itself. %1 is the first argument, %2 is the second argument and so on. To reference after the ninth argument you must use the shift command to shift the arguments 1 variable to the left so that $2 becomes $1, $1 becomes $0 and so on, $0 gets scrapped because it has nowhere to go, this can be useful to process all the arguments using a loop, using one variable to reference the first argument and shifting until you have exhausted the arguments list.
3.2.3. Control Contructs
The flow of control within batch scripts is essentially controlled via the if construct.
3.2.3.1. If
This construct takes the following three generic form, the parts enclosed within '[' and ']' are optional:
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
When a Windows command exits it exits with what is known as an error level, this indicates to anyone who wants to know the degree of success the command had in performing whatever task it was supposed to do, usually when a command executes without error it terminates with an exit status of zero. An exit status of some other value would indicate that some error had occurred, the details of which would be specific to the command. This is what the error-level if is for.
@echo off
if "%1"=="1" echo The first choice is nice
if "%1"=="2" echo The second choice is just as nice
if "%1"=="3" echo The third choice is excellent
if "%1"=="" echo I see you were wise enough not to choose, You Win!
What this example does is compare the first command line argument with the strings "1", "2" and "3" using == which compares two strings for equality, if any of them match it prints out the corresponding message. If no argument is provided it prints out the final case. If you want to include more than one command, enclose it within braces:
@echo off
if exist hello.bat (
echo Removing hello.bat...
del hello.bat
echo hello.bat removed!
)
No comments:
Post a Comment