Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Tuesday, November 26, 2013

Bash Scripting

Notes to self from an excellent online resource: http://www.thegeekstuff.com/2010/03/introduction-to-bash-scripting/

Shell is a program, which interprets user commands. The commands are either directly entered by the user or read from a file called the shell script.
Shell is called as an interactive shell, when it reads the input from the user directly.

Shell is called as an non-interactive shell, when it reads commands from a file and executes it. In this case, shell reads each line of a script file from the top to the bottom, and execute each command as if it has been typed directly by the user.

 1)
Print the value of built in shell variable $-, to know whether the shell is an interactive or non-interactive.

# echo $-
himBH
Note: $- variable contains an “i” when the shell is interactive.

2)
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 as shown below.

# which bash
/bin/bash

# whereis bash
bash: /bin/bash /usr/share/man/man1/bash.1.gz
You can switch between the shells, by typing the shell name. For example, type csh to switch to C shell.

1. You can choose any name for the file. File name should not be same as any of the Unix built-in commands.

2. Script always starts with the two character ‘#!’ which is called as she-bang. This is to indicate that the file is a script, and should be executed using the interpreter (/bin/bash) specified by the rest of the first line in the file.

Bash-Startup files

when the bash is invoked as an interactive shell, it first reads and executes commands from /etc/profile. If /etc/profile doesn’t exist, it reads and executes the commands from ~/.bash_profile, ~/.bash_login and ~/.profile in the given order. The –noprofile option may be used when the shell is started to inhibit this behavior.

Typically your bash_profile executes ~/.bashrc. If you like, you can show a welcome message. This only runs when you first log in.When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout.

Example 2. Print a welcome message when you login

Type the following contents in your bash_profile file. If the file doesn’t exist, create a file with the below content.

$ cat ~/.bash_profile
hname=`hostname`
echo "Welcome on $hname."

login as: psqa
psqa@10.20.10.150's password:
Welcome to Ubuntu 12.04.3 LTS (GNU/Linux 3.2.0-54-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/

99 packages can be updated.
46 updates are security updates.

New release '12.10' available.
Run 'do-release-upgrade' to upgrade to it.

cat ~/.bash_profile
hname=`hostname`
echo "Welcome on $hname."
Welcome on psqa-Precision-WorkStation-T3500.

Example 3. Print system related information

When you login to an interactive shell, you can show the name of the kernel installed in the server, bash version, uptime and time in the server.


cat ~/.bash_profile
hname=`hostname`
echo "Welcome on $hname."

echo -e "Kernel Details: " `uname -smr`
echo -e "`bash --version`"
echo -ne "Uptime: "; uptime

 login as: psqa
psqa@10.20.10.150's password:
Welcome to Ubuntu 12.04.3 LTS (GNU/Linux 3.2.0-54-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/

99 packages can be updated.
46 updates are security updates.

New release '12.10' available.
Run 'do-release-upgrade' to upgrade to it.

cat ~/.bash_profile
hname=`hostname`
echo "Welcome on $hname."

echo -e "Kernel Details: " `uname -smr`
echo -e "`bash --version`"
echo -ne "Uptime: "; uptime
Welcome on psqa-Precision-WorkStation-T3500.
Kernel Details:  Linux 3.2.0-54-generic-pae i686
GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Uptime:  11:01:30 up 53 days, 19:25,  3 users,  load average: 0.00, 0.01, 0.05
psqa@psqa-Precision-WorkStation-T3500:~$