0

I have this shell script that is supposed to go through a file and replace its primitive types with other specific text (it's for an assignment).

The script is below:

#!/bin/bash 
#Goes through an entire file and replaces various primitive types with specific designations given at the end of each line of the file "PrimitiveDefs.h"

while read line
do

    #Replace all instances of "unsigned char" with "CS390_UCHAR"
    sed -i 's/\bunsigned char\b/CS390_UCHAR/g' $line
    #Replace all instances of "char" with "CS390_CHAR"
    sed -i 's/\bchar\b/CS390_CHAR/g' $line
    #Replace all instances of "unsigned short" with "CS390_UINT16"
    sed -i 's/\bunsigned short\b/CS390_UINT16/g' $line
    #Replace all instances of "short" with "CS390_INT16"
    sed -i 's/\bshort\b/CS390_INT16/g' $line
    #Replace all instances of "unsigned int" with "CS390_UINT32"
    sed -i 's/\bunsigned int\b/CS390_UINT32/g' $line
    #Replace all instances of "int" with "CS390_INT32"
    sed -i 's/\bint\b/CS390_INT32/g' $line
    #Replace all instances of "float" with "CS390_FLOAT32"
    sed -i 's/\bfloat\b/CS390_FLOAT32/g' $line
    #Replace all instances of "double" with "CS390_FLOAT64"
    sed -i 's/\bdouble\b/CS390_FLOAT64/g' $line
    #Replace all instances of "bool" with "CS390_BOOL"
    sed -i 's/\bbool\b/CS390_BOOL/g' $line 

done

Then, in the terminal, while in a directory with a bunch of files I want to edit with this script, I run:

find ./ -type f -print | ../wiatrak_cs390program3.sh

(assume my script is in the parent folder).

When I run this, the terminal gives me:

bash: line 1: ./Utility.h: Permission denied
bash: line 2: ./strrpl.cpp: Permission denied
bash: line 3: ./regressiontester.cpp: Permission denied
bash: line 4: ./Utility.cpp: Permission denied
bash: line 5: ./localconfig.h: Permission denied
bash: line 6: ./makefile: Permission denied
bash: line 7: ./PrimitiveDefs.h: Permission denied
bash: line 8: ./astring.h: Permission denied

All of these files in the directory I'm searching have the following permissions: -rw-rw-rw-

So I thought that maybe for some reason the files I'm reading and editing with the script might need execute permission (I'm not very knowledgeable when it comes to how permissions work), so in the folder I run chmod +x *, giving each file in the folder execute permission, and then I run find ./ -type f -print | ../wiatrak_cs390program3.sh again.

Well doing this when they have execute permission produces a TON of errors:

