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

Recommended Answers

All 2 Replies

$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 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
commented: Good advice. +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.