Thursday, May 31, 2007

Convert Decimal to Hexadecimal, Octal or Binary using typeset

Here is a quick and dirty way of converting a decimal number to hexadecimal, octal, or binary. Below is an inline script followed by a normal shell script file. There are some non-looping examples toward the end of the post.

# ksh
# for i in 100 102 103 104 105 110 1000
> do
> typeset -i16 hex
> hex=$i
> print $i equals $hex in hexadecimal
> typeset -i8 oct
> oct=$i
> print $i equals $oct in octal
> typeset -i2 bin
> bin=$i
> print $i equals $bin in binary
> done

100 equals 16#64 in hexadecimal
100 equals 8#144 in octal
100 equals 2#1100100 in binary

102 equals 16#66 in hexadecimal
102 equals 8#146 in octal
102 equals 2#1100110 in binary

103 equals 16#67 in hexadecimal
103 equals 8#147 in octal
103 equals 2#1100111 in binary

104 equals 16#68 in hexadecimal
104 equals 8#150 in octal
104 equals 2#1101000 in binary

105 equals 16#69 in hexadecimal
105 equals 8#151 in octal
105 equals 2#1101001 in binary

110 equals 16#6e in hexadecimal
110 equals 8#156 in octal
110 equals 2#1101110 in binary

1000 equals 16#3e8 in hexadecimal
1000 equals 8#1750 in octal
1000 equals 2#1111101000 in binary

Or put it in a script and specify an argument list via CLI

# vi my_converter.ksh


