我有一个bash脚本,可以在我的系统中正确运行:
uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 GNU/Linux
但我需要它在Redhat 7.2 chroot中工作:
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 unknown
相同的代码在第一个代码上正确执行,但是当它在7.2上运行时,它首先不识别sed -i(只是-i参数)。评论某些行会遇到另一个问题:
bash: command substitution: line 1: syntax error near unexpected token `<<<'
问题是,这个脚本需要在带有debian 7.2的远程机器上执行(这就是为什么我在同一个发行版的chroot中测试它),所以它&#39;无法安装模块/升级以使其可运行。
示例代码:
#!/bin/bash
...
count=0
while read line; do
if echo "$line" | grep -q ')'
then
((count++))
comas=`grep -o "," <<< "$line" | wc -l`
num=`grep -o "byRef" <<< "$line" | wc -l`
...
sed -i 's/shortInteger/int/g' "test.h"
有什么想法吗?
感谢。
编辑: 这些是导致我麻烦的命令:
comas =`grep -o&#34;,&#34; &LT;&LT;&LT; &#34; $线&#34; | wc -l`
sed -i&#39; s / shortInteger / int / g&#39; &#34; test.h&#34;
编辑2:
GNU bash,版本2.05.8 - &gt; &#34; here string&#34; (&lt;&lt;&lt;&lt;&lt;&lt;&gt;)不存在
grep(GNU grep)2.4.2 - &gt; -o选项不存在
GNU sed版本3.02 - &gt; -i选项不存在
答案 0 :(得分:1)
最后我得到了以下内容......
对于我无法更新的版本:
sed solution
sed's / shortInteger / int / g'“test.h”&gt; test.temp.h;
mv“test.temp.h”“test.h”
@sorontar指出了这一点。非常感谢!
在字符串中查找特殊字符或子字符串
comas = $(echo“$ line”| tr“”“\ n”| grep -c“,”)
根据我的源文件,有一个模式,所以在任何逗号后我都有空格。所以tr " " "\n"
分隔出制作换行符的空格之间的子串,然后我可以用grep -c ","
用逗号来计算每一行。