Introduction to Bash Script

14 May 2018

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell.This blog is the basic introduction to bash scripts.

Common Shell types

Below are the most common shell types that exists today.

  • bash = Bourne again shell
  • sh = shell
  • csh = C shell
  • tcsh = Tenex C shell
  • tclsh = Tcl shell
  • ksh = korn shell
  • ash = a shell
  • bsh = bourne shell
  • zsh = the Z shell

Basic UNIX /Linux directory structure

/ (system root)
 _______________________________|____________________________________
 |    |	    |	 |     |    |	    |	      |	   |	  |    |    |
bin/ boot/ dev/ etc/ home/ lib/ lost+found/ proc/ root/ sbin/ usr/ var/
 |    |     |    |     |    |       |         |    |      |    |    |
 |    |     |    |     |    |       |         |    |      |    |    |-> various
 |    |     |    |     |    |       |         |    |      |    |
 |    |     |    |     |    |       |         |    |      |    |-> Read only files
 |    |     |    |     |    |       |         |    |      |
 |    |     |    |     |    |       |         |    |      |-> Superuser Binaries
 |    |     |    |     |    |       |         |    |
 |    |     |    |     |    |       |         |    |-> User roots home dir
 |    |     |    |     |    |       |         |
 |    |     |    |     |    |       |         |-> system info etc.
 |    |     |    |     |    |       |
 |    |     |    |     |    |       |-> Orphan files (look here after crashes)
 |    |     |    |     |    |
 |    |     |    |     |    |-> System / programming libraries
 |    |     |    |     |
 |    |     |    |     |-> Contains the user accounts home directories
 |    |     |    |
 |    |     |    |-> System configuration files.
 |    |     |
 |    |     |-> Devices
 |    |
 |    |-> The kernel and kernel maps.
 |
 |-> Executeble files. (binaries)


The /usr directory contains a whole lot of things, mainly user accessible things

/usr/local/bin/ and /usr/bin/ It contains binaries

/usr/lib/ and /usr/local/lib/ It contains libraries

/usr/src/linux/ It contains the kernel source related files

Same or similar structure is followed on any operating system that uses directories some of the directory names in Linux/UNIX are UNIX specific.

Exit code in Linux

Exit codes are a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process.

To check the exit code we can use the $? special variable in bash.

0   /* successful termination */
64  /* base value for error messages */
64  /* command line usage error */
65  /* data format error */
66  /* cannot open input */
67  /* addressee unknown */
68  /* host name unknown */
69  /* service unavailable */
70  /* internal software error */
71  /* system error (e.g., can't fork) */
72  /* critical OS file missing */
73  /* can't create (user) output file */
74  /* input/output error */
75  /* temp failure; user is invited to retry */
76  /* remote error in protocol */
77  /* permission denied */
78  /* configuration error */

Reference

Exit Code Linux

Share: Twitter Facebook Google+ LinkedIn
comments powered by Disqus