Search and replace script - half functional
Posted: Thu Aug 01, 2013 12:32 pm
I am currently working on archiving .WMA recordings to DVD, and need to replace a string of text, including potential spaces, with a different string of text.
If I just wanted to do one search and replace, my code works, but if I want to change the search term, it breaks to not find anything.
Here is my code:
When run, the initial search finds files in the current directory, lists them, and then repeats. If I list again, it works fine, if I try to update the search term, it does not find anything, if I update the search term back to the original one used on the command line, it still finds nothing.
Thoughts?
If I just wanted to do one search and replace, my code works, but if I want to change the search term, it breaks to not find anything.
Here is my code:
Code: Select all
#!/bin/bash
ARGS=2
E_WRONGARGS=65
END_CONDITION=1
if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.
then
echo "Usage: `basename $0` "\'text to replace\'" community-name"
exit $E_WRONGARGS
fi
REPLACE_TEXT=$1
COMMUNITY_NAME=$2
OLDIFS=$IFS
function justlist {
IFS=$(echo -en "\n\b")
OLDREPLACE=$REPLACE_TEXT
for filename in $( ls *"$REPLACE_TEXT"* ); do newname=`echo "$filename" | sed -e 's/'"$REPLACE_TEXT"'/'_"$COMMUNITY_NAME"'/g'`; echo "Change list: $filename ---> $newname"; done
REPLACE_TEXT=$OLDREPLACE
IFS=$OLDIFS
}
function whizbang {
IFS=$(echo -en "\n\b")
for filename in $( ls ); do newname=`echo "$filename" | sed -e 's/'"$REPLACE_TEXT"'/'_"$COMMUNITY_NAME"'/g'`; echo "Now changing: $filename ---> $newname"; mv "$filename" "$newname"; done
IFS=$OLDIFS
}
quitguage=0
until [ "$quitguage" = "$END_CONDITION" ]
do
act=""
echo -n "What would you like to do? {list|change|update|quit} "
read act
case $act in
list|List|LIST|l|L)
justlist
;;
change|Change|CHANGE|c|C)
whizbang
;;
quit|Quit|QUIT|q|Q)
echo "Thank you for using this utility!"
echo
quitguage=1
exit 0
;;
update|Update|UPDATE|u|U)
echo -n "Please enter the new search term including single quotes:"
read upd
REPLACE_TEXT=$upd
;;
*) echo "Please select one of the options provided."
echo "To update the search term, choose update."
;;
esac
done
exit 0
Thoughts?