Search and Replace Unix Shell Script

AI Thread Summary
The discussion revolves around a user attempting to write a search and replace script as part of a homework assignment. The user encounters an "end of file unexpected" error, expressing confusion due to their experience with compilers that provide clearer error messages. They seek guidance on the nature of the error and any tips for improvement. Key issues identified include missing quotes in the script, particularly in the error handling sections, which could lead to syntax errors. Suggestions include using an editor with syntax highlighting to help catch such mistakes. The script structure includes functions for error handling and usage instructions, but the user struggles with the actual implementation of the search and replace logic using `sed`. The discussion highlights the importance of careful syntax and the benefits of tools that enhance code readability.
hunter108
Messages
1
Reaction score
0
I'm trying to write what is basically a search and replace script. Full disclosure: This IS a homework assignment, and my attempt is below. It is supposed to have a usage statement, which I think is pretty solid so far. The problem is at the end where I do that actual searching and replacing. I'm just getting the 'end of file unexpected' error, which because i am spoiled in my young programming life with gcc, g++, and java compilers that neatly tell you where the problem is, I don't really know what is going wrong.

I think that error means it is looking for something it can't find, right? What? A hint in the right direction, or even a chastisement for being a newbie would be helpful.

Thanks in advance if anyone helps.




#!/bin/sh

# a shell function to print and error message and exit the script
error_and_die ()
{
echo "$@" >&2
exit 1

}
# a shell function to print and error message and a usage message and
# to exit the script
error_and_die_with_usage ()
{
echo "$@" >&2
usage
exit 1
}

# a shell function to print a usage message
usage ()
{
echo "
change-lines [-n] -s search string -r replace string files ...

-n do not backup the original file
-s search string the search for this string
-r replace string replace the search string with this string
-h print this message
" >&2
}


backup_file=TRUE
search_string=
replace_string=

while :
do
case $1 in
-n)
backup_file=FALSE
;;
-s)
if [ ! "$2" ]
then
error_and_die_with_usage "Search String not specified with -s"
fi
search_string="$2"
shift
;;
-r)
if [ ! "$2" ]
then
error_and_die_with_usage "Search String not specified with -r"
fi
replace_string="$2"
shift
;;
-h)
usage
exit 0
;;
-?)
error_and_die_with_usage "This should help!"
;;
*)
break
;;
esac
shift
done

if [ ! "$search_string" -a ! "$replace_string" -a $# -eq 0 ]
then
error_and_die_with_usage "No Search String, Replace String, or file to edit entered."
fi

if [ ! "$search_string" -a ! "$replace_string" ]
then
error_and_die_with_usage "No Search or Replace String entered.
fi

if [ ! "$search_string" -a $# -eq 0 ]
then
error_and_die_with_usage "No Search String or file to edit entered."
fi

if [ ! "$search_string" ]
then
error_and_die_with_usage "No Search String entered."
fi

if [ ! "$replace_string" -a $# -eq 0 ]
then
error_and_die_with_usage "No Replacement String or file to edit entered."
fi

if [ ! "$replace_string" ]
then
error_and_die_with_usage "No Replacement String entered."
fi

if [ $# -eq 0 ]
then
error_and_die_with_usage "No file to edit entered."
fi

# here is where I am pretty sure I am getting the error.
for file in $*
do
backup=$1
if ( backup_file == TRUE )
then
cp $backup $backup.keep
fi
sed 's/$search_string/$replace_string/g' $backup
shift
done

exit 0;
 
Technology news on Phys.org
You're missing a quote on one of your strings.

A piece of advice - use an editor with syntax highlighting. Doing so can help you avoid errors like that.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
2K
Replies
3
Views
2K
Replies
3
Views
3K
Replies
2
Views
2K
Replies
8
Views
2K
Replies
11
Views
2K
Replies
19
Views
4K
Replies
75
Views
6K
Back
Top