0

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

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by sknake
1

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

Edited by masijade: n/a

1

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

Edited by sknake: n/a

Votes + Comments
Good advice.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.