| BradTrupp.com - Little slices of my life and my projects | BradTrupp.com -- Tags -- Code |
| ||
BradTrupp.com -- Tags -- Code -- Bash - An Introduction to Scripting (Bash)
Bash - An Introduction to Scripting (Bash)
Bash is a Unix shell program and is the default shell for Ubuntu, as well as many Linux systems and Mac OS X. A Unix shell, which is commonly called "the command line", provides the traditional user interface for the operating system. You use it by entering commands for the shell to execute, but can also write and execute complex shell programs to do most anything. In Microsoft Windows, this would be equivalent to command.com (or cmd.exe) for the command prompt. A Basic Bash Script It is best to start your Bash script with the "shebang" line -- although it is somewhat optional being the default shell anyway.
Note: The "shebang" (also called a hashbang, hashpling, or pound bang) refers to a pair of characters "#!" that, when used as the first two characters on the first line of a script, causes Unix-like operating systems to execute that script using the interpreter specified by the rest of that line. It is also good practice to end your script with the exit command.
Here is the standard over-used "Hello World" example you see in so many programming languages.
Enter these 3 lines into a file using the text editor of your choice and save it as "helloworld". Bash Comments Comment lines in Bash scripts are similar to other programming languages where a special character, in this case "#" (the hash or pound sign), is used to delimit the start of a comment line.
Make it Executable Until you mark your script as executable, it is nothing more than a text file. The easiest way is to use the chmod command to change the file permissions. Open a terminal session, navigate to the directory that you saved "helloworld" into and enter the command:
Running a Bash Script Once again, open a terminal session, navigate to the directory that you saved "helloworld". If you enter the command
you will likely get an error like "command not found". This is because the default search path for commands does not include the current directory -- so the shell can not find your program. Now enter the command
and your script should run and display the expected "Hello World!" output. The "./" means look in the current directory. You can also fully qualify the script (assuming the script is in freddie's desktop)
and again the script should run. What's Next? Now we are ready to write some useful scripts, but first here are some related web sites.
Tags: Code Share: Del.icio.us | Digg | Facebook | Google Bookmarks | Reddit | Technorati | Twitter | Windows Live | Yahoo! My Web
|
|