Cracking IT Interview

<- Back | HOME | NEXT ->

Variable:


As name implies, something which changes its value. When we have to store any value for any calculation in script, we use variable. Consider it as a memory space with some name assigned to it. Also you can think it as a container which contains some value and we give some name to each container to recognize and use it.


Defining variable:


Declaring a variable by data type is not required in Unix. Directly we give a name and assign the value to it by assignment operator(=). Remember, do not give any white space before or after '=' sign given.


varable_name=value

Ex:

NAME="Vikas K Gupta"

num=5






We keep the variable name in capital letters mostly by convention but there is no restriction over small case letters with variable names as well. Variable names contain alphabets (A to Z / a to z), numbers (0 to 9) or an underscore sign (_). Second thing to remember, Variable name can start with either alphabets or an underscore and not with numbers. Special symbols (other than underscore) are not permitted within a variable name.


Accessing variable value:


Just to access the variable value, give "$" just before its name.

Ex: echo "your name is $NAME"


Deleting variable value:


we can delete the value using "unset" command as:

unset Variable_name

Ex: unset NAME


But, we can't delete the values of variables which are declared as "readonly". 


Read-only Variables:

We can make a value constant with the variable by "readonly" keyword. We can't change the value further once it is declared as read only.

readonly variable_name
Ex: readonly NAME





Example Script: Swapping of numbers between 2 variables 
#!/bin/sh
#Comment: Entering the value for two variables and assigning them
echo "Enter the value for VAR1 = "
read VAR1
echo "Enter the value for VAR2 = "
read VAR2
#swapping values between two variables
VAR3=$VAR1
VAR1=$VAR2
VAR2=$VAR3
echo "Value of third variable used in swapping = $VAR3"
unset VAR3
echo "Value of third variable after swapping and unset = $VAR3"
#Printing the value of variables
echo "Value of VAR1 = $VAR1"
echo "Value of VAR2 = $VAR2"
readonly VAR3
VAR3=10
echo "Value of VAR3 = $VAR3"


Script Output:

[Vikas ~]$ sh swapscript.sh
Enter the value for VAR1 =
50
Enter the value for VAR2 =
100
Value of third variable used in swapping = 50
Value of third variable after swapping and unset =
Value of VAR1 = 100
Value of VAR2 = 50
swapscript.sh: VAR3: is read only


Environmental Variables


These are the variables which already defined by the system and hold some default values. We also call it as system variables. All variable names are in uppercase letters by convention. Whenever we have to use these variables in our script to print some system values, just use "$" before the variable name as we are using in case of ordinary variables.


To list out environmental variables, use "env" or "printenv" command at the prompt:

[Vikas ~]$ env
SSH_CONNECTION=183.83.179.208 5672 75.61.90.142 22
TZ=EST5EDT
PW_SCAN_BIG_IDS=1
USER=vikas
ENV=/etc/kshrc
PWD=/p/i/vikas
HOME=/p/i/vikas
SSH_CLIENT=183.83.179.208 5672 22
SSH_TTY=/dev/pts/3
MAIL=/var/mail/vikas
SHELL=/usr/local/bin/bash
TERM=vt100
SHLVL=1
BLOCKSIZE=1k
LOGNAME=vikas
PATH=/bin:/usr/bin:/usr/local/bin:/usr/games:/usr/sbin

_=/usr/bin/env


To know value of some specific environmental variables, print with "echo" at the prompt as:


$echo $HOME
/p/i/Vikas
$echo $PATH
/bin:/usr/bin:/usr/local/bin:/usr/games:/usr/sbin
$echo $HOSTNAME
mynet.vikasnet.org
$echo $USER
Vikas
$echo $SHELL
/usr/local/bin/bash


Another way to list out your environment variable and set the environment:

  • Go to you home directory
  • List out hidden .profile file and open that file to check the environmental variables
  • Open it with VI editor and make the changes to the default value with your own.


[Vikas ~]$ cd ~
[Vikas ~]$ ls -la .profile
-rw-r--r--  1 pinku  guest  118 Jun  4  2017 .profile
[Vikas ~]$ vi .profile
PS1="$USER |`date` >>"
#CLICOLOR="YES"
#export CLICOLOR
#LSCOLORS="ExGxFxdxCxDxDxhbadExEx"
#export LSCOLORS






After changing the PS1 value when I am opening the new session window, prompt is appearing as below:

Vikas |Sat Jul  7 11:02:51 EDT 2018 >>

To change your shell prompt temporarily for one session, simply assign the required value ("$" below) to PS1 as:

pinku@m-net.arbornet.org|Sun Jul  8 03:29:45 EDT 2018 >>PS1=$
$


Set up environment / Changing the value of environmental variable:

Standard way of doing this is to assign the value to variable and export it like:


environmental_variable=Value

export environmental_variable


Ex:

USER="Gupta"
export USER
JAVA_PATH="/usr/java/jdk"
export JAVA_PATH


Array
(sh and bash shell)

Special variable which can hold multiple values. Actually, it is used group multiple values in contiguous memory location with one name assigned to it. We use index numbers for each values stored in array which starts from zero. There is no limit of values which can be stored in array.

Defining Array
ARRAY_VAR=(value1 value2 value3 …..)


Values must be separated with white spaces enclosed within parentheses() at the time of defining array.













 NEXT ->