Wednesday, October 31, 2007

Evaluate Constants and Strings with expr Command

The expr command is a handy utility for evaluating constants and strings via the command line interface. Here are a few examples of expressions.

# expr 9 \* 9
81
# expr 9 / 9
1
# expr 9 + 9
18
# expr 9 - 9
0
# expr 9 % 9
0
# expr 50 - 100
-50
# expr 6 + 8 \* 9
78
# expr 9 \* \( 2 + 6 \)
72

Evaluate relational operators: true (1) or false (0)

# expr aaa \< myblog
1
# expr aaa \> myblog
0
# expr aaa == myblog
0
# expr aaa != myblog
1
# expr 8 \<> 9
0
# expr 8 != 9
1
# expr 8 \<= 9
1

Monday, October 29, 2007

Remove the PHPSESSID From URL

The last couple of days I have been trying to remove unsightly PHPSESSIDs from my URLs. Here's how one looked on google.com.

http://www.xxxxxx.com/details_xxxxxx__xxxxxxxxxxxxxx-.html?
PHPSESSID=976fc83764c21749f95e831xxx6a3bxx

I opened up the php.ini file in Notepad and changed the 1 to 0. I'll check later on to see if it worked.

session.use_trans_sid = 0

-----
You can also put this in your config file
ini_set('session.use_trans_sid', false);

You can also modify the .htaccess file but a lot of web hosts won't let you do this type of modification. I tried.
php_flag session.use_trans_sid off

Sunday, October 28, 2007

Get Better Compression with bzip2 Command - UNIX

I was trying out the bzip2 compression command. I did notice better compression rates with larger files (testing large tarballs) compared to gzip or compress. But it does take quite a bit of time to perform the compression. Here's one run I did.

# ls -l mytest20.tar
-rw-r--r-- 1 root other 50747392 Oct 28 23:58 mytest20.tar
# gzip mytest20.tar
# ls -l mytest20.tar.gz
-rw-r--r-- 1 root other 9763163 Oct 28 23:58 mytest20.tar.gz
# gunzip mytest20.tar.gz
# bzip2 mytest20.tar
# ls -l mytest20.tar.bz2
-rw-r--r-- 1 root other 5757170 Oct 28 23:58 mytest20.tar.bz2

To uncompress use bunzip2 or bzip2 -d
# bunzip2 mytest20.tar.bz2
# ls -l mytest20.tar
-rw-r--r-- 1 root other 50747392 Oct 28 23:58 mytest20.tar

Monday, October 22, 2007

Display MAC, IP and DNS information for Windows

I've had few questions about obtaining the MAC and IP addresses on a Windows box. Personally, I find the ipconfig command as the easiest way to get this information. Here's an example.

C:\Documents and Settings\esofthub>ipconfig /all

Windows IP Configuration

Host Name . . . . . . . . . . . . : ixxx
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Mixed
IP Routing Enabled. . . . . . . . : Yes
WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) PRO/1000 CT Network Connect
ion
Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : xxx.xxx.x.x
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : xxx.xxx.xx.x
DHCP Server . . . . . . . . . . . : xxx.xxx.x.x
DNS Servers . . . . . . . . . . . : xxx.xxx.xx.x
xxx.xxx.xx.x
Lease Obtained. . . . . . . . . . : Monday, October 22, 2007 6:46:11 PM
Lease Expires . . . . . . . . . . : Tuesday, October 23, 2007 6:46:11 PM

Thursday, October 18, 2007

Modify a Command's Scheduling Priority with nice

There may be times when you need to modify the priority of a command prior to starting it. You can do this by using the nice command. It modifies the scheduling priority of the command . 19 is the lowest priority, 1 is the highest priority, and 10 is the default. Here are a few examples.


Highest priority
# nice -n 1 myfind &
returns PID

Lowest priority
# nice -n 19 myfind &
returns PID

Default priority is 10

Also, the renice command can be used to change the priority of a job.

# renice -18 PID

stephane added the priorities range from -20 (or -19 depending on the Unix) to 19, -20/-19 being the most favorable scheduling, 19 the least favorable one. The negative increments are allowed only for root.