./Utility.h: line 3: $'\r': command not found
./Utility.h: line 7: $'\r': command not found
./Utility.h: line 15: $'\r': command not found
./Utility.h: line 16: syntax error near unexpected token `('
'/Utility.h: line 16: `const char* stringf        (const char* fmt, ...);
./strrpl.cpp: line 1: /bin: Is a directory
./strrpl.cpp: line 2: syntax error near unexpected token `('
'/strrpl.cpp: line 2: `/* void strrpl (char buffer[],int buffersize,const char* str1,const char* str2); */
./regressiontester.cpp: line 3: $'\r': command not found
./regressiontester.cpp: line 4: syntax error near unexpected token `('
'/regressiontester.cpp: line 4: `const char* emsg           (const char* fmt,...);
./Utility.cpp: line 4: $'\r': command not found
./Utility.cpp: line 6: $'\r': command not found
./Utility.cpp: line 7: using: command not found
./Utility.cpp: line 7: $'\r': command not found
./Utility.cpp: line 8: $'\r': command not found
./Utility.cpp: line 9: syntax error near unexpected token `('
'/Utility.cpp: line 9: `const char* stringf(const char* fmt, ...)
./localconfig.h: line 3: $'\r': command not found
./localconfig.h: line 5: $'\r': command not found
./localconfig.h: line 7: $'\r': command not found
./makefile: line 1: CC: command not found
./makefile: line 2: OBJS: command not found
./makefile: line 3: INCPATH: command not found
./makefile: line 4: OPTS: command not found
./makefile: line 5: $'\r': command not found
./makefile: line 6: OBJS: command not found
./makefile: line 6: regressiontest: command not found
./makefile: line 7: CC: command not found
./makefile: line 7: OBJS: command not found
./makefile: line 7: -o: command not found
./makefile: line 8: $'\r': command not found
./makefile: line 9: regressiontester.o: command not found
./makefile: line 10: CC: command not found
./makefile: line 10: OPTS: command not found
./makefile: line 10: INCPATH: command not found
: No such file or directory
./makefile: line 11: $'\r': command not found
./makefile: line 12: Utility.o: command not found
./makefile: line 13: CC: command not found
./makefile: line 13: OPTS: command not found
./makefile: line 13: INCPATH: command not found
: No such file or directory
./makefile: line 14: $'\r': command not found
./makefile: line 15: strrpl.o: command not found
./makefile: line 16: CC: command not found
./makefile: line 16: OPTS: command not found
./makefile: line 16: INCPATH: command not found
: No such file or directory
./makefile: line 17: $'\r': command not found
./makefile: line 18: $'clean:\r': command not found
./makefile: line 19: @echo: command not found
./makefile: line 20: @rm: command not found
./makefile: line 21: @rm: command not found
./makefile: line 22: @rm: command not found
./makefile: line 23: @rm: command not found
./makefile: line 24: @rm: command not found
./makefile: line 25: $'\r': command not found
./PrimitiveDefs.h: line 3: $'\r': command not found
./PrimitiveDefs.h: line 4: typedef: command not found
./PrimitiveDefs.h: line 4: $'\r': command not found
./PrimitiveDefs.h: line 5: typedef: command not found
./PrimitiveDefs.h: line 5: $'\r': command not found
./PrimitiveDefs.h: line 6: typedef: command not found
./PrimitiveDefs.h: line 6: $'\r': command not found
./PrimitiveDefs.h: line 7: $'\r': command not found
./PrimitiveDefs.h: line 8: typedef: command not found
./PrimitiveDefs.h: line 8: $'\r': command not found
./PrimitiveDefs.h: line 9: typedef: command not found
./PrimitiveDefs.h: line 9: $'\r': command not found
./PrimitiveDefs.h: line 10: $'\r': command not found
./PrimitiveDefs.h: line 11: typedef: command not found
./PrimitiveDefs.h: line 11: $'\r': command not found
./PrimitiveDefs.h: line 12: typedef: command not found
./PrimitiveDefs.h: line 12: $'\r': command not found
./PrimitiveDefs.h: line 13: $'\r': command not found
./PrimitiveDefs.h: line 14: typedef: command not found
./PrimitiveDefs.h: line 14: $'\r': command not found
./PrimitiveDefs.h: line 15: typedef: command not found
./PrimitiveDefs.h: line 15: $'\r': command not found
./PrimitiveDefs.h: line 16: $'\r': command not found
./PrimitiveDefs.h: line 17: typedef: command not found
./PrimitiveDefs.h: line 17: $'\r': command not found
./PrimitiveDefs.h: line 18: $'\r': command not found
./astring.h: line 1: $'\r': command not found
./astring.h: line 4: $'\r': command not found
./astring.h: line 7: $'\r': command not found
./astring.h: line 8: namespace: command not found
./astring.h: line 9: $'{\r': command not found
./astring.h: line 10: class: command not found
./astring.h: line 11: $'{\r': command not found
./astring.h: line 12: $'public:\r': command not found
./astring.h: line 13: syntax error near unexpected token `:'
'/astring.h: line 13: `    string() : L(-1), S((char*) "") {}

It seems like it's trying to actually execute the commands than read and edit them. What should I do here?

2
  • 1
    Are you sure that's what your script file says, and especially sure that find ./ -type f -print | ../wiatrak_cs390program3.sh is how you ran it? It looks a lot more like the result of find . -type f | bash. Apr 19, 2019 at 1:16
  • What are the permissions on the directory itself? Do you have write access to it?
    – nohillside
    Apr 19, 2019 at 8:45

1 Answer 1

0

Would sed -i not rather expect files and not lines? You would not have to iterate through the lines at the first place. You could write a script that only includes the actual replacements and invoke your script running find -f .. -exec sed -i - f yourscript.sh {} \; Your current script will simply run sed with the entire line as argument. If the line contains special symbols like # or ; your script would be broken.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .