Linux Blog

Making Environment Variables Stick

Filed under: General Linux — at 5:00 am on Sunday, October 3, 2010

So, setting environment variables is a pretty easy task right? I thought so too but recently I was unable to read a variable I set from within a Tomcat application no matter what I tried. The problem turns out was easy to fix.

All I needed was to set an environment variable, I didn’t care who had access to it since it was just a path but whatever I did, it just wouldn’t stick. I quickly found out from a co-worker about a magical command that would have been a solution. Problem now was he couldn’t remember what it was. Well it turns out that it is a bash built in called source. Using this with the /etc/profile file as follows: “source /etc/profile” fixed the problem without a reboot.

Bulk Editing Text Files

Filed under: Shell Script Sundays — at 1:00 am on Sunday, May 10, 2009

A Co-Worker wanted to edit a number of files in a directory that contained a lot of files. Each file that needed to be edited contained a function that needed to be replaced. Since it was production data we did not want to do a backup and run a sed find and replace for all files and risk screwing something up we decided to use vi to edit a list of files. Here is what I came up with to do that:

vi `grep function\_name * -n |cut -d : -f 1 | uniq`

If it were me, I would not have wanted to type sed find and replaces and would have done something like this because I’m lazy and I like to live on the edge:

 grep function\_name * -n | cut -d : -f 1 | uniq | while read i; do cp $i $i-bak; sed 's/function_name/new_function_name/g' $i-bak > $i; done;

Rather than editing them with vi it makes a -bak file, and uses sed to replace function_name with new_function_name. It does this from the bak file into the original. Some may think it’s kind of scary not making a backup, but I figure the -bak file should be enough depending on the operation. Make a backup if you value your data though.