So if you are root, you can do this : nice -19 myfind

to get it running with max priority.

Wednesday, October 17, 2007

Check Sent Mail Status with mailq

I had a number of users informed me that their messages were "stuck in the queue or something hanging." Their concerns were confirmed after using the mailq command. After a brief investigation, it was discovered was a small hiccup with DNS.

Here's an example of the mailq output.
# mailq
or
# mailq -v
/var/spool/mqueue (19 requests)
----Q-ID---- --Size-- -Priority- ---Q-Time--- ---------Sender/Recipient--------
l9HDvk500367 5 120046 Oct 17 22:57 user1
user2
l9GGhCW02706 1333 7770119 Oct 17 01:43 user3
user1
l9GGhCX02706 1363 7770119 Oct 17 01:43 user4
user5
...

Tuesday, October 16, 2007

Recovering a Corrupted Mailbox -- UNIX

The other day I had user who was having problems popping mail from the mail server. My first thoughts were wrong password, POP server issue or permissions. Well, it was none of the above. Then I started thinking about the mailbox itself. Here's what I found.

Btw, this example is not the original user's mailbox but it should demonstrate the problem.

Notice there are three lines that don't make much sense.

# cd /var/mail
# more user1
Status: RO
X-Status: $$$$
X-UID: 0000000001

From user2 Fri Jun 29 22:51:34 2007
Return-Path:
Received: (from user2@localhost)
by esoft (8.11.6+Sun/8.11.6) id l5TDpYJ01536
for root; Fri, 29 Jun 2007 22:51:34 +0900 (KST)
Date: Fri, 29 Jun 2007 22:51:34 +0900 (KST)
From: Super-User
Message-Id: <200706291351.l5tdpyj01536@esoft>
...
...

I deleted those 3 lines (plus the blank line) and re-saved the mailbox. After that, he was able to pull his mail successfully.

# more user1
From user2 Fri Jun 29 22:51:34 2007
Return-Path:
Received: (from user2@localhost)
by esoft (8.11.6+Sun/8.11.6) id l5TDpYJ01536
for root; Fri, 29 Jun 2007 22:51:34 +0900 (KST)
Date: Fri, 29 Jun 2007 22:51:34 +0900 (KST)
From: Super-User
Message-Id: <200706291351.l5tdpyj01536@esoft>
...
...

Friday, October 12, 2007

The Popular XV Image Viewer for UNIX

Yesterday, I received an email asking me about a versatile jpeg image viewer for UNIX. My initial thoughts were to use the common browser or the image related utilities in /usr/openwin/bin -- imagetool or snapshot.

Then I started thinking about XV. XV is a popular image viewing shareware program. I have found it to be very effective at resizing screenshots, cropping, expanding, and converting images to different image formats. Personally, I've been using XV (3.10 and now 3.10a) for a number of years without complaint.

Here's another reliable source for XV Image Viewer.

I installed xv in my /opt/apps directory.
# /opt/apps/xv &

Thursday, October 11, 2007

Display a Specified Number of Lines From Multiple Files

Here's an ad-hoc way of viewing a specified number of lines that begins with line one. In this example, the templated report has 8 lines and only its values change each day.

I only want to show the first 5 lines for each file. Here's a quick and dirty way of doing that.

In this template file (Mon), the values are only shown for illustration purposes .

# cat Mon
Average Stats: 100
Hourly Stats: 4
Daily Stats: 103
Weekly Stats: 700
Monthly Stats: 2812
###############
END END
###############

Display 5 different files and show their first 5 lines
# head -5 Mon Tue Wed Thu Fri
==> Mon <==
Average Stats: 100
Hourly Stats: 4
Daily Stats: 103
Weekly Stats: 700
Monthly Stats: 2812

==> Tue <==
Average Stats: 102
Hourly Stats: 4
Daily Stats: 104
Weekly Stats: 706
Monthly Stats: 2822

==> Wed <==
Average Stats: 104
Hourly Stats: 5
Daily Stats: 110
Weekly Stats: 730
Monthly Stats: 2842

