Day 1 (28/OCT/2011)

DAY 1 (28/10/11): Basics of environmental variables
Courtesy: Zubin Mithra (Zubin.py)

Today I’ve learned how to does the “which” command works.

Lets see what does the which command do:

PROGRAM: which.sh

#! /bin/sh
set -ef

if test -n "$KSH_VERSION"; then
puts() {
print -r -- "$*"
}
else
puts() {
printf '%s\n' "$*"
}
fi

ALLMATCHES=0

while getopts a whichopts
do
case "$whichopts" in
a) ALLMATCHES=1 ;;
?) puts "Usage: $0 [-a] args"; exit 2 ;;
esac
done
shift $(($OPTIND - 1))

if [ "$#" -eq 0 ]; then
ALLRET=1
else
ALLRET=0
fi
case $PATH in
(*[!:]:) PATH="$PATH:" ;;
esac
for PROGRAM in "$@"; do
RET=1
IFS_SAVE="$IFS"
IFS=:
case $PROGRAM in
*/*)
if [ -f "$PROGRAM" ] && [ -x "$PROGRAM" ]; then
puts "$PROGRAM"
RET=0
fi
;;
*)
for ELEMENT in $PATH; do
if [ -z "$ELEMENT" ]; then
ELEMENT=.
fi
if [ -f "$ELEMENT/$PROGRAM" ] && [ -x "$ELEMENT/$PROGRAM" ]; then
puts "$ELEMENT/$PROGRAM"
RET=0
[ "$ALLMATCHES" -eq 1 ] || break
fi
done
;;
esac
IFS="$IFS_SAVE"
if [ "$RET" -ne 0 ]; then
ALLRET=1
fi
done

exit "$ALLRET"

In this code it searches in $PATH which is an Unix Environmental
variable.
$PATH: Contains a colon-separated list of directories that the
shell searches for commands that do not contain a slash in their
name.

Environmental variables: are set of dynamic named values that can
affect the way running processes will behave on a computer.

The next thing i learned was env command. Which is used to run a
program in a modified environment.

I wrote a code which prints the env variable using system call and
then using recursive call.

PROGRAM 1
: Prints the “env” using system call


#include

void main()
{
system("env");
}

PROGRAM 2: Prints the “env” using recursive call taking $PATH as
argument


#include

void main(int argc, char **argv)
{
char **env;
for(*env = *argv; *env != 0; *env++)
printf("%s\n", *env);
}

PROGRAM 3: Prints the “env” from the third parameter of the main()


#include

void main(iint argc, char** argv, char** envp)
{
char** env;
for (*env = *envp; *env != 0; *env++)
printf("%s\n", *env);
}

PROGRAM 4: Prints the environmental variables using extern variable
extern varible: explicit declaration of a variable outside any
function
block.


#include

extern char **environ;

void main (int argc, char** argv)
{
char **env;
for (*env = *environ; *env != 0; *env++)
printf("%s\n", *env);
}

PROGRAM 3 & 4 are similar, but the difference is extern char **environ
is defined by POSIX while envp (third arg to main) is just something
that a lot of implementations support.

Operands of the form `VARIABLE=VALUE’ set the environment variable
VARIABLE to value VALUE. VALUE may be empty (`VARIABLE=’). Setting a
variable to an empty value is different from unsetting it. These
operands are evaluated left-to-right, so if two operands mention the
same variable the earlier is ignore.

OUTPUT 1: This is the result when we set operand of the form
‘VARIABLE=VALUE’

$TEST="test" ./printenv

TEST=test
SSH_AGENT_PID=1686
KDE_MULTIHEAD=false
DM_CONTROL=/var/run/xdmctl
GPG_AGENT_INFO=/tmp/gpg-BxOIKJ/S.gpg-agent:1687:1
SHELL=/bin/bash
TERM=xterm
XDG_SESSION_COOKIE=3fbe2f4bcf01367a55a5b7da00000009-1319853565.56222-1836657581
XDM_MANAGED=method=classic
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/seshagiri/.gtkrc-2.0:/home/seshagiri/.gtkrc-2.0-kde4:/home/seshagiri/.kde/share/config/gtkrc-2.0
KONSOLE_DBUS_SERVICE=:1.60
GS_LIB=/home/seshagiri/.fonts
GTK_RC_FILES=/etc/gtk/gtkrc:/home/seshagiri/.gtkrc:/home/seshagiri/.kde/share/config/gtkrc
WINDOWID=69206034
OLDPWD=/home/seshagiri/s7_codes/hackingnotes
SHELL_SESSION_ID=2645cf0c109d4000b64fd51ddc31cd45
GTK_MODULES=canberra-gtk-module
KDE_FULL_SESSION=true
USER=seshagiri
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
SSH_AUTH_SOCK=/tmp/ssh-HHHsGNWv1640/agent.1640
SESSION_MANAGER=local/sav:@/tmp/.ICE-unix/1786,unix/sav:/tmp/.ICE-unix/1786
DEFAULTS_PATH=/usr/share/gconf/default.default.path
XDG_CONFIG_DIRS=/etc/xdg/xdg-default:/etc/xdg
DESKTOP_SESSION=default
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/home/seshagiri/s7_codes/bla
KDE_SESSION_UID=1000
LANG=en_IN
MANDATORY_PATH=/usr/share/gconf/default.mandatory.path
QT_GRAPHICSSYSTEM=raster
UBUNTU_MENUPROXY=libappmenu.so
KONSOLE_DBUS_SESSION=/Sessions/4
HOME=/home/seshagiri
COLORFGBG=15;0
SHLVL=1
KDE_SESSION_VERSION=4
LANGUAGE=en_IN:en
XCURSOR_THEME=oxy-white
LOGNAME=seshagiri
XDG_DATA_DIRS=/usr/share/default:/usr/local/share/:/usr/share/
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-snoxDpU8ne,guid=4c033814089e44f2f44d73f30000006c
LESSOPEN=| /usr/bin/lesspipe %s
WINDOWPATH=7
PROFILEHOME=
DISPLAY=:0
QT_PLUGIN_PATH=/home/seshagiri/.kde/lib/kde4/plugins/:/usr/lib/kde4/plugins/
LESSCLOSE=/usr/bin/lesspipe %s %s
_=./printenv

OUTPUT 2:This is result when we set Operand ‘VARIABLE=’


TEST=
SSH_AGENT_PID=1686
KDE_MULTIHEAD=false
DM_CONTROL=/var/run/xdmctl
GPG_AGENT_INFO=/tmp/gpg-BxOIKJ/S.gpg-agent:1687:1
SHELL=/bin/bash
TERM=xterm
XDG_SESSION_COOKIE=3fbe2f4bcf01367a55a5b7da00000009-1319853565.56222-1836657581
XDM_MANAGED=method=classic
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/seshagiri/.gtkrc-2.0:/home/seshagiri/.gtkrc-2.0-kde4:/home/seshagiri/.kde/share/config/gtkrc-2.0
KONSOLE_DBUS_SERVICE=:1.60
GS_LIB=/home/seshagiri/.fonts
GTK_RC_FILES=/etc/gtk/gtkrc:/home/seshagiri/.gtkrc:/home/seshagiri/.kde/share/config/gtkrc
WINDOWID=69206034
OLDPWD=/home/seshagiri/s7_codes/hackingnotes
SHELL_SESSION_ID=2645cf0c109d4000b64fd51ddc31cd45
GTK_MODULES=canberra-gtk-module
KDE_FULL_SESSION=true
USER=seshagiri
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
SSH_AUTH_SOCK=/tmp/ssh-HHHsGNWv1640/agent.1640
SESSION_MANAGER=local/sav:@/tmp/.ICE-unix/1786,unix/sav:/tmp/.ICE-unix/1786
DEFAULTS_PATH=/usr/share/gconf/default.default.path
XDG_CONFIG_DIRS=/etc/xdg/xdg-default:/etc/xdg
DESKTOP_SESSION=default
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/home/seshagiri/s7_codes/bla
KDE_SESSION_UID=1000
LANG=en_IN
MANDATORY_PATH=/usr/share/gconf/default.mandatory.path
QT_GRAPHICSSYSTEM=raster
UBUNTU_MENUPROXY=libappmenu.so
KONSOLE_DBUS_SESSION=/Sessions/4
HOME=/home/seshagiri
COLORFGBG=15;0
SHLVL=1
KDE_SESSION_VERSION=4
LANGUAGE=en_IN:en
XCURSOR_THEME=oxy-white
LOGNAME=seshagiri
XDG_DATA_DIRS=/usr/share/default:/usr/local/share/:/usr/share/
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-snoxDpU8ne,guid=4c033814089e44f2f44d73f30000006c
LESSOPEN=| /usr/bin/lesspipe %s
WINDOWPATH=7
PROFILEHOME=
DISPLAY=:0
QT_PLUGIN_PATH=/home/seshagiri/.kde/lib/kde4/plugins/:/usr/lib/kde4/plugins/
LESSCLOSE=/usr/bin/lesspipe %s %s
_=./printenv

OUTPUT 3: If we invocate the executable file by using env
before the executable file.

$env ./printenv

SSH_AGENT_PID=1686
KDE_MULTIHEAD=false
DM_CONTROL=/var/run/xdmctl
GPG_AGENT_INFO=/tmp/gpg-BxOIKJ/S.gpg-agent:1687:1
SHELL=/bin/bash
TERM=xterm
XDG_SESSION_COOKIE=3fbe2f4bcf01367a55a5b7da00000009-1319853565.56222-1836657581
XDM_MANAGED=method=classic
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/seshagiri/.gtkrc-2.0:/home/seshagiri/.gtkrc-2.0-kde4:/home/seshagiri/.kde/share/config/gtkrc-2.0
KONSOLE_DBUS_SERVICE=:1.60
GS_LIB=/home/seshagiri/.fonts
GTK_RC_FILES=/etc/gtk/gtkrc:/home/seshagiri/.gtkrc:/home/seshagiri/.kde/share/config/gtkrc
WINDOWID=69206034
OLDPWD=/home/seshagiri/s7_codes/hackingnotes
SHELL_SESSION_ID=2645cf0c109d4000b64fd51ddc31cd45
GTK_MODULES=canberra-gtk-module
KDE_FULL_SESSION=true
USER=seshagiri
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
SSH_AUTH_SOCK=/tmp/ssh-HHHsGNWv1640/agent.1640
SESSION_MANAGER=local/sav:@/tmp/.ICE-unix/1786,unix/sav:/tmp/.ICE-unix/1786
DEFAULTS_PATH=/usr/share/gconf/default.default.path
XDG_CONFIG_DIRS=/etc/xdg/xdg-default:/etc/xdg
DESKTOP_SESSION=default
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/home/seshagiri/s7_codes/bla
KDE_SESSION_UID=1000
LANG=en_IN
MANDATORY_PATH=/usr/share/gconf/default.mandatory.path
QT_GRAPHICSSYSTEM=raster
UBUNTU_MENUPROXY=libappmenu.so
KONSOLE_DBUS_SESSION=/Sessions/4
HOME=/home/seshagiri
COLORFGBG=15;0
SHLVL=1
KDE_SESSION_VERSION=4
LANGUAGE=en_IN:en
XCURSOR_THEME=oxy-white
LOGNAME=seshagiri
XDG_DATA_DIRS=/usr/share/default:/usr/local/share/:/usr/share/
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-snoxDpU8ne,guid=4c033814089e44f2f44d73f30000006c
LESSOPEN=| /usr/bin/lesspipe %s
WINDOWPATH=7
PROFILEHOME=
DISPLAY=:0
QT_PLUGIN_PATH=/home/seshagiri/.kde/lib/kde4/plugins/:/usr/lib/kde4/plugins/
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/env

OUTPUT 1 & 2 creates an environment variable. In OUTPUT 1 TEST
variable is made with a value test, while in OUTPUT 2 it TEST
is not assigned to any values and hence NULL is assigned to
TEST. But OUTPUT 3 just invocate env and hence variables
are not made.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s