Remove ^M at end of each line in shell script
本问题已经有最佳答案,请猛点这里访问。
我有这个代码,但不起作用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | line="20170425" anycopia=${line:0:4} mescopia=${line:4:2} diacopia=${line:6:2} echo $anycopia echo $mescopia echo $diacopia DATE=$(date +%Y%m%d) any=${DATE:0:4} mes=${DATE:4:2} dia=${DATE:6:2} echo $any echo $mes echo $dia if [ $anycopia == $any ]; then echo"equals" else echo"not equals" fi |
错误:
1 | syntax error near unexpected token fi |
我试过改变"然后",但没关系,就像这样:
1 2 3 4 5 6 | if [ $anycopia == $any ] then echo"equals" else echo"not equals" fi |
同样的错误一直持续着。
PD:Stack Overflow中有相同问题的其他答案对我不起作用。
编辑:
我做了这个命令:
1 | hexdump -C script.sh |
这是输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 00000000 6c 69 6e 65 3d 22 32 30 31 37 30 34 32 35 22 0d |line="20170425".| 00000010 0a 61 6e 79 63 6f 70 69 61 3d 24 7b 6c 69 6e 65 |.anycopia=${line| 00000020 3a 30 3a 34 7d 0d 0a 6d 65 73 63 6f 70 69 61 3d |:0:4}..mescopia=| 00000030 24 7b 6c 69 6e 65 3a 34 3a 32 7d 0d 0a 64 69 61 |${line:4:2}..dia| 00000040 63 6f 70 69 61 3d 24 7b 6c 69 6e 65 3a 36 3a 32 |copia=${line:6:2| 00000050 7d 0d 0a 65 63 68 6f 20 24 61 6e 79 63 6f 70 69 |}..echo $anycopi| 00000060 61 0d 0a 65 63 68 6f 20 24 6d 65 73 63 6f 70 69 |a..echo $mescopi| 00000070 61 0d 0a 65 63 68 6f 20 24 64 69 61 63 6f 70 69 |a..echo $diacopi| 00000080 61 0d 0a 44 41 54 45 3d 24 28 64 61 74 65 20 2b |a..DATE=$(date +| 00000090 25 59 25 6d 25 64 29 0d 0a 61 6e 79 3d 24 7b 44 |%Y%m%d)..any=${D| 000000a0 41 54 45 3a 30 3a 34 7d 0d 0a 6d 65 73 3d 24 7b |ATE:0:4}..mes=${| 000000b0 44 41 54 45 3a 34 3a 32 7d 0d 0a 64 69 61 3d 24 |DATE:4:2}..dia=$| 000000c0 7b 44 41 54 45 3a 36 3a 32 7d 0d 0a 65 63 68 6f |{DATE:6:2}..echo| 000000d0 20 24 61 6e 79 0d 0a 65 63 68 6f 20 24 6d 65 73 | $any..echo $mes| 000000e0 0d 0a 65 63 68 6f 20 24 64 69 61 0d 0a 69 66 20 |..echo $dia..if | 000000f0 5b 20 24 61 6e 79 63 6f 70 69 61 20 3d 3d 20 24 |[ $anycopia == $| 00000100 61 6e 79 20 5d 3b 20 74 68 65 6e 0d 0a 20 20 20 |any ]; then.. | 00000110 20 65 63 68 6f 20 22 68 6f 6c 61 22 0d 0a 65 6c | echo"hola"..el| 00000120 73 65 0d 0a 20 20 20 20 65 63 68 6f 20 22 61 64 |se.. echo"ad| 00000130 65 75 22 0d 0a 66 69 0d 0a |eu"..fi..| 00000139 |
PDD:我在Windows上使用Bash在Ubuntu上运行它。
EDIT2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | user@DESKTOP-UO9KRO4:/mnt/d$ cat -v script.sh line="20170425"^M anycopia=${line:0:4}^M mescopia=${line:4:2}^M diacopia=${line:6:2}^M echo $anycopia^M echo $mescopia^M echo $diacopia^M DATE=$(date +%Y%m%d)^M any=${DATE:0:4}^M mes=${DATE:4:2}^M dia=${DATE:6:2}^M echo $any^M echo $mes^M echo $dia^M if [ $anycopia == $any ]; then^M echo"hola"^M else^M echo"adeu"^M fi^M |
1 | dos2unix script.sh |
另外作为一种安全的编码实践,