Welcome back, everyone!
In this lesson, I'm going to go over the use of the echo function and variables, as well as basic syntax.
You will need:
- A text editor - I personally recommend the free (and amazing) Notepad ++, but you can use any text editor you want, even Windows' built-in Notepad.
I will be assuming you are using Windows for this series, but you can use any Linux distro that you want, you will just need the Windows command prompt to run these scripts.
This is pretty simple, just launch your text editor. If you are using Notepad, then press Windows button and R to open the Run dialog, then type "notepad" into the search box.
If you are using Notepad ++, once it is opened and you have started a new document, click on the Languages tab and select Batch from the drop-down menu.
At the start of the file, type "@echo off".
What this does is that it gets rid of the user prompt in CMD (Command Prompt), changing it from this:
It seems like a small change, but it can be important to have.
Then, type in "title Null Byte Batch Script" or whatever you want the command prompt window to be titled.
This changes the text on the CMD screen from this:
Next comes the 'echo' command.
This command writes text to the screen. For example, if you type in "echo hello", the command prompt will return 'hello'.
One of the nice things about batch is it's simplicity. You rarely need brackets or quotes, you can just type in the commands without worrying about formatting.
So now, under the Title command, we will type "echo What is your name?"
This will be our question to the user. For the answer, we need a variable.
Variables are declared by enclosing the variable in percentage signs (%). Bear in mind that variables are case sensitive, so %variable% is different to %VARIABLE% is different to %Variable%.
To define the value of a variable ourselves, we use "set /a VARIABLENAME= VARIABLEVALUE"
However we want the user to set the variable (by answering the question), so we will replace /a with /p.
So now, enter "set /p useranswer=" into your text editor.
When we are setting a variable, we do not need to enclose it in %'s.
This will display a blank line on the command prompt, allowing the user to enter their name.
Now we want to display the user's name. To do this, we will type:
"echo Hello, %useranswer%!"
Unlike many other languages, you do not need to use the & symbol to insert a variable into a string.
Now, underneath that, type "pause >nullbyte".
The 'pause' command pauses the command prompt, waiting for the user to press a key. Without it, the command prompt would close directly after you entered your name.
The '>nullbyte' tells the 'pause' command to write it's contents to a file called 'nullbyte' You can name this file anything you want, and if the file doesn't exist, Windows will create it for you. Without the '>nullbyte', the command prompt would print "Press any key to continue...", but it looks kind of ugly, so we can tell it to write to a file instead :)
Now we need to save it.
Save your file by clicking Save. You will see this window.
If the 'Save as Type window says 'Batch File' or '.bat', then just enter the name of your batch file and click Save!
If, however, you get this screen:
Make sure that the file type is set to 'All types' if the 'Batch' option isn't available, and make sure that it ends in '.bat'.
Time to run it!
Just open Explorer and double-click on your batch file.
You should see a screen similar to this:
Type in your name and you should get something like this:
If you got that, congratulations! If you didn't, go back over my tutorial and make sure you followed each step precisely (and without the quotation marks) :)
Want to start making money as a white hat hacker? Jump-start your white-hat hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from ethical hacking professionals.