Problem with a substitution script in Unix

October 10, 2010 at 14:02:56
Specs: Unix
#!/bin/sh
s1=$1
s2=$2
3=$3
shift
echo The first two parameters are $1 and $2;
echo The remaining parameters are for x in $* > $2;
if grep -l $1 $3;
then
mv $3 $3.bak;
sed s/s1/s2/g $3.bak > $3;
else
echo No changes needed.;
fi

I have created the above shellscript. I created 3 text files to test it all with many o's in the text. I run the test as follows:

> ./subst o x ./testfiles2/lolliPop.txt ./testfiles2/redRover.txt ./testfiles2/homeonRange.txt

The output:

./subst: 3=./testfiles2/lolliPop.txt: not found
The first two parameters are x and ./testfiles2/lolliPop.txt
./testfiles2/redRover.txt

The intent is for o and x to be assigned to $1 and $2 respectively and entered into the sed command in the place of s1 and s2. Each text file is supposed to be compared to $3 in the grep program for the presence of $1 then replacing $1 with $2 in each case where it occurs in and saved it in a .bak file for each .txt file. Obviously, it didn't work. what did I do wrong.


See More: Problem with a substitution script in Unix

Report •

#1
October 10, 2010 at 21:43:23
Here are my comments:
1) Variables must not be a number: s=$3 should be something like s3=$3
2) Semicolons are not required to terminate lines
3) Your strings should be enclosed in double quotes
4) To embed shell variables in sed, they must be surrounded like such: '"${s1}"'


#!/bin/sh
# UNTESTED!
s1=$1
s2=$2
s3=$3
shift


echo "The first two parameters are $1 and $2"
echo "The remaining parameters are for x in $*" > $2

if grep -l $1 $3
then
   mv $3 $3.bak
   sed 's/'"${s1}"'/'"${s2}"'/g' $3.bak > $3
else
   echo "No changes needed."
fi


Report •

#2
October 11, 2010 at 06:21:40
Thank you for responding.

I changed the script file to:

#!/bin/sh
s1=$1
s2=$2
s3=$3
shift
echo "The first two parameters are $1 and $2
echo "The remaining parameters are for x in $*" > $2
if grep -l $1 $3
then
mv $3 $3.bak
sed 's/'"{s1}"'/'"{s2}"'/g' $3.bak > $3
else
echo "No changes needed."
fi

The only part I didn't enter was
# UNTESTED!
which is commented out.

the test run commands and arguments were:

> ./subst o x ./testfiles2/lolliPop.txt ./testfiles2/homeonRange.txt ./testfiles2/redRover.txt

output is:

The first two parameters are x and ./testfiles2/lolliPop.txt
echo The remaining parameters are for x in x ./testfiles2/lolliPop.txt ./testfiles2/homeonRange.txt ./testfiles2/redRover.txt > ./testfiles2/lolliPop.txt
if grep -l x ./testfiles2/homeonRange.txt
then
mv ./testfiles2/homeonRange.txt ./testfiles2/homeonRange.txt.bak
sed 's/'{s1}'/'{s2}'/g' ./testfiles2/homeonRange.txt.bak > ./testfiles2/homeonRange.txt
else
echo No changes needed.
fi

It changed one file as desired, but the values of the echo statements were assigned to the other two files.


Report •

#3
October 11, 2010 at 07:18:01
Terminate the echo statement with a double quote. This:

echo "The first two parameters are $1 and $2

should be:

echo "The first two parameters are $1 and $2"


Report •
Related Solutions


Ask Question