Difference between revisions of "Shell scripting"

From Gender and Tech Resources

m
m (Hello World)
 
(17 intermediate revisions by 2 users not shown)
Line 2: Line 2:
  
 
== 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 <code>which</code> or <code>whereis</code> unix commands to find out where a specific shell is located:
# which bash
+
<pre># which bash
/bin/bash
+
/bin/bash</pre>
 
or:
 
or:
# whereis bash
+
<pre># whereis bash
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</pre>
  
 
== Hello World ==
 
== Hello World ==
 +
Shell scripts are text files that can be created with your favourite text editor.
 +
 +
$ geany helloworld.sh
 +
 +
In the file have this:
 +
 +
<pre>#!/bin/bash
 +
echo "Hello World"</pre>
 +
 +
The first line should always have that <code>#!/bin/bash</code>. The <code>#</code> starts a comment. The <code>#!</code> syntax acts as a comment for shells that do not understand this syntax. The <code>/bin/bash</code> tells any running shell, bash or other, which program to use to run the script, in this case bash. Not all systems have a bash shell, in which case you will find <code>/bin/sh</code> instead.
 +
 +
Save.
 +
 +
To run the file it must first be made executable:
 +
 +
$ chmod u+r helloworld.sh
 +
 +
Now you can run it with:
 +
 +
<pre>$ ./helloworld.sh
 +
Hello World</pre>
 +
 +
And that only works if you are in the directory that contains the script. By putting the directory containing executable scripts in your <code>PATH</code>, it can be identified by its full or relative path and you can run it from anywhere in your system.
 +
 +
Most modern Linux distributions encourage a practice in which each user has a specific directory for the programs and scripts he/she personally uses. This directory is called bin and is a subdirectory of your home directory.
 +
 +
$ mkdir ~/bin; cp helloworld.sh ~/bin/; PATH=$PATH:~/bin
 +
 +
Check that it is in your path with:
 +
 +
<pre>$ echo $PATH
 +
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/user/bin</pre>
 +
 +
Not you can just call your script from anywhere in your file system with:
 +
 +
$ helloworld.sh
 +
 +
That <code>PATH</code> is a socalled environment variable.
 +
 +
''Note: Do not put a dot <code>.</code> in the <code>PATH</code> environment variable to indicate commands can be run from the current directory. It can override well known commands if the name of your script is the same. A major security issue.
 +
 +
== Environment variables ==
  
 
== Print system related information ==
 
== Print system related information ==
 +
 +
== Environment ==
 +
 +
== Mental shortcuts ==
 +
 +
<pre>
 +
#!/bin/bash
 +
# set an infinite loop
 +
while :
 +
do
 +
clear
 +
        # display menu
 +
        echo "Server Name - $(hostname)"
 +
echo "-------------------------------"
 +
echo "    M A I N - M E N U"
 +
echo "-------------------------------"
 +
echo "1. Display date and time."
 +
echo "2. Display all processes owned by user (with CPU/MEM)."
 +
echo "3. Display network connections."
 +
echo "4. Exit"
 +
        # get input from the user
 +
read -p "Enter your choice [ 1 -4 ] " choice
 +
        # make decision using case..in..esac
 +
case $choice in
 +
1)
 +
echo "Today is $(date)"
 +
read -p "Press [Enter] to continue..." readEnterKey
 +
;;
 +
2)
 +
# show all running processes (with CPU/MEM) of user
 +
ps -u user u
 +
read -p "Press [Enter] to continue..." readEnterKey
 +
;;
 +
3)
 +
# show all tcp connections with no dns resolution (no reverse dns lookups are done)
 +
netstat -nat
 +
read -p "Press [Enter] to continue..." readEnterKey
 +
;;
 +
4)
 +
echo "Goodbye!"
 +
exit 0
 +
;;
 +
*)
 +
echo "Error: Out of my range ..."
 +
read -p "Press [Enter] to continue..." readEnterKey
 +
;;
 +
esac
 +
 +
done
 +
</pre>
  
 
== Backup and restore ==
 
== Backup and restore ==
Line 26: Line 118:
 
