Difference between revisions of "Shell scripting"

From Gender and Tech Resources

(Created page with "== Shell scripting == Shell scripts are good for automating repetitive shell tasks. Bash and other shells include the “usual” constructs found in programming languages, su...")
 
m
Line 1: Line 1:
== Shell scripting ==
 
 
Shell scripts are good for automating repetitive shell tasks. Bash and other shells include the “usual” constructs found in programming languages, such as for loops, tests, if and case statements, but there is only one type of variable: strings.
 
Shell scripts are good for automating repetitive shell tasks. Bash and other shells include the “usual” constructs found in programming languages, such as for loops, tests, if and case statements, but there is only one type of variable: strings.
  
=== Shells ===
+
== Shells ==
 
Unix has variety of Shells. Bourne shell (sh), Bourne again shell (bash), C shell (csh), Korn shell (ksh), Tenex C shell (tcsh). Use the which or whereis unix commands to find out where a specific shell is located:
 
Unix has variety of Shells. Bourne shell (sh), Bourne again shell (bash), C shell (csh), Korn shell (ksh), Tenex C shell (tcsh). Use the which or whereis unix commands to find out where a specific shell is located:
 
  # which bash
 
  # which bash
Line 10: Line 9:
 
  bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
 
  bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
  
=== Hello World bash script ===
+
== Hello World ==
  
=== Print system related information ===
+
== Print system related information ==
  
=== Backup and restore using brace expansion ===
+
== Backup and restore ==
 +
 
 +
=== Preliminaries ===
  
 
The brace expansion is present in two basic forms, <strong>string lists</strong> and <strong>ranges</strong>. It can be switched on and off under runtime by using the <code>set</code> builtin and the option <code>-B</code> and <code>+B</code> or the long option <code>braceexpand</code>. If brace expansion is enabled, the stringlist in <code>SHELLOPTIONS</code> contains <code>braceexpand</code>.
 
The brace expansion is present in two basic forms, <strong>string lists</strong> and <strong>ranges</strong>. It can be switched on and off under runtime by using the <code>set</code> builtin and the option <code>-B</code> and <code>+B</code> or the long option <code>braceexpand</code>. If brace expansion is enabled, the stringlist in <code>SHELLOPTIONS</code> contains <code>braceexpand</code>.
Line 27: Line 28:
 
<nowiki> </nowiki>For instance, when embedded inside a for loop in a shell script:  
 
<nowiki> </nowiki>For instance, when embedded inside a for loop in a shell script:  
 
  for i in $(eval echo {$a..$b})
 
  for i in $(eval echo {$a..$b})
Backup script using brace expansion:
 
  
Restore script using brace expansion:
+
=== Backup script ===
 +
 
 +
=== Restore script ===
 +
 
 +
== Arp sweeps ==
 +
If you regularly do arp sweeps of your network, you can use arp-scan or develop your own from this basic script:
 +
#!/bin/bash
 +
PREFIX=$1
 +
INTERFACE=$2
 +
for SUBNET in {1..255}
 +
do
 +
    for HOST in {1..255}
 +
    do
 +
      echo "[*] IP : "$PREFIX"."$SUBNET"."$HOST
 +
      arping –c 3 –i $INTERFACE $PREFIX"."$SUBNET"."$HOST 2> /dev/null
 +
    done
 +
done
 +
If named arpsweep (or arpsweep.sh) call with (for example):
 +
$ arpsweep 192.168 eth0

Revision as of 09:19, 10 July 2015

Shell scripts are good for automating repetitive shell tasks. Bash and other shells include the “usual” constructs found in programming languages, such as for loops, tests, if and case statements, but there is only one type of variable: strings.

Shells

Unix has variety of Shells. Bourne shell (sh), Bourne again shell (bash), C shell (csh), Korn shell (ksh), Tenex C shell (tcsh). Use the which or whereis unix commands to find out where a specific shell is located:

# which bash
/bin/bash

or:

# whereis bash
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

Hello World

Print system related information

Backup and restore

Preliminaries

The brace expansion is present in two basic forms, string lists and ranges. It can be switched on and off under runtime by using the set builtin and the option -B and +B or the long option braceexpand. If brace expansion is enabled, the stringlist in SHELLOPTIONS contains braceexpand.

When doing:

$ echo {a,b}$PATH

the brace expansion does not expand the variable - this is done in a later step. Brace expansion just makes it being:

echo a$PATH b$PATH

Another common pitfall is to assume that a range like {1..200} can be expressed with variables using {$a..$b}. It simply is not possible, because it's the very first step in doing expansions. A possible way to achieve this, is using the eval command, which basically evaluates a commandline twice:

eval echo {$a..$b}

For instance, when embedded inside a for loop in a shell script:

for i in $(eval echo {$a..$b})

Backup script

Restore script

Arp sweeps

If you regularly do arp sweeps of your network, you can use arp-scan or develop your own from this basic script:

#!/bin/bash
PREFIX=$1
INTERFACE=$2
for SUBNET in {1..255}
do
   for HOST in {1..255}
   do
      echo "[*] IP : "$PREFIX"."$SUBNET"."$HOST
      arping –c 3 –i $INTERFACE $PREFIX"."$SUBNET"."$HOST 2> /dev/null
   done
done

If named arpsweep (or arpsweep.sh) call with (for example):

$ arpsweep 192.168 eth0