Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Tuesday, July 26, 2011

How to load user preference at user login

At Linux/Unix, user can set preference value(e.g. alias, folder and file default color). These values are loaded from a startup file every time when a user logs in or by excecuting the "source {filename}" command.

Startup file are different from one shell to and other. To confirm what the current shell is, simply do the command "echo $shell", and it will be shown on the screen. The startup file usually stored at the default root directory - usually this is the first location where a user hits after the login.

There are always some overly cautious administrator would assign the user to another directory other than the default root directory by executing a cd command at the startup file for the sake of safety. In this case, one must first guess what the the default root directory is by the username and have a search in the /usr/ directory.

Startup file are usually invisible(filenames with "." at the front) to the user, to make it visible, do "ls -a" and edit it with your default editor.

Startup file name by Shells:
.login      --      csh shell
.tcshrc    --     tcsh shell

A sample Startup script:

#set environment

#alias
alias lla 'ls -lrta'
alias ll 'ls -lrt'
alias ls 'ls -x --color'
alias pwd 'echo $cwd' # This is faster than executing the pwd command
alias rm '/bin/rm -i'
alias checkdate 'ls \\!^ | awk -F. {print $1 $7} | sort -u'

setenv LS_COLORS 'no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:'

Monday, June 27, 2011

SQLPLUS Command using cronjob

Spend three days to figure out how to copy an oracle DB table from a cronjob.

After some intensive surfing for reference online, finally realize what I miss is the declaration.
It is worthy to note down:

For sh:

#!/bin/sh

ORACLE_HOME=/DB/HOME
export ORACLE_HOME
ORACLE_BASE=/DB/HOME
export ORACLE_BASE
ORACLE_OWNER=oracle
export ORACLE_OWNER
ORACLE_SID=MYDBID
export ORACLE_SID
NLS_LANG="american_america.we8iso8859p1"
export NLS_LANG

HOME=/HOME/DIR/

PATH=/usr/bin:/bin:/usr/local/bin:$ORACLE_HOME/bin
export PATH

sqlplus -s username/password@MYDBID @${HOME}/script.sql >> ${HOME}/something.txt

For csh:

#!/bin/csh

setenv ORACLE_HOME /DB/HOME
setenv ORACLE_BASE /DB/HOME
setenv ORACLE_OWNER oracle
setenv ORACLE_SID MYDBID
setenv NLS_LANG "american_america.we8iso8859p1"

set HOME = /HOME/DIR/

setenv PATH /usr/bin:/bin:/usr/local/bin:$ORACLE_HOME/bin

sqlplus -s username/password@MYDBID @${HOME}/script.sql >> ${HOME}/something.txt

-hayashi

Friday, April 8, 2011

Wednesday, February 9, 2011

About chmod...

1. set it readable only for outsiders:
Before: -rwxr-xr-x  archive.sh
Command: chmod o=r archive.sh
After: -rwxr-xr--  archive.sh
2. Take away all permissions for the group for topsecret.inf
Before: -rw-r-----  topsecret.inf
Command: chmod g= topsecret.inf
After: -rw-------  topsecret.inf
3. Open up publicity.html for reading and writing by anyone.
Before: -rw-r--r--  publicity.html
Command: chmod og=rw publicity.html
After: -rw-rw-rw-  publicity.html

Passwordless SSH connection

Spend the whole day to figure out how it works = ="

This is the scenario where server A wants to establish a connection to B without keying password:

a) Log in Server A with user Id 'userA' and corresponding passowrd.

b) Create the keys in server 'A' with the following command.
/user/local/bin/ssh-keygen –t rsa

c) Copy the public key in id_rsa.pub file under HOME_DIRECTOR/.ssh directory of server 'A'

d) Copy the 'id_rsa.pub' file of server A to 'authorized_keys' file of server B in B's HOME_DIRECTORY/.ssh directory

e) Grant both .ssh/ of server A & B 700 (chmod -R 700 .ssh/)

f) Log into server 'A' with 'userA' user ID

g) Execute the sftp command for server B, eg. sftp userB@Bserver



Tuesday, February 8, 2011

Current time

