file replacement not working!

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster

file replacement not working!

 
0
  #1
Nov 9th, 2009
Hi Team,

I am stuck in a very tight situation.. need your help

The below script does not work even please let me know where is the problem and rectify me.
Script as below:
replace.sh

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. file_name=$1
  3. old_templet=$2
  4. new_templet=$3
  5.  
  6. for file in file_name
  7. do
  8. cat $file | sed "s/old_templet/new_templet/g" > $file.new
  9. done

1)Here the file_name stands for file name
2)old_templet stands for old word to change
3)new_templet stands for new word

But when the script it gives me the below error:
./replace.sh one single oracle
cat: file_name: No such file or directory

Thanks for your patience..
hope 4 a speedy reply

cheers!
Whizkidash
Last edited by peter_budo; Nov 9th, 2009 at 5:34 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
1
  #2
Nov 9th, 2009
$file_name, not file_name
ditto for the other variables
use "sed -e" not just "sed"
and change $file.new to ${file}.new just to be safe

Edit: So many problems, so little text. ;-)
Last edited by masijade; Nov 9th, 2009 at 3:20 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,470
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast
 
1
  #3
Nov 9th, 2009
Masijade answered your question but i'd like to throw something else in there. You should add checks for your arguments on the command line and verify the file exists:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. file_name=$1
  3. old_templet=$2
  4. new_templet=$3
  5.  
  6. if [ "$3" = "" ]; then
  7. echo "usage: ${0} <file_name> <new_templet> <old_templet>"
  8. exit 1
  9. fi
  10.  
  11. if ! test -f ./${file_name}; then
  12. echo "file not found"
  13. exit 1
  14. fi
  15.  
  16. if test -f ./${file_name}.new; then
  17. rm ./${file_name}.new
  18. fi
  19.  
  20. sed "s/${old_templet}/${new_templet}/g" ${file_name} >> ${file_name}.new
Last edited by sknake; Nov 9th, 2009 at 5:17 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 716 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC