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のみを実行します。行に#または;同じ特殊記号が含まれていると、スクリプトが壊れます。

おすすめ記事