How to write a script in ubuntu

How to install and use docker on ubuntu Scripting can be very fun and rewarding, as they allow you to automate large jobs or just simplify something that you find yourself doing over and over. This is a great habit to get into.

how to write a script in ubuntu

A script is a very simple concept. A script is just a text file, that contains commands for your shell to execute one by one. If not, all you really need to know is that nano is a good text editor to begin with and is simple to use.

Setting Permissions

Basic usage of nano homework expectations of classical schools really easy: all you need to do is give the nano command a path and filename.

Save the file and exit the editor. In order to run this file as a script, we need to mark it as executable:. Normally, lines starting with a hash symbol are ignored by the interpreter. The one on the first line is an exception to this. Basically, it just tells the shell which interpreter to use to run the commands inside the script. There are other interpreters we could be using, such as! The lines that followed were how to write a script in ubuntu print statements. The concept of scripting becomes more valuable when you start to think of things you do on a regular basis that you can how to write a script in ubuntu automate.

Again, if you are going to do a job more than once, script it.

Creating folders

This time, the script will actually how to write a script in ubuntu somewhat useful:. But from the preceding script, you can probably see how scripting can be useful in condensing the amount of work you do. This script could be an advanced web server install script, that you could simply copy to a new server and then run. The first ot after the hash bang is a comment, letting us know what the script does:.

Help Others, Please Share

Comments are ignored by the interpreter but are useful in letting us know what a block of code is doing. Bash, like any scripting language, supports branching and the if statement is one way of doing that.

We can change this to -d to check for the existence of a directory instead.

how to write a script in ubuntu

If we wanted to check if something is present, we would omit the exclamation mark. In rwite case, inside the brackets we are just executing a shell command, and then the result is checked. The commands sandwiched inside the if statement are simply installing packages.

Finally, we close out our if statement with the word if backwards fi. If you forgot to do this, the script will fail. In regard to the concept of if statements, we ih compare values as well.

Hello World script

Consider how to write a script in ubuntu following example:. The if statement checks to see if the variable was equal to 1. Alternatively, we can use -ne not equal-gt greater than-ge greater than or equal to-lt less thanand so on. At this writw, I recommend you take a break from reading to further practice scripting practice is key to committing concepts to memory.

how to write a script in ubuntu

Try the following challenges:.]