Another common pitfall is to assume that a range like <code>{1..200}</code> can be expressed with variables using <code>{$a..$b}</code>. It <strong>simply is not possible</strong>, because it's the very first step in doing expansions. A possible way to achieve this, is using the <code>eval</code> command, which basically evaluates a commandline twice:  
 
Another common pitfall is to assume that a range like <code>{1..200}</code> can be expressed with variables using <code>{$a..$b}</code>. It <strong>simply is not possible</strong>, because it's the very first step in doing expansions. A possible way to achieve this, is using the <code>eval</code> command, which basically evaluates a commandline twice:  
 
  eval echo {$a..$b}
 
  eval echo {$a..$b}
<nowiki> </nowiki>For instance, when embedded inside a for loop in a shell script:  
+
 
 +
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})
  
Line 48: Line 141:
 
If named arpsweep (or arpsweep.sh) call with (for example):
 
If named arpsweep (or arpsweep.sh) call with (for example):
 
  $ arpsweep 192.168 eth0
 
  $ arpsweep 192.168 eth0
 +
 +
== Data mining ==
 +
 +
== Object encryption ==
 +
 +
== Resources ==
 +
 +
=== Environment ===
 +
 +
== Related ==
 +
 +
* [[Installing linux]]
 +
* [[Linux applications]]
 +
* [[Linux security]]
 +
* [[Kinky linux command-line]]
 +
* [[Anonymising your traffic with linux]]
 +
* [[Clean up all teh things]]
 +
* [[Encrypting everything]]
 +
 +
== References ==
 +
 +
[[Category:How_To]]

Latest revision as of 22:09, 23 September 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

Shell scripts are text files that can be created with your favourite text editor.

$ geany helloworld.sh

In the file have this:

#!/bin/bash
echo "Hello World"

The first line should always have that #!/bin/bash. The # starts a comment. The #! syntax acts as a comment for shells that do not understand this syntax. The /bin/bash tells any running shell, bash or other, which program to use to run the script, in this case bash. Not all systems have a bash shell, in which case you will find /bin/sh instead.

Save.

To run the file it must first be made executable:

$ chmod u+r helloworld.sh

Now you can run it with:

$ ./helloworld.sh
Hello World

And that only works if you are in the directory that contains the script. By putting the directory containing executable scripts in your PATH, it can be identified by its full or relative path and you can run it from anywhere in your system.

Most modern Linux distributions encourage a practice in which each user has a specific directory for the programs and scripts he/she personally uses. This directory is called bin and is a subdirectory of your home directory.

$ mkdir ~/bin; cp helloworld.sh ~/bin/; PATH=$PATH:~/bin

Check that it is in your path with:

$ echo $PATH
 /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/user/bin

Not you can just call your script from anywhere in your file system with:

$ helloworld.sh

That PATH is a socalled environment variable.

Note: Do not put a dot . in the PATH environment variable to indicate commands can be run from the current directory. It can override well known commands if the name of your script is the same. A major security issue.

Environment variables

Print system related information

Environment

Mental shortcuts

#!/bin/bash
# set an infinite loop
while :
do
	clear
        # display menu
        echo "Server Name - $(hostname)"
	echo "-------------------------------"
	echo "     M A I N - M E N U"
	echo "-------------------------------"
	echo "1. Display date and time."
	echo "2. Display all processes owned by user (with CPU/MEM)."
	echo "3. Display network connections."
	echo "4. Exit"
        # get input from the user 
	read -p "Enter your choice [ 1 -4 ] " choice
        # make decision using case..in..esac 
	case $choice in
		1)
			echo "Today is $(date)"
			read -p "Press [Enter] to continue..." readEnterKey
			;;
		2) 
		# show all running processes (with CPU/MEM) of user
			ps -u user u	
			read -p "Press [Enter] to continue..." readEnterKey
			;;
		3) 
		# show all tcp connections with no dns resolution (no reverse dns lookups are done)
			netstat -nat
			read -p "Press [Enter] to continue..." readEnterKey
			;;
		4)
			echo "Goodbye!"
			exit 0
			;;
		*)
			echo "Error: Out of my range ..."	
			read -p "Press [Enter] to continue..." readEnterKey
			;;
	esac		
 
done

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

Data mining

Object encryption

Resources

Environment

Related

References