There are three types of shell variables available. They are,
a)Local variables
b)Environment variables
c)Shell variables or Special variables
Local variables:
A local variable is a variable, its value will be visible to the current shell.
Environment variables:
An environment variable is a variable, its value will be visible to all the child shell's or child processes started from the shell.
Shell variables or Special variables:
A shell variable or special variable is a variable set by shell for its necessity.Some of these variables are environment variables whereas others are local variables
Shell Scripting for Unix/Linux
Thursday, April 7, 2011
Wednesday, April 6, 2011
How to quote the value of the variables appropriately?
First of all, there are two type of quoting. Each one has its own significance. We will see that through examples.
Types of quoting:
a.) Single quoting
b.) Double quoting
When to use single quote?
If you want the value to be treated as exactly entered. Then, should go for quoting through single quotes. But there are some special characters which will not follow the rule thought it is entered inside single quotes.
Eg:
volcano@volcano-laptop:~/shellscript$ os='sun solaris'
volcano@volcano-laptop:~/shellscript$ echo $os
sun solaris
volcano@volcano-laptop:~/shellscript$ echo 'another operating system is Linux'
another operating system is Linux
When to use double quote?
If you want special characters expanded properly while it is part of the value of variable.Then double quote should be used.
volcano@volcano-laptop:~/shellscript$ logged_user=$USER
volcano@volcano-laptop:~/shellscript$ echo "Logged in user is $logged_user" Logged in user is volcano
Types of quoting:
a.) Single quoting
b.) Double quoting
When to use single quote?
If you want the value to be treated as exactly entered. Then, should go for quoting through single quotes. But there are some special characters which will not follow the rule thought it is entered inside single quotes.
Eg:
volcano@volcano-laptop:~/shellscript$ os='sun solaris'
volcano@volcano-laptop:~/shellscript$ echo $os
sun solaris
volcano@volcano-laptop:~/shellscript$ echo 'another operating system is Linux'
another operating system is Linux
When to use double quote?
If you want special characters expanded properly while it is part of the value of variable.Then double quote should be used.
volcano@volcano-laptop:~/shellscript$ logged_user=$USER
volcano@volcano-laptop:~/shellscript$ echo "Logged in user is $logged_user" Logged in user is volcano
Overview about variables in shell script
Variables meaning and its use are more or less similar like other programming language variables. As usual variables are used to hold values. Like other programming languages, shell script doesn't require you to declare the variables.Value of the variables can be retrieved by placing $ (dollar) symbol in front of the variable.
Rules governing variables:
a. Variables name should start either with character or underscore
b. While assigning the value to a variable, no space should be left between the left and right hand side of "=" operator.
c. While assigning the value to a variable, required quotes should be provided to avoid the error. If value has space in between them, then value should be enclosed in quotes.
eg:
"country" and "same" are variable names.
volcano@volcano-laptop:~/shellscript$ country=India
volcano@volcano-laptop:~/shellscript$ echo $country
India
volcano@volcano-laptop:~/shellscript$ same=$country
volcano@volcano-laptop:~/shellscript$ echo $same
India
volcano@volcano-laptop:~/shellscript$ name=sujay kumar
kumar: command not found
volcano@volcano-laptop:~/shellscript$ echo $name
volcano@volcano-laptop:~/shellscript$ name="Sujay Kumar"
volcano@volcano-laptop:~/shellscript$ echo $name
Sujay Kumar
Example for variables in a shell script:
volcano@volcano-laptop:~/shellscript$ cat vari.sh
#!/bin/bash
#variable declaration, assignment, & retrieval of value
country=India
echo $country
same=$country
echo $same
name="Sujay Kumar"
echo $name
volcano@volcano-laptop:~/shellscript$ ./vari.sh
India
India
Sujay Kumar
volcano@volcano-laptop:~/shellscript$
Rules governing variables:
a. Variables name should start either with character or underscore
b. While assigning the value to a variable, no space should be left between the left and right hand side of "=" operator.
c. While assigning the value to a variable, required quotes should be provided to avoid the error. If value has space in between them, then value should be enclosed in quotes.
eg:
"country" and "same" are variable names.
volcano@volcano-laptop:~/shellscript$ country=India
volcano@volcano-laptop:~/shellscript$ echo $country
India
volcano@volcano-laptop:~/shellscript$ same=$country
volcano@volcano-laptop:~/shellscript$ echo $same
India
volcano@volcano-laptop:~/shellscript$ name=sujay kumar
kumar: command not found
volcano@volcano-laptop:~/shellscript$ echo $name
volcano@volcano-laptop:~/shellscript$ name="Sujay Kumar"
volcano@volcano-laptop:~/shellscript$ echo $name
Sujay Kumar
Example for variables in a shell script:
volcano@volcano-laptop:~/shellscript$ cat vari.sh
#!/bin/bash
#variable declaration, assignment, & retrieval of value
country=India
echo $country
same=$country
echo $same
name="Sujay Kumar"
echo $name
volcano@volcano-laptop:~/shellscript$ ./vari.sh
India
India
Sujay Kumar
volcano@volcano-laptop:~/shellscript$
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!
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.
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.
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.
#!/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!"
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
%
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
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
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
Monday, February 28, 2011
Why Bash Shell?
- Shell script written in Bourne shell (sh) can be executed in Bash without any modification
- Shell script written in Bash shell can be executed in Korn Shell(ksh) without any modification
- Bash incorporates useful features from Korn shell and C shell
- Bash shell is the default shell in GNU/Linux systems
Sunday, February 27, 2011
Shell scripting advantages and disadvantages
Advantages:
Disadvantages:
- To automate the frequently performed operations
- To run sequence of commands as a single command
- Easy to use
- Portable (It can be executed in any Unix-like operating systems without any modifications)
Disadvantages:
- Slow execution speed compared to any programming languages
- A new process launched for almost every shell command executed
Saturday, February 26, 2011
Shell Scripting Cycle
1. Creation of script
Inclusion of specific shell absolute path (eg: #!/bin/bash )
Inclusion of comments about the functionality of the script
Inclusion of Unix commands and scripting language constructs
2. Setting up execute permission to the script
3. Execution of script
4. Debugging of script
Inclusion of specific shell absolute path (eg: #!/bin/bash )
Inclusion of comments about the functionality of the script
Inclusion of Unix commands and scripting language constructs
2. Setting up execute permission to the script
3. Execution of script
4. Debugging of script
Friday, February 25, 2011
Shell Script
Unix command(s) or/and shell programming logic written in a file to accomplish certain specific objective is called shell script.
Shell scripts are small programs written using shell and Unix commands and interpreted by shell.
Shell scripts are small programs written using shell and Unix commands and interpreted by shell.
Thursday, February 24, 2011
Shell Scripting language
Different kinds of shells used or available in the Unix/Linux operating system contains a built-in programming language referred to as shell scripting language.
Brief Shell History
First Unix Shell: Thompson Shell -> Implemented in the first version of Unix in 1971 by Ken Thompson.
sh (Bourne Shell) is the full featured Unix shell written by Stephen Bourne at Bell labs in 1974.
Other shells are,
bash (Bourne again shell)
ash (almquist shell)
csh (c shell)
ksh (Korn shell)
tcsh (TENEX c shell)
zsh (z shell)
sh (Bourne Shell) is the full featured Unix shell written by Stephen Bourne at Bell labs in 1974.
Other shells are,
bash (Bourne again shell)
ash (almquist shell)
csh (c shell)
ksh (Korn shell)
tcsh (TENEX c shell)
zsh (z shell)
Wednesday, February 23, 2011
What is Shell in Unix/Linux?
Shell is a command interpreter program which acts as an interface between the user and the Unix/Unix-like operating systems.
Primary objective is to read commands from console/terminal and then execute them.
Two major feature of any shell:
Primary objective is to read commands from console/terminal and then execute them.
Two major feature of any shell:
- Acts as Interpreter
- Acts as programming environment or capability which allows you to write & execute shell script
Subscribe to:
Posts (Atom)