From the course: Penetration Testing Essential Training
Refreshing your Bash skills - Linux Tutorial
From the course: Penetration Testing Essential Training
Refreshing your Bash skills
- [Instructor] One of the skills that can be quite useful when pen testing is Bash scripting. Bash is a program that comes built into Linux, and Bash scripts, also called Shell scripts, are text documents with a ".sh" extension. There's a number of courses on Bash scripting in our library, and if you want an in-depth understanding of the language, then I'd recommend you look in particular at the course by Scott Simpson. In the next few videos, I'll run through a quick refresher on the key points of Bash scripting. Let's have a look at the traditional Hello World script in Bash. I've got that prepared under the file "hello.sh". We start with the declaration that this is a Bash shell, and then we set a string variable and print it. We can now set the Shell file to enable execution with "chmod +x hello.sh". And then we can execute it. And we have our message displayed. We can use the convention dollar one, dollar two, et cetera to access the first second and so on argument on the command line. Let's have a look at the script "argue.sh". We'll put two arguments on the command line and then print them as one message. Again we'll "chmod +x argue.sh". And "argue.sh", we'll put "hello" as the first argument and "world" as the second argument. And we have two arguments and the message is "Hello World". Bash allows us to set variables and carry out operations on them. Let's have a look at the script "variables.sh". I'm setting "num1" to the value of the command line argument and setting "num2" as a constant. I'm checking whether the argument is greater than or equal to the constant and displaying a message accordingly. And I'm then displaying the sum of the two numbers. Variables in Bash are un-typed, so we have to use a double bracket syntax to achieve a numeric add. And we'll make sure the file is executable, "variables.sh", and we'll execute "variables.sh" and we'll give it the value 19. Bash displays the correct message comparing the two values, and then displays the sum. We can read information into the Bash script with the "read" command. So let's have a look at a script called "reader.sh". In this case we'll use an echo statement and then we'll read a name. And then we'll do the read and the message on the same line. "Chmod +x reader.sh". And then we'll execute "reader.sh". And we'll enter our name, enter my name. And the second message, we'll enter the name again. As we can see when reading with a prompt, our entry is on the same line.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.