2016-10-19 6 views
0

sedコマンドを自動的にアセンブルして実行するプログラムを作成しようとしています。sed:1: "'/ regex/...":無効なコマンドコード'

次のような出力になり
fmt.Println("Running command: sed", args) 
program := exec.Command(programName, args...) 
output, err := program.CombinedOutput() 
fmt.Printf("%s", output) 
if err != nil { 
    fmt.Println(err) 
} 

(コマンドの100Sを生成:その後、私は完全なコマンドを実行するには、以下のコードスニペットを使用し

switch command { 
case "=", "d": 
    return fmt.Sprintf("'/%s/ %s'", regex, command) 
case "c", "a", "i": 
    return fmt.Sprintf("'/%s/ %s\\\n%s'", regex, command, phrase) 
case "s", "y": 
    return fmt.Sprintf("'%s/%s/%s/'", command, regex, phrase) 
default: 
    return "" 
} 

:私は、コマンドを生成するために、次のコードスニペットを使用しています、これは単なる1)である:

Running command: sed [-e '/([0a-z][a-z0-9]*,)+/ c\ 
abc said he""llo!!!\n 1 ' -e '/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)/ a\ 
0,0,0,156, aac' -e '/(s?)a.b,a\nb/ d' -f number_lines.txt -f eliminate_punctuation.txt -f delete_leading_trailing_whitespace.txt -f delete_last_ten_lines.txt -f eliminate_blanks.txt -f number_non_blank_lines.txt -f reverse_lines.txt -f strip.txt input1.txt input2.txt input3.txt] 

sed: 1: " '/([0a-z][a-z0-9]*,)+/ ...": invalid command code ' 
exit status 1 

奇妙だが、私はもちろんの角括弧なしで手によって生成されたコマンドを()を実行した場合どのような奇妙なのは、それだけで正常に動作していること!何が起きてる?

答えて

1

シェルが引用符を削除すると、exec.Commandは表示されません。したがって、sedには'のコマンドが渡されている可能性があります。一重引用符を使用せずにコマンドを試してください:

switch command { 
// remove single quotes from strings 
case "=", "d": 
    return fmt.Sprintf("/%s/ %s", regex, command) 
case "c", "a", "i": 
    return fmt.Sprintf("/%s/ %s\\\n%s", regex, command, phrase) 
case "s", "y": 
    return fmt.Sprintf("%s/%s/%s/", command, regex, phrase) 
default: 
    return "" 
} 
関連する問題