The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
dynamic editing using shell script habzone2007 Shell Programming and Scripting 1 05-14-2008 11:55 PM
editing a file via a shell script ?? jimmyc Shell Programming and Scripting 2 03-28-2008 12:21 PM
Editing file rahul303 Shell Programming and Scripting 4 09-23-2007 11:49 PM
Editing the File using Awk awk_beginner Shell Programming and Scripting 3 04-06-2007 11:43 AM
Editing a file in a shell script jowpup UNIX for Dummies Questions & Answers 3 02-08-2005 04:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-28-2003
Ypnos Ypnos is offline
Registered User
  
 

Join Date: May 2003
Location: Lake District, U.K.
Posts: 42
Editing a file using a script

Hi

I have about 10 config files belonging to software that runs on SCO UNIX. These files contain, amongst many other things, a path which points to the software locations. We normally have to change them manually every time the software is coppied to a new location and when you gotta do a few times a day, it becomes quite laboreous.

I would like to know how to change all these paths using a single script. I'm hoping to have the user enter the 2 different paths once and let the script find the paths in these files (line numbers for the paths vary from file to file) and over write them with the new ones.

It is worth noting that the only alterations that can be made to the config files are the path names. Anything else will cause the software that reads them, to disappear up its own backside.

I hope I'm being clear and I look forward to some help
  #2 (permalink)  
Old 05-28-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
When you deploy your software, do you also have a common directory structure associated with it? For instance, if I were to deploy to /home/my-user, would there be common directories under /home/my-user such as sh, log, bin, files? If so, you could develop a common approach to deployment by having your user enter in the common path to the directory.

For example:

#My Deployment Script

echo "Enter Deployment Directory"
read DEPLOY_DIR

if [ ! -d $DEPLOY_DIR ]
then
echo "$DEPLOY_DIR Not A Directory"
exitt 1
fi

export UNIX_ENV=$DEPLOY_DIR

. $UNIX_ENV/envFile #Source in the environment file

#envFile
#Add all path variables to the env file so
#that you never have to change a path #again!

export SH_PATH=$UNIX_ENV/sh
export BIN_PATH=$UNIX_ENV/bin
export LOG_PATH=$UNIX_ENV/log

If this is not an option, then you can used sed to change all of the paths based upon user input. There will be a little effort to ensure your changing the correct line in each file. This can easly be overcome by tagging the line in each file that you want to have changed. Make sure that before you change your original file that you make a backup!
  #3 (permalink)  
Old 05-28-2003
Ypnos Ypnos is offline
Registered User
  
 

Join Date: May 2003
Location: Lake District, U.K.
Posts: 42
Thanks for your reply google.

Unfortunately I'm only a junior manager here and making changes to the software code itself isn't an option in my position unfortunately...

the second way you propose is more up my street, but I'm not sure how to use the sed command. I've been working with UNIX for 2 days now

I have written the script that coppies the software and the 10 files in queswtion, but it's a royal pain to edit these files every time. See my problem?
  #4 (permalink)  
Old 05-28-2003
Ypnos Ypnos is offline
Registered User
  
 

Join Date: May 2003
Location: Lake District, U.K.
Posts: 42
Cool

Edit: replied to wrong thread
  #5 (permalink)  
Old 05-28-2003
smehra smehra is offline
Registered User
  
 

Join Date: Oct 2002
Posts: 5
Use sed

As far as I understand sed can meet your requirements or you can even do a global replace in vi.
Open the file using vi ,type escape followed by : then type
s/Oldpath/newpath/g .This should change the path through out the file.Also note that if your path contains "/" ,then you will have to escape it using a "\"
For example:

s/old_dir1\/old_dir2\/old_dir3/new_dir1\/new_dir2\/new_dir3/g

alternately you can use sed
sed 's/old_dir1\/old_dir2\/old_dir3/new_dir1\/new_dir2\/new_dir3/g' filename >new_filename
Also if you want to pass the pathnames as argument then you can type a script containing sed which takes the path as arguments
I haven't tried the last solution,but I don't see any major problem with it.

Hope this helps
Saurabh
  #6 (permalink)  
Old 05-28-2003
Ypnos Ypnos is offline
Registered User
  
 

Join Date: May 2003
Location: Lake District, U.K.
Posts: 42
Wouldn't you have to edit the script with the old and new directories every time? (which kinda defeats the purpose)

There are 152 variations (groups) of these 10 files knocking about in several servers and you choose at random a group of 10 to modify. None of the 152 variations are the same...

Just to throw a spanner in the works and confuse everyone
  #7 (permalink)  
Old 05-28-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Here is the problem as I understand it.
1. You have a ton of scripts that have hard coded paths
2. Each time a new deployment occurs, you must edit the paths
3. Your sick of manually editing the paths!

Proposal:

-- Create a script that anyone can use that will do the following.
1. Copy the files in questions (deploy them were they need to be)
2. For each file and path that needs a change, Prompt the user with the existing directory paths. Ask user if this is correct (if so, exit) if not ask user to specify the path.
3. Validate the users input.
4. Backup the original config files that you are about to change.
5. Use sed to change the path by replacing the old path in the file with what the user just requested.
6. Work on a more robust deployment plan

Here is a link to some sed info to get you started

Last edited by google; 05-28-2003 at 12:20 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0