$‘\r‘: command not found的解决方法

当在Linux系统中运行在Windows下编写的Shell脚本时,可能会遇到‘$' ': command not found’的错误,原因是Windows文件行尾使用 ,而Linux使用 。解决此问题,可以使用dos2unix命令将文件格式转换为Unix格式。在CentOS系统中,通过‘yum install -y dos2unix’安装,在Ubuntu系统中,使用‘apt-get install -y dos2unix’安装。之后运行‘dos2unix build.sh’进行转换,成功后会显示转换信息。
问题说明

在Linux系统中,运行Shell脚本,出现了如下错误:

build.sh: line 1: $'\r': command not found

出现这样的错误,是因为Shell脚本在Windows系统编写时,每行结尾是\r\n,而在Linux系统中行每行结尾是\n,所以在Linux系统中运行脚本时,会认为\r是一个字符,导致运行错误。

问题解决
  • 安装 dos2unix
centos系统:yum install -y dos2unix

ubuntu系统:apt-get -y install dos2unix 
  • 格式转换
# dos2unix build.sh

出现以下字符,说明格式转换成功
dos2unix: converting file one-more.sh to Unix format ...
命令讲解
  • dos2unix

dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令

Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n

dos2unix命令其实就是将文件中的\r\n 转换为\n

命令语法:

dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
  • unix2dos

而unix2dos则是和dos2unix互为孪生的一个命令

它是将Linux&Unix格式文件转换为Windows格式文件的命令

命令语法:

