Saturday, March 19, 2011

How to run shell scripts like UNIX commands?

By including the script path in the environment variable PATH, you can run your shell script just like any UNIX command. i.e., irrespective of your current location you can run the script with just script name.

Example:
volcano@volcano-laptop:~$ PATH=$PATH:/home/volcano/shellscript
volcano@volcano-laptop:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:.:/home/volcano/shellscript
volcano@volcano-laptop:~$ hello.sh
Hello World!

Thursday, March 17, 2011

Why ./ is used to run shell scripts?

By default, UNIX or Linux based operating systems will search the executable of any command under the directories mentioned in the PATH variable to run the command.

If location of the shell script is not included in the PATH variable. Then we need to run the shell script by explicitly mentioning the directory name. This can be achieved in two ways.


a) Explicitly providing the absolute path(directory name from /) of the script
eg:
volcano@volcano-laptop:~/shellscript$ /home/volcano/shellscript/hello.sh
Hello World!

b) Explicitly providing the relative path (current directory) of the script
eg:
volcano@volcano-laptop:~/shellscript$ ./hello.sh
Hello World!

Meaning of ./ ( dot slash):
. -> Always refers to current directory
/ -> directory path separator

If you are not providing the directory separator means it will look like hidden file execution(.hello.sh). Then, operating system will try to run this as hidden file and then it will fail.So slash (/) is must after the . (dot) command to run the script.

Tuesday, March 15, 2011

Multiple ways to execute any shell script

Different ways of executing shell script mainly falls under two categories.
1. Executing the shell script in a new shell
2. Executing the shell script in the current shell

1.How to execute the shell script in a new shell.
This type can be accomplished in two ways. But any changes made to the shell or its environment will not be retained after the script execution.

a) Script set with execute permission and then executed in a new shell
As a general standard UNIX practice, we should set execute permission for any script before proceeding with execution.

Example:
volcano@volcano-laptop:~/shellscript$ chmod u+x hello.sh
volcano@volcano-laptop:~/shellscript$ ./hello.sh
Hello World!

b) Execution of script without setting execute permission for the script
We can achieve it by passing the script name as an argument to the shell. In the below example, if you keenly observe the permission of the owner of the file was not set.

Example:
volcano@volcano-laptop:~/shellscript$ ls -l hello.sh
-rw-r--r-- 1 volcano volcano 56 2011-03-13 00:41 hello.sh
volcano@volcano-laptop:~/shellscript$ bash hello.sh
Hello World!

 2. Executing the shell script in the current shell
Bash supports . (dot) & source command to make this possible. But any changes made to the shell or its environment will be in effect after the script execution.


Example:
volcano@volcano-laptop:~/shellscript$ . ./hello.sh
Hello World!
volcano@volcano-laptop:~/shellscript$ source ./hello.sh
Hello World!

Note: For running the script in the current shell also there is no need for to set the file with execute permission.

Why always ./ is used in front of the script for execution in all our examples will be detailed in the next post.

Monday, March 14, 2011

Hello World shell script dissected

1. Specific shell invocation
#!/bin/bash - It is called hash-bang or shebang statement. Presence of this statement in the beginning of any shell script is to invoke the specific shell to execute the script

/bin/bash  : Absolute path of the bash shell

Note 1: Shebang statement should be precisely in the first line. If it is given in other than first line, it will be considered as comment
Note 2: Shebang statement is not needed, if your script will execute in any shell

2. Comment for easy understanding of script
#Script to demonstrate - It is called comment. Anything comes after hash(#) will be considered as comment by shell and hence it will not be executed.

Note 1: #! will be executed by shell
Note 2: Shell supports single line comment only.`If you want to comment multiple line means you need to comment each line with hash(#) at the beginning of line.


3. Unix command or Shell programming statements
echo "Hello World!" - It is just a print command.  By default echo command will print its arguments to standard output

4. Script naming convention
hello.sh - It is just a script file name. sh extension is added to the script file purely for our convenience. No matter whatever extension we will give, UNIX/Linux just ignores it.

5. Making the script executable

chmod u+x hello.sh  - By using chmod command, we are giving execute permission to the owner of the file. Because without execute permission, we cannot execute the script directly.

6. Starting the script execution
./hello.sh  - This is invoking the script for execution.

Note: Different type of execution will be explained in the next post.

Sunday, March 13, 2011

Hello World script to demonstrate shell script creation and execution

In any programming language, first program will be classic "Hello World!" program. So our first script will be classic "Hello World!"

Prerequisite:
Any shell. Preferably bash
Any editor which is convenient to you.Preferably vi. But there is no need of editor to write simple shell script.

STEP 1: Creation of script file with name as hello.sh
volcano@volcano-laptop:~/shellscript$ cat > hello.sh

#!/bin/bash
#Script to demonstrate
echo "Hello World!"

STEP 2: Setting up execute permission to the script
volcano@volcano-laptop:~/shellscript$ chmod u+x hello.sh 

STEP 3: Execution of script in the command line
volcano@volcano-laptop:~/shellscript$ ./hello.sh 
Hello World!

Friday, March 11, 2011

How to switch between Shells?

Just you need to enter the available shell name in the command prompt to move from one shell to another shell.

Example:
 volcano@volcano-laptop:~$ echo $0
bash
volcano@volcano-laptop:~$ ksh
$ echo $0
ksh
$ sh
$ echo $0
sh
$ exit
$ echo $0
ksh
$ exit
volcano@volcano-laptop:~$ echo $0
bash
volcano@volcano-laptop:~$ csh
%

Where is your shell?

Any shell or Unix/Linux commands can be located using "which" command. If the requested command or shell is available means it will return with absolute path.

Example:
volcano@volcano-laptop:~/shellscript$ which bash
/bin/bash
volcano@volcano-laptop:~/shellscript$ which ksh
/usr/bin/ksh
volcano@volcano-laptop:~/shellscript$ which ls
/bin/ls

Which Shell you are right now?

It's a pretty simple question. But even people good at shell scripting can't answer correctly most of the time. The reason is people tend to use the value of the environment variable (SHELL) as current shell. But it is not correct, if you are not inside the login shell.

Note 1: echo $SHELL -> gives always login shell value
Note 2: echo $0 -> gives always the current shell
Note 3: cat /etc/shells -> lists valid login shells. It doesn't mean that all shells are available in your system.

Examples:
volcano@volcano-laptop:~/shellscript$ echo $SHELL
/bin/bash
volcano@volcano-laptop:~/shellscript$ echo $0
bash
volcano@volcano-laptop:~/shellscript$ ksh
$ echo $SHELL
/bin/bash
$ echo  $0
ksh
$ exit
volcano@volcano-laptop:~/shellscript$ echo $0
bash
volcano@volcano-laptop:~/shellscript$

volcano@volcano-laptop:~/shellscript$ cat /etc/shells
# /etc/shells: valid login shells
/bin/csh
/bin/sh
/usr/bin/es
/usr/bin/ksh
/bin/ksh
/usr/bin/rc
/usr/bin/tcsh
/bin/tcsh
/usr/bin/esh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/screen
/bin/ksh93
volcano@volcano-laptop:~/shellscript$ tcsh
The program 'tcsh' is currently not installed.  You can install it by typing:
sudo apt-get install tcsh