Tuesday, July 31, 2012

Enable and disable shell built in commands.

Enable command allows you to enable and disable your shell built in commands.

View the shell built in commands using

[root@tester ~]# enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable continue
enable declare
enable dirs
enable disown
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable fg
enable getopts
enable hash
enable help
enable history
enable jobs
enable kill
enable let
enable local
enable logout
enable popd
enable printf
enable pushd
enable pwd
enable read
enable readonly
enable return
enable set
enable shift
enable shopt
enable source
enable suspend
enable test
enable times
enable trap
enable true
enable type
enable typeset
enable ulimit
enable umask
enable unalias
enable unset
enable wait

Disable the commands using -n option

[root@tester ~]# enable -n history
[root@tester ~]# history
bash: history: command not found.

[root@tester ~]# history

Enable the commands using -a option.

145  enable -a history
  146  history
  147  enable
  148  man command
  149  enable -n history
  150  history
  151  enable -a history
  152  history | less
 153   history


You can also view using the set command . For more options you can check with man page.

[root@tester ~]# set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off




Implementing Global and Local variable using Set, Unset, env

The bash shell uses a feature called environment variables to store information about the shell session and the working environment. This feature also allows you to store data in memory that can be accessed by any program or script running from the shell. There are 2 types of variables Global and local.

    Global variables are visible from the shell session. Local variables are only available in the shell creates them.The linux system sets several global variables when you start your bash session. The system environment variables always use capital letters to differentiate them from normal user environmental variables

To view the global variables you can use the printenv command.
The set command displays all of the environmental variables set for a specific processes. How ever this also includes the global environment variables.

[root@tester ~]# set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.1.17(1)-release'
COLORS=/etc/DIR_COLORS.xterm
COLORTERM=gnome-terminal
COLUMNS=80
CVS_RSH=ssh
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-u7p0n37t4X,guid=5fc61850c666640beda0c84c08ab0a00
DESKTOP_SESSION=default
DESKTOP_STARTUP_ID=
DIRSTACK=()
DISPLAY=:0.0
EUID=0
GDMSESSION=default
GDM_XSERVER_LOCATION=local
GNOME_DESKTOP_SESSION_ID=Default
GNOME_KEYRING_SOCKET=/tmp/keyring-oY8mdS/socket
GROUPS=()
GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2
G_BROKEN_FILENAMES=1
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=rac1.orainstance.com
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=24
LOGNAME=root
LS_COLORS='no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:'
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=5466
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SESSION_MANAGER=local/rac1.orainstance.com:/tmp/.ICE-unix/4499
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
SSH_AGENT_PID=4597
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_AUTH_SOCK=/tmp/ssh-nHuEYj4499/agent.4499
TERM=xterm
UID=0
USER=root
WINDOWID=31457361
XAUTHORITY=/tmp/.gdmRXFEIW
XMODIFIERS=@im=none
_=/etc/bashrc
consoletype=pty

[root@tester ~]# env
SSH_AGENT_PID=4597
HOSTNAME=rac1.orainstance.com
DESKTOP_STARTUP_ID=
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2
WINDOWID=31457361
USER=root
LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
GNOME_KEYRING_SOCKET=/tmp/keyring-oY8mdS/socket
SSH_AUTH_SOCK=/tmp/ssh-nHuEYj4499/agent.4499
SESSION_MANAGER=local/rac1.orainstance.com:/tmp/.ICE-unix/4499
MAIL=/var/spool/mail/root
DESKTOP_SESSION=default
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
GDM_XSERVER_LOCATION=local
INPUTRC=/etc/inputrc
PWD=/root
XMODIFIERS=@im=none
LANG=en_US.UTF-8
GDMSESSION=default
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/root
SHLVL=2
GNOME_DESKTOP_SESSION_ID=Default
LOGNAME=root
CVS_RSH=ssh
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-u7p0n37t4X,guid=5fc61850c666640beda0c84c08ab0a00
LESSOPEN=|/usr/bin/lesspipe.sh %s
DISPLAY=:0.0
G_BROKEN_FILENAMES=1
COLORTERM=gnome-terminal
XAUTHORITY=/tmp/.gdmRXFEIW
_=/bin/env

To display the value of an environmental variable, use the echo command.
You can set the environmental variables and remove it using unset command.


[root@tester ~]# test="krish"
[root@tester ~]# echo test
test
When referencing a environment variable , you must place a dollar sign before the environmental variable name
[root@tester ~]# echo $test
krish
[root@tester ~]# unset test
[root@tester ~]# echo $test

The PATH environment variables seems to cause the most problem on linux systes. It defines where the shell looks for commands you enter on the command line. If it cant find the command it produces an error message.
PATH=$PATH:/home/user/test
echo $PATH
/usr/kerberos/sbin:/usr/local/bin:/home/user/test
The individual directories separated by a colon.












Monday, July 9, 2012

Disable append files in Linux



Appending the files sometimes may erase the existing data. For security purpose you can use noclobber command to prevent this.

[root@tester ~]# cat > testers.txt
append the data to test
[root@tester ~]# cat testers.txt
append the data to test

Set noclobber using the following syntax and you will not be able to append the file there after.

[root@tester ~]# set -o noclobber testers.txt
[root@tester ~]# cat > testers.txt
-bash: testers.txt: cannot overwrite existing file

You can remove the noclobber using the below syntax.

[root@tester ~]# set +o noclobber testers.txt
[root@tester ~]# cat >> testers.txt
test completed
[root@tester ~]# cat testers.txt
append the data to test
test completed

Few vim techniques

There are many commands to work with vim editor but here are few which will be more useful in realtime.

vim +/keyword file name

will place the cursor to the exact key workd line, you can also use the line number like below

vim +linenumber filename

to move the cursor from the reverse you can use

vim +?keyword file name

you can also enter the vim editor and press ? to search the keyword from reverse. To check from the correct order you can use /key word.

To view the line numbers you can press ESC:se nu

Some of the method to copy the lines in the file.

yy

to copy 10 lines you can use yy10

to paste you can use P

you can also use the visual mode and select the lines you require to copy.

ctrl+z and selecting the lines and use right click copy or yy.

To select all the lines from the cursor point you can use yG

To make your files more secured, you can set password for your files.

open vim editor type the content, ESC:X
type password.
:wq

There after when ever you need to access the file it will prompt you to enter the password. If incorrect password is set the data will be displayed in the encrypted format.

use vimdiff command to find the difference between two files .
use ctrl +ww to change the switch the cursor to the other file and if you need to copy the line to the next window press ctrl dp