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

Friday, August 15, 2008

How to check if environment variable is defined or not in Unix?

Suppose, I want to check if variable USER_ID is defined or not.

---------------------------------------------------------------

The best solution I found is:



NOTDEFINED=ItsUnDefined

USER_ID=${USER_ID:-$NOTDEFINED}



if [ "$USER_ID" = "ItsUnDefined" ]

then

echo Enter USER ID:

read USER_ID

fi

echo USER_ID



Explanation: USER_ID=${USER_ID:-$NOTDEFINED}

This will check if USER_ID variable is defined or not. If it is undefined, it will assign value of variable NOTDEFINED to it otherwise it will use its own value.



Generally we use this to see if user has passed command line parameters. And if user has not passed parameter, you can set default value using this.

USER_ID=${1:-arpit}

echo $USER_ID

---------------------------------------------------------------

The second solution can be used is:



envgrep "^USER_ID"

if [ $? -eq 1 ]

then

echo Enter USER ID:
read USER_ID
fi
echo USER_ID



Explanation: env lists all environment variables defined for the shell. Then you can grep to see, if the variable is defined or not.



---------------------------------------------------------------

Wednesday, May 28, 2008

Unix: sed to change characters in particular position

Suppose you have big file. You want to change character 100-110 with some text. So, simply you want to interchange first 4 characters with the next 4 characters in each line. In these kind of scenarios, sed one liners will be very useful.

See few examples below:

$cat a.tmp
20090918ARPITH2010011634
20090918ARPITH2010012050
20090905ARPITH2010011382
20090824ARPITH2010012075
20090921ARPITH2010012075
--Follwing sed command will interchange first 4 digits with next 4 digits.
$sed 's:\([0-9]\{4\}\)\([0-9]\{4\}\):\2\1:' a.tmp
09182009ARPITH2010011634
09182009ARPITH2010012050
09052009ARPITH2010011382
08242009ARPITH2010012075
09212009ARPITH2010012075
Explanation:
\([0-9]\{4\}\) It defines first four digits. If you want to replace any 4 characters not only digit then replace [0-9] with . like \(.\{4\}\)
\ It is to escape (
() This bracket will define different groups. It is used for backreference and can be used again using \1 or \2 etc based on its occurence.
[0-9] Check for digits
\{4\} Four occurence of digit in [0-9]

\2\1 It says first put second set and then first set.

Following sed command will interchange first 8 characters/digits with next 6 characters. And it will also seperate each set with space.
$sed 's:\(.\{8\}\)\(.\{6\}\):\2 \1 :' a.tmp
ARPITH 20090918 2010011634
ARPITH 20090918 2010012050
ARPITH 20090905 2010011382
ARPITH 20090824 2010012075
ARPITH 20090921 2010012075

Following command will replace characters from 9 to 15 with text "NIRAVB"
sed 's:\(.\{8\}\)\(.\{6\}\):\1NIRAVB:' a.tmp
20090918NIRAVB2010011634
20090918NIRAVB2010012050
20090905NIRAVB2010011382
20090824NIRAVB2010012075
20090921NIRAVB2010012075

Limitation: If you specify more than 255 in curly braces "\{ \}", you will get following error.
$sed 's:\(.\{256\}\)\(.\{6\}\):\1NIRAVB:' a.tmp
sed: Function s:\(.\{256\}\)\(.\{6\}\):\1NIRAVB: cannot be parsed.

Solution: To overcome the issue you can break 335 in 255 + 80. As I have shown in following example. Here I want to change character 335 to 338 with text "TEST"
$cat a.tmp
20090913476178957893478958937589078903745897123890789074589076238904758906179038759081738904715890378904518978907348907579487592763785691287589072386590827890752890475890274890768902748906892748976898000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005
20090913476178957893478958937589078903745897123890789074589076238904758906179038759081738904715890378904518978907348907579487592763785691287589072386590827890752890475890274890768902748906892748976898000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005
20090913476178957893478958937589078903745897123890789074589076238904758906179038759081738904715890378904518978907348907579487592763785691287589072386590827890752890475890274890768902748906892748976898000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005

$sed 's:\(.\{335\}\)\(.\{4\}\):\1TEST:' a.tmp
sed: Function s:\(.\{335\}\)\(.\{4\}\):\1TEST: cannot be parsed.

$sed 's:\(.\{255\}\)\(.\{80\}\)\(.\{4\}\):\1\2TEST:' a.tmp

20090913476178957893478958937589078903745897123890789074589076238904758906179038759081738904715890378904518978907348907579487592763785691287589072386590827890752890475890274890768902748906892748976898000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000TEST00000000000000000000000000000000005
20090913476178957893478958937589078903745897123890789074589076238904758906179038759081738904715890378904518978907348907579487592763785691287589072386590827890752890475890274890768902748906892748976898000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000TEST00000000000000000000000000000000005
20090913476178957893478958937589078903745897123890789074589076238904758906179038759081738904715890378904518978907348907579487592763785691287589072386590827890752890475890274890768902748906892748976898000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000TEST00000000000000000000000000000000005

Thursday, April 17, 2008

Which Shell I am using in Unix

Try following commands:

$echo $0
OR
$finger -m
OR
$echo $SHELL
OR
$ps -p $$

Friday, April 11, 2008

Sunday, March 9, 2008

Use of truss and lsof in Unix

First command is truss.
The truss utility executes the specified command and produces a trace of the system calls it performs, the signals it receives, and the machine faults it incurs.
Each line of the trace output reports either the fault or signal name or the system call name with its arguments and return value(s).The command can be used as::: truss ./abc [abc is executable program]On following link you can find examples of the same.
http://www.ibm.com/developerworks/aix/library/au-unix-tracingapps.html
Another useful command is lsof.
lsof lists all the open files used by all the programs.
An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file.lsof can also be used to retrieve accidently deleted files, if they are currently being used by any process.On following link you can find examples of the same.
http://www.ibm.com/developerworks/aix/library/au-lsof.html?S_TACT=105AGX20&S_CMP=EDU