Search and Replace Unix Shell Script

In summary, the speaker is attempting to create a script that can search and replace text. They have included a usage statement and are having trouble with the actual search and replace function. They are unsure of why they are getting an "end of file unexpected" error and are seeking guidance. They have also requested advice for beginners. The expert summarizer advises the speaker to add a missing quote in one of their strings and suggests using an editor with syntax highlighting to avoid similar errors in the future.
  • #1
hunter108
1
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
  • #2
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.
 
  • #3



Hello there,

It looks like you have a solid start to your search and replace shell script. The 'end of file unexpected' error could mean a few things, but most likely it is because you have a syntax error in your code. In this case, you are missing a closing quotation mark on line 63 after "No Search or Replace String entered."

Additionally, there are a few other things that could be improved in your script. For example, you might want to consider using more descriptive variable names and using the double bracket syntax for conditional statements. Also, instead of using 'shift' to move through the arguments, you could use a 'for' loop to iterate through them.

Overall, it seems like you have a good understanding of the logic behind the script, but you may just need to work on the syntax and formatting. Keep practicing and don't be afraid to ask for help when you need it. Good luck with your assignment!
 

1. How does a search and replace script work in Unix shell?

A search and replace script in Unix shell works by using a combination of commands and regular expressions to find and replace specific patterns of text in a given file or directory.

2. What are the advantages of using a search and replace script in Unix shell?

One of the main advantages of using a search and replace script in Unix shell is that it allows for quick and efficient editing of large files or directories without having to manually search and replace each instance. It also allows for automation of repetitive tasks, saving time and effort.

3. Can a search and replace script be used for multiple files at once?

Yes, a search and replace script in Unix shell can be used to replace text across multiple files at once by using wildcards or specifying a directory. This can be particularly useful when making changes to a website or a project with multiple files.

4. What are some common commands used in a search and replace script in Unix shell?

Some common commands used in a search and replace script in Unix shell include "sed", which stands for stream editor and is used to perform the search and replace function, and "grep", which is used to search for specific patterns of text. Other commands such as "awk" and "cut" can also be used to manipulate text in a search and replace script.

5. Are there any potential drawbacks to using a search and replace script in Unix shell?

One potential drawback of using a search and replace script in Unix shell is that it can be complex and difficult to learn for those who are not familiar with command line interfaces. It also requires a good understanding of regular expressions, which can be challenging for some users. Additionally, it is important to double check and test the script before running it to avoid unintended changes to important files.

Similar threads

  • Programming and Computer Science
Replies
2
Views
621
  • Programming and Computer Science
Replies
3
Views
2K
Replies
16
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
Back
Top