I am writing one shell script which finds a given text in the files and replace that from a specified path and after replacing the text, Rename that file with with the given word. I am getting an error of permission denied while using the sed. My script looks like this

`echo "Please Insert the path of the folder"
 read input_variable

    read -p "You entered: $input_variable is correct y/n  " yn

    read -p "Enter the word to find = " word
    read -p "Enter word to replace = " replace
    case $yn in
        [Yy]* ) find "${input_variable}" -type f -iname "${word}.*" | while read filename; do "`echo "${filename}" | sed -i 's/$word/$replace/g' ${filename}| sed -i 's/\$word/\$replace/' ${filename}`"; done ;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac`

I am getting the below error

bulk_rename.sh: 34: bulk_rename.sh: : Permission denied

Any suggestions ?

After the suggestion by @vijay updated the script

echo "Please Insert the path of the folder"
read input_variable

read -p "You entered: $input_variable is correct y/n  " yn

read -p "Enter the word to find = " word
read -p "Enter word to replace = " replace
case $yn in
    [Yy]* ) find "${input_variable}" -type f -iname "${word}.*" | while read filename; do 
    perl -pi -e 's/$word/$replace' ${filename}
    mv ${filename} $word;   done;;

    [Nn]* ) exit;;
    * ) echo "Please answer yes or no.";;
esac

now I am getting the following


Substitution replacement not terminated at -e line 1

This is what i get when I chmod and displays the output

abc@PC-DEV-41717:~/Documents/blog$ chmod +x bulk_rename.sh ; /bin/ls -l bulk_rename.sh
chmod +x bulk_rename.sh ; /bin/ls -l bulk_rename.sh
+ chmod +x bulk_rename.sh
+ /bin/ls -l bulk_rename.sh
-rwxrwxr-x 1 abc abc 1273 Aug  1 16:51 bulk_rename.sh
有帮助吗?

解决方案

Finally I come with my solution of my problem using the SED and with the help of this question that I have asked Question

echo "Please Insert the path of the folder"
read input_variable

read -p "You entered: $input_variable is correct y/n  " yn

read -p "Enter the word to find = " word
read -p "Enter word to replace = " replace
case $yn in
    [Yy]* ) grep -r -l "$word" $input_variable  | while read file; do echo $file; echo $fname; sed -i "s/\<$word\>/$replace/g" $file ; done; find "$input_variable" -type f -name "$word.*" | while read file; do dir=${file%/*};  base=${file##*/}; noext=${base%.*}; ext=${base:${#noext}}; newname=${noext/"$word"/"$replace"}$ext; echo mv "$file" "$dir/$newname"; done;;
    [Nn]* ) exit;;
    * ) echo "Please answer yes or no.";;
esac

其他提示

I guess you are making it complex: why not make it simple with two simple satements. Its up to you how you use the below statements for your purpose:

perl -pi -e 's/wordtofind/wordtoreplace' your_file #for replacing the word in the file

mv your_file wordtoreplace  #for renaming the file

Change

   perl -pi -e 's/$word/$replace' ${filename}

To

   perl -pi -e "s/$word/$replace/" ${filename}
 --------------^----------------^^--------

The error msg indicates a missing `/' char.


Also, so what error do you know get with your original code?

Note that the you'll need dbl-quotes surrounding your sed, just as in perl, so the shell can substitute in the values. i.e.

..... | sed -i "s/$word/$replace/g"
     ----------^------------------^

This assumes there are not naughty characters, especially / inside of $word or $replace.

IHTH

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top