sed 쉘 스크립트가 제대로 작동하지 않습니다

sed 쉘 스크립트가 제대로 작동하지 않습니다

파일을 반복하고 원래 유형을 다른 특정 텍스트(작업용)로 바꾸는 쉘 스크립트가 있습니다.

스크립트는 다음과 같습니다.

#!/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

그런 다음 터미널에서 이 스크립트를 사용하여 여러 파일이 포함된 디렉터리를 편집하려면 다음을 실행합니다.

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

(내 스크립트가 상위 폴더에 있다고 가정).

이 명령을 실행하면 터미널에 다음이 표시됩니다.

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

내가 검색 중인 디렉터리에 있는 모든 파일에는 다음과 같은 권한이 있습니다. -rw-rw-rw-

그래서 어떤 이유로 스크립트로 읽고 편집하는 파일에 실행 권한이 필요할 수 있다고 생각했습니다(권한이 어떻게 작동하는지 잘 모르겠습니다). 그래서 실행 중인 폴더에서 chmod +x *각 파일 폴더에 실행 권한을 부여했습니다. 권한을 부여한 다음 find ./ -type f -print | ../wiatrak_cs390program3.sh다시 실행합니다.

실행 권한이 있을 때 이 작업을 수행하면 수많은 오류가 발생합니다.

./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*) "") {}

명령을 읽고 편집하는 대신 실제로 명령을 실행하려는 것처럼 보입니다. 여기서 무엇을 해야 합니까?

답변1

sed -i는 줄이 아닌 파일을 기대하지 않습니까? 먼저 행을 반복할 필요는 없습니다. 실제 대체 항목만 포함하는 스크립트를 작성하고 find -f .. -exec sed -i - f yourscript.sh {} \; 현재 스크립트는 전체 줄을 인수로 사용하여 sed만 실행합니다. 줄에 # 또는 ; 같은 특수 기호가 포함되어 있으면 스크립트가 깨집니다.

관련 정보