Posts

Showing posts from November, 2013

Checking environment variable in shell scripts

Found couple of ways, the common one :

My test scripts :

#!/bin/sh

if [ -n $JJ ] ; then
echo "-n1"
fi

if [ -z $JJ ] ; then
echo "-z1"
fi

JJ=""

if [ -n $JJ ]; then
echo "-n2"
fi

if [ -z $JJ ] ; then
echo "-z2"
fi

JJ="jcrys26"

if [ -n $JJ ]; then
echo "-n3"
fi

if [ -z $JJ ]; then
echo "-z3"
fi
The result is ...

-n1
-z1
-n2
-z2
-n3


-z to test if a string is empty.
-n to test is a string is not empty.

What about if a string is undefined? And the result really confused me. I have checked the environment variable, I don't have one with name of "JJ".

Reference : http://linuxcommand.org/wss0090.php

Have kept this in draft for a long long time due to the -n result. So.... A quick conclusion is, use the -z. Use -n at your own risk. To revisit this next time, if I still remember. :P



"Rename" a user in Linux system

I tried this in Ubuntu 12.04 LTS. Run these commands as Root in a terminal.

To change a user name.

usermod -l<new_user_id> <old_user_id>
To change the home directory

usermod -m -d /home/<new_user_id> <new_user_id>
The above 2 can be combined into a command.

usermod -l <new_user_id> -m -d /home/<new_user_id> <old_user_id>
* I didn't tried this though. :P

Next, change the content of the following files. Edit the content by replacing <old_user_id> to <new_user_id>.

/etc/hosts
/etc/hostname
What is next? Yes, the user name. This can be change via  User Accounts. Click on the upper right people icon and open the User Accounts GUI. The name can be changed via that interface.

After logout, you'll see the new_user_id account is ready to be used. :)

Note, if you are seeing error prompting unable to change the user_id, you'll need to login to the system as Root, not to use sudo su as Root.

Reference : http://www.ubuntututorials.com/c…