if [ $# = 0 ]; then
echo Add an argument list

for i in $*

typeset -i16 hex
print $i equals $hex in hexadecimal

typeset -i8 oct
print $i equals $oct in octal

typeset -i2 bin
print $i equals $bin in binary


# chmod 755 my_converter.ksh

# my_converter.ksh 23 32 33 (Michael Jordan, Magic Johnson, Larry Bird)

No looping examples

# sh
Convert Decimal to Hexadecimal
# echo 'obase=16;100'| bc

Convert Decimal to Decimal
# echo 'obase=10;10'| bc

Convert Decimal to Octal
# echo 'obase=8;34'| bc

Convert Decimal to Binary
# echo 'obase=2;10'| bc

Binary, Octal, Hexadecimal to Decimal
# ksh
# echo $((2#101010))
# echo $((8#52))
# echo $((16#2A))

Wednesday, May 30, 2007

Calculate with the Basic Calculator -- bc

You can do simple command line calculations with the basic calculator, bc, such as add, subtract, multiply, division, modulus, sqrt, raise a number to a power, logarithms, exponential, compute sines, cosines, loops, functions, and more. Here are some really basic examples -- scale was used to represent precision. I have used the bc command while in the single user mode.


Sunday, May 27, 2007

Exclude Files or Directories from an Archive using tar

Sometimes you might want to routinely backup certain files or directories that are on a mass storage device (RAID), but you don't want to backup the entire RAID. You can do this by using the tar command's exclude option. You probably want to exclude the exclude and tarball files. Here's an example of its use.

First I create the exclude file and give it an arbitrary name, filesDir_to_exclude

#cat > filesDir_to_exclude
Control d

#cd /raid
#tar cvfpX esofthub.tar filesDir_to_exclude *

Encrypt and Decrypt the Contents of a File

Here's a simple way to encrypt the contents of a message using the crypt command. The security level is fairly weak but it's not meant to be PGP. Here an example to encrypt and decrypt the contents of a file.

To encrypt
Encrypt with mykey in the open
# crypt mykey<not_encrypted_file>encrypted_file

Prompted for mykey
# crypt<not_encrypted_file>encrypted_file
Enter key:mykey

To decrypt
Decrypt with mykey in the open
# crypt mykey<encrypted_file | more

Prompted for mykey
# crypt<encrypted_file | more
Enter key:mykey

Mail the encrypted file as an attachment
Use a mail client such as Netscape, FireFox, or dtmail because it's easier to move the binary file.

Saturday, May 26, 2007

Remove the UNIX root Password

From time to time, passwords are lost or forgotten - even root's password. One way of removing the root password is to boot to single user mode from an installation cdrom. Mount the root, /, partition and blank out the encrypted password field in the shadow file. Here's an illustration.

ok boot cdrom -s

# mount /dev/dsk/c0t0d0s0 /mnt

# more /mnt/etc/shadow

# vi /mnt/etc/shadow
root:BLANK THIS FIELD OUT:13832::::::

# cd /
# umount /mnt
# reboot -- -s

Change the root password in single user mode (don't need to add "root" but I do it for clarification.
# passwd root
passwd: Changing password for root
New password:
Re-enter new password:
passwd (SYSTEM): passwd successfully changed for root

Thursday, May 24, 2007

Display the Address Space of a Process

The pmap command is used to display the address space of process ID. It shows the specific allocation of private and shared memory in kilobytes and the permissions for each mapped file. We've used it to troubleshoot database issues with memory. Here's an example.

pmap -x 2563
2563: in.telnetd
Address Kbytes Resident Shared Private Permissions Mapped File
-------- ------ ------ ------ ------
total Kb 1792 1528 1120 408

Monday, May 21, 2007

Sun Fire V1280 is Crashing

For this post, I'm deviating from my normal simple UNIX HOWTO's format.

A colleague of mine is having a problem with a Sun Fire v1280 server crashing. He tried Googling for the error message in red but hasn't found anything yet. Your insights would be greatly appreciated.

"cannot schedule clearing of error on page 0x00000000.f6eb4000; page not
in VM system type errors."

Here are other comments he made...

We *are* seeing memory errors too, but a note said this is a CPU problem.

We have some apparent bad memory and we have the replacements but the current memory errors are on a different J stick.

Remove Zero Length and Core Files With Inline Script

Here's a C shell inline script to remove zero length and core files. This particular script removes these files from the esofthub directory. Here's an example.

# foreach zero_core(/home/esofthub/*)
? if (-z $zero_core || `basename $zero_core` == "core") then
? rm $zero_core
? endif
? end

Just find zero length files (per request)

# foreach zero_core(/home/esofthub/*)
? if (-z $zero_core) then
? rm $zero_core
? endif
? end

Sunday, May 20, 2007

Drop a Sybase Database and its Device

Occasionally, you may need to drop a Sybase database and its device. For example, the database may need to be expanded but there is enough space on the device. But first, you will need to drop the database before dropping its device. Then confirm whether the database and the device were dropped or not. Here is an example.

# isql -Usa -Ppassword
1> use master
2> go
1> sp_helpdb
2> go
1> drop database MYDB
2> go
1> sp_dropdevice MYDB_device
2> go
1> sp_helpdb
2> go
1> sp_helpdevice
2> go

Friday, May 18, 2007

Extract the Contents of a Compressed Tar File

A colleague of mine asked about extracting the contents of a compressed tarball into a particular directory. I suggested downloading the tarball into the /tmp directory. It was a fairly large tarball consisting of several hundred megabytes. Here's an example of what was done.

#cd /tmp
#zcat compressed_tarball.Z | (cd /export/opt; tar xvfp -)

Thursday, May 17, 2007

Identify Process Hierarchy for an Application

Occasionally, you might want to print out the process hierarchy for an application. This can be done using the ptree command. This command can print out the parent and child processes. Here’s an example of its use using Netscape’s PID.

# ptree -a 532
1 /etc/init -
274 /usr/dt/bin/dtlogin -daemon
292 /usr/dt/bin/dtlogin -daemon
311 /bin/ksh /usr/dt/bin/Xsession
356 /usr/dt/bin/sdt_shell -c unset DT; DISPLAY=:0;
359 -sh -c unset DT; DISPLAY=:0; /usr/dt/bin/dtse
372 /usr/dt/bin/dtsession
379 dtwm
404 /usr/dt/bin/dtexec -open 0 -ttprocid 2.16H6ao 01 371 1289
405 /usr/dt/bin/dtterm
407 /sbin/sh
531 ./netscape
532 ./.netscape.bin -xrm *appDir: /usr/dt/appconfig/n
534 (dns helper)

Tuesday, May 15, 2007

Get Information on a Domain Name

I was curious as to how many variations were out there. I used the whois utility, which is an Internet user name directory service, to ascertain that information.

# whois

Whois Server Version 1.X

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to
for detailed information.


The registry database contains only .COM, .NET, .EDU domains and

Thursday, May 10, 2007

Nested Inline For Loop for Recursive Directory Creation

The inline script will create 5 recursive directories and recursively change the directory permission bits to read, write and execute for the owner and for all others read and execute.

Here's the syntax.

#cd to_creation_dir
# for i in dir_1 dir_2 dir_3 dir_4 dir_5
> do
> for j in 2005 2006 2007
> do
> for k in jan feb mar apr may jun jul aug sep oct nov dec
> do
> for l in mon tue wed thu fri sat sun
> do
> for m in log1 log2 log3 log4 log5
> do
> mkdir -p $i/$j/$k/$l/$m; chmod -R 755 $i/$j/$k/$l/$m
> done
> done
> done
> done
> done

Add String to Filenames Using an Inline For Loop

A friend of mine had about 500 files in a directory. Based on specific extensions, he wanted roughly 300 of them to have a prepended string attached. An inline script was used to accomplish the task. Here is a simple example.

Here is the busy work part
# ls *.specific_extension_1 > mylist
# ls *.specific_extension_2 >> mylist
# ls *.specific_extension_3 >> mylist
# ls *.specific_extension_1 *.specific_extension_2 *.specific_extension_3 > mylist

Now for the inline for loop
# sh
# for filename in `cat mylist`
mv $filename PREPEND_STRING$filename
echo $filename changed | tee -a /tmp/logfile

Wednesday, May 09, 2007

Dump Sybase Database to Tape

Obviously, it is always a good idea to backup your database to a spare system disk, RAID, or to another slice on the system disk. But I think it is equally important to backup the database to something offline, i.e. tape.

If the tape hasn't been initialized before, you will need to add the keyword init into your syntax. You will only have to do that once. Again, it's a fairly straightforward process. Here is an example.

# isql -Usa -Ppassword
1> use master
2> go
1> dump database MYDB to mytapealias with init
2> go

Load Sybase Database From Tape

On occasion, you may have to restore a database from a tape. This is a fairly simple and straightforward task to perform. Here is the syntax to load a Sybase database from tape.

First, get a list of the database filename(s) on the tape

# isql -Usa -Ppassword
1> load database MYDB from mytapealias with listonly
2> go

Next, restore the database from tape and then put it online

1> use master
2> go
1> load database MYDB from mytapealias with file = 'MYDBXXXXXXX'
2> go
1> online database MYDB
2> go

Tuesday, May 08, 2007

List Logged In Users on System

Here are four handy commands to ascertain who is logged onto a local system. The first one is the who command, which provides a list of users. The second one is the who am i command. It shows the current user. The third one is the w command. It shows information about currently logged-in users. The last command is the finger command, and it displays user information.

The who command

The who am i command
#who am i

The w command
From a commenter's post.
root@mars# w
1:44pm up 4:29, 1 user, load average: 0.05, 0.05, 0.05
User tty login@ idle JCPU PCPU what
root pts/1 9:24am 1:56 w

The finger command
#finger esofthub

Other notable commands to ascertain logged in users on the network (rwho and rusers can bog large networks down due to broadcasts)

The rwho command

The rusers command

Sunday, May 06, 2007

Lookup an Internet Protocol (IP) Address for a URL

Sometimes it might be necessary to lookup an IP address for a URL. The nslookup command can provide you that capability. Here's an example.


Here's the output of nslookup
Server: (DNS server)
Address: x.x.x.1

Non-authoritative answer:
Addresses: (the IP addresses for

Here are some more examples using nslookup
# nslookup

Default Server:
Address: XXX.XXX.X.1


> set type=ns
Address: XXX.XXX.X.1

Non-authoritative answer: nameserver = nameserver = nameserver = nameserver = nameserver = nameserver = nameserver =

Authoritative answers can be found from: internet address = XXX.XX.X.7 internet address = XX.XX.XX.X internet address = XX.XX.XX.X internet address = XXX.XX.X.X internet address = XX.XXX.XX.9 internet address = XX.XXX.X.X internet address = XX.XX.X.X

Mail servers

> set type=mx
Address: XX.XXX.XX.1

Non-authoritative answer: preference = 1, mail exchanger = preference = 1, mail exchanger = preference = 1, mail exchanger = preference = 1, mail exchanger = preference = 1, mail exchanger = preference = 1, mail exchanger = preference = 1, mail exchanger =
Authoritative answers can be found from: nameserver = nameserver = nameserver = nameserver = nameserver = nameserver = nameserver = internet address = X.X.X.11 internet address = X.X.X.2 internet address = X.X.X.X internet address = X.X.X.X internet address = XX.XXX.XXX.X internet address = X.XX.XX.X internet address = XX.XXX.X.X internet address = X.X.X.13 internet address = XXX.XX.XX.X internet address = X.X.X.12 internet address = X.XX.XXX.X internet address = X.X.XX.X internet address = XX.XX.XX.X internet address = XX.XXX.X.3