September 16, 2014

Scripting made fun

I have never been a fan of programming or scripting. Thought is was a skill I was unable to learn. When learning to administer a Linux system, scripting can not be avoided.

One of the skills every sysAdmin must learn is scripting. The benefit of scripting is to automate a task or job that is constantly run every time a system is running. A sysAdmin can make that job run on its own and concentrate on other tasks that are not so easily automated. These scripts are either written using a text editor, the shell, or, scripting language.

I was not automating anything, just getting used to writing scripts. When I was studying "Linux essentials" in preparation for the Linux certification, I was practicing passing variable values. That is, printing the value of one of my system's default variables. I had a crazy thought. What if I actually ran a script using a variable. I soon learned, Linux is what you make of it.

There are default variables already set on a Linux system. To find what these variables are, you simply type this command in a shell or terminal emulator, “printenv”

This is a screen shot of the default variables on my Linux Mint 17 system.

XDG_VTNR=7

SSH_AGENT_PID=1808

XDG_SESSION_ID=c1

TERM=xterm

SHELL=/bin/bash

XDG_SESSION_COOKIE=9f6e6efa3c8a79091dbec7c85403e053-1410643266.227255-1746036259

WINDOWID=35651588

GNOME_KEYRING_CONTROL=/run/user/1000/keyring-ccaIQh

USER=x

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:*.war=01;31:*.ear=01;31:*.sar=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:*.webm=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:

OPENDVD=eject /dev/sr0

SSH_AUTH_SOCK=/x

PLAYMEDIA=vlc /dev/sr0

SESSION_MANAGER=x

USERNAME=x

DEFAULTS_PATH=/usr/share/gconf/default.default.path

XDG_CONFIG_DIRS=/etc/xdg/xdg-default:/etc/xdg

PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games

DESKTOP_SESSION=default

GDM_XSERVER_LOCATION=local

PWD=/etc

GNOME_KEYRING_PID=1585

LANG=en_US.UTF-8

MANDATORY_PATH=/usr/share/gconf/default.mandatory.path

MDM_XSERVER_LOCATION=local

GDMSESSION=default

SHLVL=1

XDG_SEAT=seat0

HOME=/x/x

XDG_SESSION_DESKTOP=default

LOGNAME=x

XDG_DATA_DIRS=/usr/share/default:/usr/share/xfce4:/usr/local/share/:/usr/share/:/usr/share/mdm/

DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-ONZKNCkspd,guid=10fed374626ed9dae74c359c5414b544

MDMSESSION=default

TEXTDOMAIN=im-config

WINDOWPATH=7

XDG_RUNTIME_DIR=/run/user/1000

DISPLAY=:0.0

MDM_LANG=en_US.UTF-8

TEXTDOMAINDIR=/usr/share/locale/

COLORTERM=xfce4-terminal

XAUTHORITY=/x/x/x

_=/usr/bin/printenv

Take a close look at the bold text above. These are my custom variables I set to run some custom scripts.

OPENDVD=eject /dev/sr0 is used to open my laptop's DVD disc drive.

PLAYMEDIA=vlc /dev/sr0 is used to play a DVD using VLC.

Of course I could have just written the scripts using a text editor. I wanted to make things more interesting by try something different. Also, doing this helped me learn and appreciate environmental variables much more. I find them really fun to play with.

Always remember this syntax, "command argument". The command is what you want to run, the argument is what you want it to run on. The command “VLC” opens the VLC media player program. The argument “/dev/sr0” is the DVD disc drive I want it to open. Typing this in your terminal will do just that.

If you look at the subfolder “etc”, you will notice a file named “environment”. You can actually use this filie to create and store your own custom declared variables. On some Ubuntu based systems, you might noticed the default “PATH” variable is also located there. A little F.Y.I.

Once you set your desired variable, and save the file, restart your system. Run the “printenv” command and you will see your variables listed.

To set a variable, type in all caps, the name of the variable, then after that, the “=” sign and then the value that the variable will relate to. Like this, VARIABLE=value. In my case I typed, OPENDVD=”eject /dev/sr0”. The quotes surrounding the command indicates that the command should be treated as one value.

Once this is written, I had reset my mint system. Once logged in, and the terminal was opened, I called the value of the new variable. This is when you tell your terminal to print the value of a variable. The command to do this is, prompt<$VARIABLENAME>.When I typed, “$OPENDVD”, my dvd disc drive opens. Your system will recognize a variable when it starts with a dollar sign followed by a name in all caps as in, "$OPENDVD".

I was not big on programming. Yet, scripting, using variables, not only makes Linux even more interesting but programming as well. I already have a large list of scripts I want to experiment with. When I think about it, scripting might actually be my thing. I went beyond just learning what a variable is. I begun learning how to talk to Linux using my own language.

Click Here!