sed抛出“替换命令中的错误标志”,但在package.json中工作正常

时间:2019-11-17 14:15:27

标签: bash sed

以下在package.json内部工作

https://github.com/facebook/react-native/issues/13198#issuecomment-302917321

"scripts": {
  "postinstall": "sed -i '' 's\/#import <RCTAnimation\\/RCTValueAnimatedNode.h>\/#import \"RCTValueAnimatedNode.h\"\/' ./node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h",
  ...rest scripts
}

但是,如果我将命令移至postinstall.sh并尝试执行,则会导致错误

postinstall.sh:

sed -i '' 's\/#import <RCTAnimation\\/RCTValueAnimatedNode.h>\/#import \"RCTValueAnimatedNode.h\"\/' ./node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h

1 个答案:

答案 0 :(得分:1)

原始命令比原来的要复杂,导致不必要的和令人困惑的转义。

"scripts": {
  "postinstall": "sed -i '' 's|#import <RCTAnimation/RCTValueAnimatedNode.h>|#import \"RCTValueAnimatedNode.h\"|' ./node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h",
  ...rest scripts
}

因此提取的sed命令应该简单地

sed -i '' 's|#import <RCTAnimation/RCTValueAnimatedNode.h>|#import "RCTValueAnimatedNode.h"|' \
  ./node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h

唯一的区别是您无需在替换文本中转义"

顺便说一句,使用sed -i通常意味着您只能使用流编辑器ed所基于的文件编辑器sed

printf '%s\n' \
  's|#import <RCTAnimation/RCTValueAnimatedNode.h>|#import "RCTValueAnimatedNode.h"|' \
  'wq' |
  ed ./node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h