I am not sure what I am doing wrong here. The code should work fine. I have been making small changes insuring that each new bit works. Now running my sed through multiple files I am getting incorrect output. Any help and instruction would be greatly appreciated.
The problem -
Generalize your sub2 script, producing a new script sub3 that will apply a substitution to any number of files given on the command line. For example
~/UnixCourse/scriptAsst/sub3 foo bar myFile1.txt myFile2.txt
myFile3.txt
should apply the same substitution throughout each of the three files named there.
Give the command:
~cs252/bin/scriptAsst.pl
to complete the assignment.
My code -
#!/bin/sh
p1="$1"
shift
p2="$1"
shift
p3="$1"
shift
for FILE in "$@"
do
echo $p1
echo $p2
echo $p3
#echo "$p1" | sed -e 's/\([*.[^$]\)/\\\1/g' > temp
#p1="`cat temp`"
#rm temp
#echo $p1
sed "s/$p1/$p2/g" "$p3" > temp.out
mv temp.out "$p3"
done
Results -
sub3 produced incorrect output on test 28: /home/cbliss/UnixCourse/scriptAsst/sub3 '3' '9' '_aardvark.cpp'
I do not know what test 28 inputs or the contents of _aadvark.cpp. We run a given command and it automatically tests our script. If it passes a test we see:
1...
2...
3... etc.
Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Old Dominion University - Norfolk, Va USA
Professor Steven Zeil
CS 252 - An Introduction to Unix for Programmers
Did it look like you were getting the correct set of arguments to give to sed when you tried my sample script? (Did you try my sample script before changing it?)
Why did you change:
for FILE in "$@"
in your original code and in my example to:
for FILE in *
The shell expands "$@" to a list of the quoted arguments remaining on your command line after the shift commands removed your first two command-line arguments. The shell expands * to a list of files in the current directory.
#!/bin/sh
p1="$1"
shift
p2="$1"
shift
for FILE in "$@"
do
echo $p1
echo $p2
echo "Process $FILE using $p1 and $p2..."
#echo "$p1" | sed -e 's/\([*.[^$]\)/\\\1/g' > temp
#p1="`cat temp`"
#rm temp
#echo $p1
sed "s/$p1/$p2/g" $FILE > temp.out
mv temp.out $FILE
done
Running
~cs252/bin/scriptAsst.pl
This is the output -
Checking sub3...
28...
29...
30...
31...
sed: can't read _king: No such file or directory
sed: can't read cobra.dat: No such file or directory
mv: target `cobra.dat' is not a directory
32...
33...
sed: can't read _king: No such file or directory
sed: can't read cobra.dat: No such file or directory
mv: target `cobra.dat' is not a directory
sub3 produced incorrect output on test 33: /home/cbliss/UnixCourse/scriptAsst/sub3 'Unix' 'U N I X' '_king cobra.dat' '_ne$wt.foo'
In your original script (which was passing many more of the tests), you had:
sed "s/$p1/$p2/g" "$p3" > temp.out
mv temp.out "$p3"
Now you have:
sed "s/$p1/$p2/g" $FILE > temp.out
mv temp.out $FILE
Other than changing p3 to FILE, what else changed?
Did you notice that the error reports you got included:
sed: can't read _king: No such file or directory
sed: can't read cobra.dat: No such file or directory
mv: target `cobra.dat' is not a directory
and
sub3 produced incorrect output on test 33: /home/cbliss/UnixCourse/scriptAsst/sub3 'Unix' 'U N I X' '_king cobra.dat' '_ne$wt.foo'
Do you see that the diagnostic messages from sed and mv refer to parts of one of the parameters passed to your script? Could changing quotes around arguments passed to sed and mv have an effect leading to the errors you're seeing?
Hey is this thread still open, i ran the code and am having trouble with line 37 of the file going through, am i allowed to post my code and recieve help?