$ date
Tue Dec 19 21:16:03 CST 2000 
$ date +"%m" 
12 
$ date +"%B"
December
$ date +"%Y-%m-%d" 
2000-12-19
$

Monday, February 7, 2011

Simple awk Command to de-deplicate file

$ cat file.txt
DD:12:A
AA:11:N
EE:13:B
AA:11:F
BB:09:K
DD:13:X

#Based on first field. Duplicates are DD,AA
$ awk '!x[$1]++' FS=":" file.txt
DD:12:A
AA:11:N
EE:13:B
BB:09:K

Again,

$ cat file.txt
DD:12:A
AA:11:N
EE:13:B
AA:11:F
BB:09:K
DD:13:X


#This time, based on first and 2nd field.Only duplicate combination is (AA:11)
$ awk '!x[$1,$2]++' FS=":" file.txt
DD:12:A
AA:11:N
EE:13:B
BB:09:K
DD:13:X

Thursday, January 20, 2011

Get Date of Yesterday

----------------------------Ksh Shell Script--------------------------------

#! /usr/bin/ksh
# Get yesterday's date in YYYY-MM-DD format.
# With argument N in range 1..28 gets date N days before.
# Tapani Tarvainen January 2002
# This code is in the public domain.

OFFSET=${1:-1}

case $OFFSET in
  *[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year

# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=$((day - OFFSET))
if (( day <= 0 )) ;then
  month=$((month - 1))
  if (( month == 0 )) ;then
    year=$((year - 1))
    month=12
  fi
  set -A days `cal $month $year`
  xday=${days[$(( ${#days[*]}-1 ))]}
  day=$((xday + day))
fi

print $year-$month-$day
print $month/$day/${year#??}


------------------------Bash Shell Script------------------------------

#!/bin/bash

OFFSET=1;

eval `date "+day=%d; month=%m; year=%Y"`

# Subtract offset from day, if it goes below one use 'cal'

# to determine the number of days in the previous month.

day=`expr $day - $OFFSET`

if [ $day -le 0 ] ;then

month=`expr $month - 1`

if [ $month -eq 0 ] ;then

year=`expr $year - 1`

month=12

fi

set `cal $month $year`

xday=${$#}

day=`expr $xday + $day`

fi

echo $year-$month-$day


------------------------- Linux--------------------------

curdate=`date +%y%m%d -d "yesterday"`

Write Script with arithmetic Operations

#!/bin/sh
echo "enter two numbers"
read a b
c=$(($a+$b))
echo "$a + $b = $c"
# or
let c="$a+$b"
# or
c=`expr $a + 1`

echo "$a + $b is $c"
 



Useful monitoring command of Unix/Linux Vol.2

Change ownership of a directory: chown -Rv username somedir
Change usergroup or a directory: chgrp -Rv usergroup somedir
To set environment variable: setenv
To load the control file: source <filename> (Shell csh only)
To enable sqlplus/Oracle at the first run:

setenv ORACLE_HOME /opt/app/oracle/product/10.2.0/Db_1
setenv ORACLE_SID ASMDB003
setenv ORACLE_TRACE T
setenv ORAENV_ASK NO
setenv NLS_LANG American_America.we8iso8859p1

setenv LD_LIBRARY_PATH /usr/lib:/usr/ucblib:/usr/local/lib:/opt/SUNWspro/lib:$OR
ACLE_HOME/lib:$ORACLE_HOME/precomp/lib:$ORACLE_HOME/rdbms/lib:$ORACLE_HOME/jdbc/
lib:$ORACLE_HOME/dbjava/lib:/user/sfw/lib

setenv PATH /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/cc
s/bin:/opt/SUNWspro/bin:${ORACLE_HOME}/bin
setenv PATH ${PATH}:${HOME}/bin


Useful monitoring command of Unix/Linux Vol.1

For Solaris Only:

check number of core: prtdiag -v 
create new user: useradd jsmith
get user information: finger jsmith
set password: passwd jsmith
set default home directory: usermod -d /path/to/new/homedir/ username
set default Shell: change permenently the SHELL at etc/passwd
check Solaris release: cat /etc/release
Check current shell: echo $SHELL
Check shell version: /bin/bash --version