954,116 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

file replacement not working!

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

#!/bin/bash
file_name=$1
old_templet=$2
new_templet=$3

for file in file_name
do
cat $file | sed "s/old_templet/new_templet/g" > $file.new
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

whizkidash
Newbie Poster
20 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

$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. ;-)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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:

#!/bin/bash
file_name=$1
old_templet=$2
new_templet=$3

if [ "$3" = "" ]; then
  echo "usage:  ${0} <file_name> <new_templet> <old_templet>"
  exit 1
fi

if ! test -f ./${file_name}; then
  echo "file not found"
  exit 1
fi

if test -f ./${file_name}.new; then
  rm ./${file_name}.new
fi

sed "s/${old_templet}/${new_templet}/g" ${file_name} >> ${file_name}.new
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You