unix2dos [options] [-c convmode] [-o file ...] [-n infile outfile ...]
/eap/.clang-format: line 1: BasedOnStyle:: command not found /eap/.clang-format: line 2: Language:: command not found /eap/.clang-format: line 3: AccessModifierOffset:: command not found /eap/.clang-format: line 4: AlignAfterOpenBracket:: command not found /eap/.clang-format: line 5: AlignConsecutiveAssignments:: command not found /eap/.clang-format: line 6: AlignConsecutiveDeclarations:: command not found /eap/.clang-format: line 7: AlignOperands:: command not found /eap/.clang-format: line 8: AlignTrailingComments:: command not found /eap/.clang-format: line 9: AllowAllArgumentsOnNextLine:: command not found /eap/.clang-format: line 10: AllowAllParametersOfDeclarationOnNextLine:: command not found /eap/.clang-format: line 11: AllowShortFunctionsOnASingleLine:: command not found /eap/.clang-format: line 12: AllowShortIfStatementsOnASingleLine:: command not found /eap/.clang-format: line 13: AlwaysBreakAfterDefinitionReturnType:: command not found /eap/.clang-format: line 14: AlwaysBreakAfterReturnType:: command not found /eap/.clang-format: line 15: AlwaysBreakBeforeMultilineStrings:: command not found /eap/.clang-format: line 16: AlwaysBreakTemplateDeclarations:: command not found /eap/.clang-format: line 17: BinPackArguments:: command not found /eap/.clang-format: line 18: BinPackParameters:: command not found /eap/.clang-format: line 19: $'BraceWrapping:\r': command not found /eap/.clang-format: line 20: AfterCaseLabel:: command not found /eap/.clang-format: line 21: AfterClassKeyword:: command not found /eap/.clang-format: line 22: AfterControlStatement:: command not found /eap/.clang-format: line 23: AfterEnumKeyword:: command not found /eap/.clang-format: line 24: AfterFunctionKeyword:: command not found /eap/.clang-format: line 25: AfterNamespaceKeyword:: command not found /eap/.clang-format: line 26: AfterStructKeyword:: command not found /eap/.clang-format: line 27: AfterUnionKeyword:: command not found /eap/.clang-format: line 28: AfterCXXCatchKeyword:: command not found /eap/.clang-format: line 29: AfterElse:: command not found /eap/.clang-format: line 30: BeforeElse:: command not found /eap/.clang-format: line 31: BeforeWhile:: command not found /eap/.clang-format: line 32: BreakBeforeBinaryOperators:: command not found /eap/.clang-format: line 33: BreakBeforeBraces:: command not found /eap/.clang-format: line 34: BreakBeforeTernaryOperators:: command not found /eap/.clang-format: line 35: BreakConstructorInitializersBeforeComma:: command not found /eap/.clang-format: line 36: BreakInheritanceListBeforeComma:: command not found /eap/.clang-format: line 37: BreakInheritanceListBeforeColon:: command not found /eap/.clang-format: line 38: BreakStringLiterals:: command not found /eap/.clang-format: line 39: ColumnLimit:: command not found /eap/.clang-format: line 40: CommentPragmas:: command not found /eap/.clang-format: line 41: ConstructorInitializerAllOnOneLineOrOnePerLine:: command not found /eap/.clang-format: line 42: ConstructorInitializerIndentWidth:: command not found /eap/.clang-format: line 43: ContinuationIndentWidth:: command not found /eap/.clang-format: line 44: Cpp11BracedListStyle:: command not found /eap/.clang-format: line 45: DerivePointerAlignment:: command not found /eap/.clang-format: line 46: DisableFormat:: command not found /eap/.clang-format: line 47: ExperimentalAutoDetectBinPacking:: command not found /eap/.clang-format: line 48: FixNamespaceComments:: command not found /eap/.clang-format: line 49: $'ForEachMacros:\r': command not found /eap/.clang-format: line 50: -: command not found /eap/.clang-format: line 51: -: command not found /eap/.clang-format: line 52: -: command not found /eap/.clang-format: line 53: $'IncludeCategories:\r': command not found /eap/.clang-format: line 54: -: command not found /eap/.clang-format: line 55: Priority:: command not found /eap/.clang-format: line 56: -: command not found /eap/.clang-format: line 57: Priority:: command not found /eap/.clang-format: line 58: -: command not found /eap/.clang-format: line 59: Priority:: command not found /eap/.clang-format: line 60: IncludeIsMainRegex:: command not found /eap/.clang-format: line 61: IndentCaseLabels:: command not found /eap/.clang-format: line 62: IndentCaseBlocks:: command not found /eap/.clang-format: line 63: IndentGotoLabels:: command not found /eap/.clang-format: line 64: IndentPPDirectives:: command not found /eap/.clang-format: line 65: IndentWidth:: command not found /eap/.clang-format: line 66: IndentWrappedFunctionNames:: command not found /eap/.clang-format: line 67: JavaScriptQuotes:: command not found /eap/.clang-format: line 68: JavaScriptRegex:: command not found /eap/.clang-format: line 69: KeepEmptyLinesAtTheStartOfBlocks:: command not found /eap/.clang-format: line 70: MacroBlockBegin:: command not found /eap/.clang-format: line 71: MacroBlockEnd:: command not found /eap/.clang-format: line 72: MaxEmptyLinesToKeep:: command not found /eap/.clang-format: line 73: NamespaceIndentation:: command not found /eap/.clang-format: line 74: ObjCBlockIndentWidth:: command not found /eap/.clang-format: line 75: ObjCSpaceAfterProperty:: command not found /eap/.clang-format: line 76: ObjCSpaceBeforeProtocolList:: command not found /eap/.clang-format: line 77: PenaltyBreakAssignment:: command not found /eap/.clang-format: line 78: PenaltyBreakBeforeFirstCallParameter:: command not found /eap/.clang-format: line 79: PenaltyBreakComment:: command not found /eap/.clang-format: line 80: PenaltyBreakFirstLessLess:: command not found /eap/.clang-format: line 81: PenaltyBreakString:: command not found /eap/.clang-format: line 82: PenaltyBreakTemplateDeclaration:: command not found /eap/.clang-format: line 83: PenaltyExcessCharacter:: command not found /eap/.clang-format: line 84: PenaltyReturnTypeOnItsOwnLine:: command not found /eap/.clang-format: line 85: PointerAlignment:: command not found /eap/.clang-format: line 86: ReflowComments:: command not found /eap/.clang-format: line 87: SortIncludes:: command not found /eap/.clang-format: line 88: SortUsingDeclarations:: command not found /eap/.clang-format: line 89: SpaceAfterCStyleCast:: command not found /eap/.clang-format: line 90: SpaceAfterLogicalNot:: command not found /eap/.clang-format: line 91: SpaceAfterTemplateKeyword:: command not found /eap/.clang-format: line 92: SpaceBeforeAssignmentOperators:: command not found /eap/.clang-format: line 93: SpaceBeforeCaseColon:: command not found /eap/.clang-format: line 94: SpaceBeforeCpp11BracedList:: command not found /eap/.clang-format: line 95: SpaceBeforeCtorColon:: command not found /eap/.clang-format: line 96: SpaceBeforeInheritanceColon:: command not found /eap/.clang-format: line 97: SpaceBeforeKeywords:: command not found /eap/.clang-format: line 98: SpaceBeforeParens:: command not found /eap/.clang-format: line 99: SpaceBeforeRangeBasedForLoopColon:: command not found /eap/.clang-format: line 100: SpaceInEmptyParentheses:: command not found /eap/.clang-format: line 101: SpacesBeforeTrailingComments:: command not found /eap/.clang-format: line 102: SpacesInAngles:: command not found /eap/.clang-format: line 103: SpacesInContainerLiterals:: command not found /eap/.clang-format: line 104: SpacesInCStyleCastParentheses:: command not found /eap/.clang-format: line 105: SpacesInParentheses:: command not found /eap/.clang-format: line 106: SpacesInSquareBrackets:: command not found /eap/.clang-format: line 107: Standard:: command not found /eap/.clang-format: line 108: $'StatementMacros:\r': command not found /eap/.clang-format: line 109: -: command not found /eap/.clang-format: line 110: -: command not found /eap/.clang-format: line 111: -: command not found /eap/.clang-format: line 112: -: command not found /eap/.clang-format: line 113: -: command not found /eap/.clang-format: line 114: -: command not found /eap/.clang-format: line 115: -: command not found /eap/.clang-format: line 116: -: command not found /eap/.clang-format: line 117: -: command not found /eap/.clang-format: line 118: -: command not found /eap/.clang-format: line 119: -: command not found /eap/.clang-format: line 120: -: command not found /eap/.clang-format: line 121: -: command not found /eap/.clang-format: line 122: -: command not found /eap/.clang-format: line 123: -: command not found /eap/.clang-format: line 124: -: command not found /eap/.clang-format: line 125: -: command not found /eap/.clang-format: line 126: -: command not found /eap/.clang-format: line 127: -: command not found /eap/.clang-format: line 128: -: command not found /eap/.clang-format: line 129: -: command not found /eap/.clang-format: line 130: -: command not found /eap/.clang-format: line 131: -: command not found /eap/.clang-format: line 132: -: command not found /eap/.clang-format: line 133: -: command not found /eap/.clang-format: line 134: -: command not found /eap/.clang-format: line 135: -: command not found /eap/.clang-format: line 136: -: command not found /eap/.clang-format: line 137: -: command not found /eap/.clang-format: line 138: -: command not found /eap/.clang-format: line 139: -: command not found /eap/.clang-format: line 140: -: command not found /eap/.clang-format: line 141: -: command not found /eap/.clang-format: line 142: -: command not found /eap/.clang-format: line 143: -: command not found /eap/.clang-format: line 144: -: command not found /eap/.clang-format: line 145: -: command not found /eap/.clang-format: line 146: -: command not found /eap/.clang-format: line 147: -: command not found /eap/.clang-format: line 148: -: command not found /eap/.clang-format: line 149: -: command not found /eap/.clang-format: line 150: -: command not found /eap/.clang-format: line 151: -: command not found /eap/.clang-format: line 152: -: command not found /eap/.clang-format: line 153: -: command not found /eap/.clang-format: line 154: -: command not found /eap/.clang-format: line 155: -: command not found /eap/.clang-format: line 156: TabWidth:: command not found /eap/.clang-format: line 157: UseCRLF:: command not found /eap/.clang-format: line 158: UseTab:: command not found /eap/.clang-format: line 159: $'WhitespaceSensitiveMacros:\r': command not found /eap/.clang-format: line 160: -: command not found /eap/.clang-format: line 161: -: command not found
最新发布
08-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杰哥的技术杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值