Collection of unix commands is called as shell scripting
Shell Valriables
System defined variables (environmental variables)
User defined variables
useally we can declar the variable like
x=10
echo x -- it will give you the x value in the same shell prompt
If we have to pass the variable as globally for diffrent shell prompts
then we need to export the value
export x=10
echo x --- now it will pass the same value to diffrent shell prompts
use can switch from one shell to other shell
echo $0 -- it will show you the current shell prompt details
ksh --- it will switch to ksh shell prompt --- $exit --- you can exit from the
current shell promt environment
every shell script ends with .sh -- as a extention
Environmental varialbles:These are used for mostly making the configuration and env
setup process
echo $DISPLAY
DISPLAY EDITOR GROUP HOME HOST IFS(internal file separators)
LOGNAME PATH PS1-primary string ($) PS2-secondary prompt string (default to >)
SHELL-shell type we are using TERM-terminal type
User defined variables:
These are defined by user and are used most extensively in shell programming.
Creating user defined variables
1. The first character of a variable name should be alphabet or underscore
2. No commas or blanks are allowed within a variable name.
3. Variable names should be of any reasonable length.
4. Variable names are case sensitive
5. It shouldn't be reserved word.
Shell key or Reserved word: We have many key/reserved words in unix, below are the
some of the words
echo esac if eval read break
else exec set continue read-only
unset while untill do trap ulimit
case shift wait exit done umask
Mainly shell scripts are two types,
1. Non-Interactive shell scripting --- While running the script it doesn't requires
any inputs from the user or keyboard.
2. Interactive shell scripting --- While running the script it requires the input
from the user or keyboard.
sample script-1
vi [Link]
#It's my sampel script
echo "Welcome to the world of Shell Scripting"
echo 'Defauld script is BASH'
echo Thank you.!
run the script -- sh [Link] or ksh [Link]
Sample script-2
Counting the current number of users in the unix system
genally we can get it by using a command like
$ users -- it will give you the user details
$ users | wc -w (or) who -q --- this will give you the count of the users
same result will make in the form of scripting,
Sample script-3
vi [Link]
#Number of current users in the unix system
echo currently logged in users are ' who | wc -l '
echo currently logged in users are ' who -q '
echo logged in users count is: ' users | wc -w '
echo "success, thanks!"
will list out the files in a directory
$ ls | wc -l --- will count the files
vi [Link]
echo "Number of files in the current dir"
ls | wc -l
echo "are existed"