==> Thu <==
Average Stats: 90
Hourly Stats: 200
Daily Stats: 2400
Weekly Stats: 4499
Monthly Stats: 3006

==> Fri <==
Average Stats: 101
Hourly Stats: 3
Daily Stats: 106
Weekly Stats: 703
Monthly Stats: 2855

Here's a poor man's label maker. Cut (literally) and paste :)
# cat > From
FirstName LastName
123 Any Street
Anytown, State, Zipcode
Ctrl+d

# head From From From
==> From <==
FirstName LastName
123 Any Street
Anytown, State, Zipcode

==> From <==
FirstName LastName
123 Any Street
Anytown, State, Zipcode

==> From <==
FirstName LastName
123 Any Street
Anytown, State, Zipcode

Friday, October 05, 2007

Delete Extra Spaces Between Words in a File

I had a file peppered with extra spaces, which were between the terms (chars need to be contiguous). I could read it but it was fairly annoying. I used the tr utility to delete most of the extra spaces and then vi'ed the file to rid it of the rest. To accomplish this task, here's an example using the tr utility -- nothing fancy.

Since Blogger is displaying this test file without the extra spaces, please take my word for it.
# cat filename
This is a test. We will delete the extra spaces.
This can be accomplished with the UNIX tr utility.
# cat filename | tr -s ' ' ' '
This is a test. We will delete the extra spaces.
This can be accomplished with the UNIX tr utility.

Output to newfilename
# cat filename | tr -s ' ' ' ' > newfilename

View newfilename contents
# more newfilename
This is a test. We will delete the extra spaces.
This can be accomplished with the UNIX tr utility.

Per a reader's request, here's a picture similar to the aforementioned without Blogger compressing the spaces.

Thursday, October 04, 2007

Manage Removing Directories from a Stack with popd

I thought about adding the popd command verbiage to the last post (pushd), but I do like to keep these HOWTOs pithy. The pushd command was used to push directories onto a stack, so now I'm going to use the popd to remove directories from the stack. Again, I will use the dirs command to display the stack.

# csh
# dirs
/usr/local/popper / /export/home/esoft/xml/XMLDIR /opt/myapps/etc
# pwd
/usr/local/popper

Removes /usr/local/popper directory
# popd
/ /export/home/esoft/xml/XMLDIR /opt/myapps/etc
# dirs
/ /export/home/esoft/xml/XMLDIR /opt/myapps/etc
# pwd
# /

Removes /opt/myapps/etc directory
# popd +2
# / /export/home/esoft/xml/XMLDIR
# dirs
/ /export/home/esoft/xml/XMLDIR
# pwd
/

Manage Adding Directories to a Stack with pushd

While working on my FreeAdLists personal project at home, I decided it was time to better manage my pwd efforts. I quickly grew weary and impatient (exacerbated by the fact I'm fighting a nasty cold and acute rhinitis) of changing directories so many darn times, so I decided it was time to use a stack (stack starts with 0). Here are few examples of the pushd and dirs commands, which pushes a directory and displays a stack, respectively.

# csh
# cd /
# pushd `pwd`
/ /export/home/esoft/xml/XMLDIR /opt/apps/apps1
# pushd /usr/local/popper
# dirs
/usr/local/popper / /export/home/esoft/xml/XMLDIR /opt/apps/apps1
# pwd
/usr/local/popper

Switches between the first two directories paths
# pushd
/ /usr/local/popper /export/home/esoft/xml/XMLDIR /opt/apps/apps1
# dirs
/usr/local/popper / /export/home/esoft/xml/XMLDIR /opt/apps/apps1
# pwd
# /

Moves /export/home/esoft/xml/XMLDIR to the top of the stack
# pushd +2
/export/home/esoft/xml/XMLDIR / /usr/local/popper /opt/apps/apps1
# dirs
/export/home/esoft/xml/XMLDIR / /usr/local/popper /opt/apps/apps1
# pwd
/export/home/esoft/xml/XMLDIR