How to use bash shell

last updated in Categories , ,

Q. How do I use bash shell? What command I need to type use bash shell?

A. GNU Bourne-Again Shell is default shell on most Linux systems (distributions). So when you open terminal from X Windows (KDE/Gnome) you directly opens a bash shell. Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.

To find out your shell version type:
$ echo $SHELL
Output:

/bin/csh

If you are using different shell, just type bash at command prompt and load bash shell:
$ bash

Shell script to get the time difference

last updated in Categories , , , ,

Q. I am able to write PHP or Perl script where I can find out time difference between script executions. Now I have .shtml file that is nothing but a shell script outputting some data to browser. What I want is time difference or time it took to execute a script. How do I write a shell script?

A. Your Logic should be as follows:

* Get start time and store to a variable START

* Execute a shell script

* Grab output and send to web browser

* Get time again and store to a variable END

* Calculate difference using expression END – START

Shell script o get the time difference

Here is small script that does the same thing (please note that script teated on GNU/Linux and with GNU date command only):
$ vi timediff.bash
Append text as follows:

#!/bin/bash
START=$(date +%s)
# do something

# start your script work here
ls -R /etc > /tmp/x
rm -f /tmp/x
# your logic ends here

END=$(date +%s)
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

Save and execute the script as follows:
$ chmod +x timediff.bash
Execute the script:
$ ./timediff.bash
Output:

It took 4 seconds