• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Saturday, October 10, 2009

Posted by venu k
5 comments | 9:55 AM
Defining the Shell Type

To make a bash script crate a new file with a starting line like:
#!/bin/bash
It is important that the path to the bash is proper and
first two characters must be “#!” . The shell from which you
are starting the script will find this line and hand the whole
script over to bash. Without this line the script would be
interpreted by the same type of shell as the one, from which
it was started.But since the syntax is different for all shells,
it is necessary to define the shell with that line.
Some typical interpreters for shebang lines:

• #!/bin/bash — Execute using the Bourn-again shell
• #!/bin/sh — Execute using the Bourn shell (if available) or a
Bourne compatible shell such as Bourne-again shell
• #!/usr/bin/ksh --- Execute using korn shell

Note:
In computing, a shebang (also called a hashbang, hashpling,
pound bang, or crunchbang) refers to the characters "#!" when
they are the first two characters in a text file. In a Unix-like
operating system, the program loader takes the presence of these
two characters as an indication that the file is a script, and
tries to execute that script using the interpreter specified by
the rest of the first line in the file. For instance, shell
scripts for the Bourne shell start with the first line:
#!/bin/sh

Executing a shell script


Once a shell script is created, there are several ways to
execute it. However, before executing a shell script you must
assign proper permissions. Using chmod command you can change
file permissions.For example giving execute permission to the
file “temp”:
chmod +x temp
First create a sample shell script “temp”.

$cat > temp
#!/bin/bash
echo "My name is `whoami`"
echo "Users are `users`"
echo "OS name is `uname -s`"
echo "current shell is $SHELL"
ctrl+d


Method 1 to execute a shell script:


./scriptname

You must assign execute permission to the script.

With this method script is executed by interpreter which is
defined at sha-bang line.

Note: Script path should be relative to the current
directory then only this method works. If you want to execute a
script in the current directory you use ./scriptname. In fact if
you want to execute a script in the parent directory of your
current location you can use ../scriptname , as . means current
and .. means parent.

Run temp script.

[root@localhost shell]# ./temp
My name is root
Users are root root
OS name is Linux
current shell is /bin/bash


now change sha-bang line with #!/bin/ksh and execute the script

[root@localhost shell]# ./temp
bash: ./temp: /bin/ksh: bad interpreter: No such file or directory


now change sha-bang line with #!/bin/zsh and execute the script

[root@localhost shell]# ./temp
My name is root
Users are root root
OS name is Linux
current shell is /bin/bash


So wrong path to interpreter or bad interpreter gives error message.

Method 2 to execute a shell script:


you can execute the bash script by specifying the filename
as an argument to the bash,sh,zsh commands.
This is a bad way to call a script, since it will override
the #! at the beginning of the script.Depending on command you
used, current shell spawns relative subshell.

$sh temp
or
$bash temp
or
$zsh temp
Output:
My name is root
Users are king root root sai venu
OS name is Linux
Current shell is /bin/bash

Note: It is good to use proper command. Suppose your script is
korn shell script,then run it as:
ksh temp

Caution: In this method shell runs script in sub shell.
So you can't access some shell builtin variable values.
For example run bellow shell script:

#!/bin/bash
lines=$LINES
cols=$COLUMNS
echo $lines
echo $cols


Output:
$ sh sample
It prints nothing

Note: To solve this problem export current shell variables
using “export” command.Before executing script, export all
variables you need.

Example:
$ export LINES COLUMNS
$ sh sample
24
80

Method 3 to execute a shell script:


you can execute the bash script by specifying the filename
as an argument to the . (dot) or source command. In this method
script will be run in current shell.
Example:
$ . temp
or
$ source temp

Output:
[root@localhost shell]# . temp
My name is root
Users are king root root sai venu
OS name is Linux
current shell is /bin/bash

[root@localhost shell]# source temp
My name is root
Users are king root root sai venu
OS name is Linux
current shell is /bin/bash

Caution:
Try to unset variables at the end of the script. Otherwise
you will get add results. Variable and functions used in the
script will be alive after execution of the script.

Example:
create two files
cat > sample2
n=100
m=200
echo "n=$n m=$m"
ctrl+d

cat > sample3
n=$((n+m))
m=$((m+25))
echo "n=$n m=$m" ctrl+d

Execute scripts using sh command

[root@localhost shell]# sh sample2
n=100 m=200
[root@localhost shell]# sh sample3
n=0 m=25

Now execute scripts using . Or source command

[root@localhost shell]# . sample2
n=100 m=200
[root@localhost shell]# . sample3
n=300 m=225

So be careful. Try to unset variables at the end of the script.

Method 4 to execute a shell script:


You can also execute scripts by just typing its name alone.
However, for this method to work, the directory containing the
script must be defined in your PATH variable and file must has
executable permission.

For example run “temp” script directly

[root@localhost shell]# temp
bash: temp: command not found

Now add your home directory to PATH variable(I am assuming
script located at home directory).

PATH=${PATH}:$HOME

Now execute script with name, you will get result.

[root@localhost ~]# temp
My name is root
Users are king root root sai venu
OS name is Linux
current shell is /bin/bash

Suppose you changed “temp” script name to “ls”, what will
happen, which result you will get.

[root@localhost ~]# mv temp ls
[root@localhost ~]# ls
Desktop ls sample sample2 sample3

So you will get “ls” command output. Why ?

When you type name of any command at $ prompt, what shell
do is it first look that command in it's internal part(called
as internal command,which is part of shell itself, and always
available to execute),if found as internal command shell will
execute it, if not found then shell will look PATH setting,
and try to find our requested commands executable file in all
of the directories mentioned in PATH settings,if found it will
execute it, other wise it will give message
“bash:XXXX:command not found”.

So don't use command name as shell script.

Note:If you have two different files with same name in different
directories, which file will execute?
The directory which comes first in PATH setting will be executed.

Example:
First create a file “testscript” in home directory.

$ cat > testscript
whoami
ctrl+d

Then create a directory “bin” in home directory
$ mkdir bin
Now create a file with same name “testscript” in bin dirctory.
$ cd bin
$ cat > testscript
date
ctrl+d

Then add “bin” directory to PATH variable.If your “bin” directory
already in PATH setting leave it.

PATH=${PATH}:$HOME/bin

now check your path settings

[root@localhost ~]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:
/usr/lib/ccache:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/
bin:/usr/bin:/bin:/root/bin:/root

/root and /root/bin added to your PATH.
I added /root/bin directory before /root directory.

now execute “testscript” script

[root@localhost ~]# testscript
Sat Oct 10 06:57:14 IST 2009

5 comments:

  1. this is really gud yarr, keep it up

    ReplyDelete
  2. In "Method 1" the shell script first should be given the executable permission, if it does not have by chmod.

    chmod +x script.sh
    ./script.sh

    ReplyDelete
    Replies
    1. I need to give executable permission through script.

      Delete
  3. Well post and this article tell us about shell script and how to create a file for counting in documents thanks for sharing letter of recommendation service .

    ReplyDelete
  4. To get maximum protection for your little child you got to read this little article here.

    ReplyDelete