Jump to content

Bash programming: Difference between revisions

From IdeaWazaWiki
wikademia>Erkan Yilmaz
+cats
wikademia>Wikademia
No edit summary
 
(13 intermediate revisions by 8 users not shown)
Line 1: Line 1:
==Bash Shell==
==The Bash Shell==
One of the most powerful tools when programming for Linux is the Bash Shell. When coding for the Bash Shell, you are writing scripts that will be executed by the shell. Bash Scripts are similar in nature to Batch scripts in Windows, but go much deeper into the operation of the OS.  
One of the most powerful tools when using any Unix is the Bash Shell. It is available as either an optional or default shell on most Unix variants. When writing scripts for the Bash Shell, you are creating a file that will be executed by the shell. Bash Scripts are similar in nature to Batch scripts in Microsoft Windows, but allow for much greater functionality.
{{ntnes}}
==What does it look like?==
==What does it look like?==
The Bash Shell syntax works very much like Batch, in that you use terminal commands. A sample script can be as simple as
A sample bash script can be as simple as this:
<source lang="bash">
<source lang="bash">
#!/bin/bash
#!/bin/bash
echo "hello world!"
echo "Hello World!"
</source>  
</source>
and can be as complex as script designed to ping a DNS server to retrieve an IP. A typical Bash Script is used to automate monotonous tasks like scanning folders and deleting automatically backed up files, or setting permissions to new files.  
Alternatively, it can be as complex as script designed to install and configure whole operating systems or troubleshoot problematic servers. A typical Bash Script is used to automate monotonous tasks like scanning directories and deleting automatically backed up files, or setting permissions to new files.
 
==How do you make a Bash Script?==
==How do you make a Bash Script?==
To begin the coding of a Bash Script, all one needs is a text editor (such as Gedit). Head the file off with: <source lang="bash">#!/bin/bash</source>. Then one would write the commands or arguments that need to be performed, such as: <source lang="bash">alias nmp="nmap -vv -T Insane -P0 -O"</source>
To begin the coding of a Bash Script, all one needs is a text editor (such as Gedit or vi/vim, emacs, nano, jed, etc). Start off the file with:
That example makes a script that eases the use of the Nmap Port Scanner. The finished product would look something like:
<source lang="bash">#!/bin/bash</source>
(This varies by Unix variant, and depends on the placement of the bash binary.  To find out what the path to the bash binary is, use this command: which bash ).
Then one would write the commands or arguments that need to be performed, such as:
<source lang="bash">alias nmp="nmap -vv -T Insane -P0 -O"</source>
That example makes a script that eases the use of the Nmap Port Scanner. It uses a function called aliasing, which allows complex commands to be simplified by using another sequence.  Next, you can call the alias to do work -- an alias is used like a constant from other programming languages, except it's used to execute programs.  So, for a finished script to scan your own computer, it would look something like this:
<source lang="bash">
<source lang="bash">
#!/bin/bash
#!/bin/bash
alias nmp="nmap -vv -T Insane -P0 -O"
alias nmp="nmap -vv -T Insane -P0 -O"
nmp 127.0.0.1
</source>
==Interactive Input==
Taking user input is invaluable when writing an application. Receiving and interpreting a user's input is a simple task in Bash scripting. All that is needed is to declare a variable to read, and use it for something. An easy way to explain this would be to ask for a user's name, and display it:
<source lang="bash">
#!/bin/bash
echo "Enter your name please:"
read NAME
echo "Hi there $NAME, pleased to meet you."
</source>
==Loops==
Loops are an essential part to many programming languages, and this holds true for Bash scripting as well. A simple way to incorporate a loop into a Bash script would look like this:
<source lang="bash">
#!/bin/bash
echo "Please enter a list of names to greet"
read NAMES
for i in $NAMES; do
  echo "Hi there $i, pleased to meet you."
done
</source>
</source>


==More complicated stuff==
[[Category:Linux]]
[[Category:Linux]]
[[Category:Programming]]
[[Category:Programming]]

Latest revision as of 08:02, 15 April 2009

The Bash Shell

One of the most powerful tools when using any Unix is the Bash Shell. It is available as either an optional or default shell on most Unix variants. When writing scripts for the Bash Shell, you are creating a file that will be executed by the shell. Bash Scripts are similar in nature to Batch scripts in Microsoft Windows, but allow for much greater functionality.

What does it look like?

A sample bash script can be as simple as this: <source lang="bash">

  1. !/bin/bash

echo "Hello World!" </source> Alternatively, it can be as complex as script designed to install and configure whole operating systems or troubleshoot problematic servers. A typical Bash Script is used to automate monotonous tasks like scanning directories and deleting automatically backed up files, or setting permissions to new files.

How do you make a Bash Script?

To begin the coding of a Bash Script, all one needs is a text editor (such as Gedit or vi/vim, emacs, nano, jed, etc). Start off the file with: <source lang="bash">#!/bin/bash</source> (This varies by Unix variant, and depends on the placement of the bash binary. To find out what the path to the bash binary is, use this command: which bash ). Then one would write the commands or arguments that need to be performed, such as: <source lang="bash">alias nmp="nmap -vv -T Insane -P0 -O"</source> That example makes a script that eases the use of the Nmap Port Scanner. It uses a function called aliasing, which allows complex commands to be simplified by using another sequence. Next, you can call the alias to do work -- an alias is used like a constant from other programming languages, except it's used to execute programs. So, for a finished script to scan your own computer, it would look something like this: <source lang="bash">

  1. !/bin/bash

alias nmp="nmap -vv -T Insane -P0 -O" nmp 127.0.0.1 </source>

Interactive Input

Taking user input is invaluable when writing an application. Receiving and interpreting a user's input is a simple task in Bash scripting. All that is needed is to declare a variable to read, and use it for something. An easy way to explain this would be to ask for a user's name, and display it: <source lang="bash">

  1. !/bin/bash

echo "Enter your name please:" read NAME echo "Hi there $NAME, pleased to meet you." </source>

Loops

Loops are an essential part to many programming languages, and this holds true for Bash scripting as well. A simple way to incorporate a loop into a Bash script would look like this: <source lang="bash">

  1. !/bin/bash

echo "Please enter a list of names to greet" read NAMES for i in $NAMES; do

 echo "Hi there $i, pleased to meet you."

done </source>

More complicated stuff