sed Shell 脚本无法正常工作

sed Shell 脚本无法正常工作

我有这个 shell 脚本,它应该遍历一个文件并将其原始类型替换为其他特定文本(用于作业)。

脚本如下:

#!/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。如果该行包含特殊符号,如 # 或 ;你的脚本就会被破坏。

相关内容