9 Jul 2013

Crunchbang Linux - Hello World!

There's a tradition that when you write your first program in any programming or scripting language you write a program to display the words "Hello World". Here's how you can make a shell script file in Crunchbang Linux (it will work for other Linux distros too). This is similar to creating a batch file in the world of Windows command line.


Instructions
Right click on the desktop, click Text Editor

Geany (the text editor) will appear on the screen - enter the following:

#!/bin/bash

echo "Hello World!"


You should have something like this:



Click File | Save

Save the file as "HelloWorld.sh" (the .sh file extension is to indicate it's a shell script file)
The default place for it to be saved is in your home folder.

Right click on the desktop

Click Terminal 

Type the following command at the $ prompt:
chmod a+x HelloWorld.sh      [Enter]

The chmod command changes the attributes of the HelloWorld.sh file to make it executable. For more details about this command, click http://en.wikipedia.org/wiki/Chmod

To run the file type:
~/HelloWorld.sh    [Enter]

The ~ means 'home'. If you save your .sh file in a sub-folder you'd need to type ~/sub-folder/HelloWorld.sh for example.

The following screen shot shows what you should see.

The echo command in the .sh file is just like the same command from Windows, it displays text on the screen. In our case you can see it has displayed the words "Hello World!":



Conclusion
I know this doesn't seem very exciting but it is useful, I promise! In the world of Windows it is possible to create a command file or 'batch' file. The idea being that you can run a number of commands one after another. This is useful for automating common tasks. Here in Linux what we've done here is the basics of creating a shell script (batch) file. Of course you could add more commands to do any number of different tasks. The first line of the script file should be !#/bin/bash but otherwise you can do as you wish, enter any number of commands. Of course they must be valid Bash shell scripting commands.


References
A forum post with more details
http://www.linuxquestions.org/questions/linux-newbie-8/how-to-create-a-batch-file-in-linux-608655/

Chmod
http://en.wikipedia.org/wiki/Chmod

Bash Shell Scripting
http://en.wikipedia.org/wiki/Bash_shell

Bash Shell Scripting Tutorial
http://arachnoid.com/linux/shell_programming.html

More Crunchbang Linux articles
http://mgxp.blogspot.ch/search/label/Crunchbang


No comments: