【问题标题】:Learning to walk with C - translating a 5 lines bashshell script to C [closed]学习用 C 走路 - 将 5 行 bash shell 脚本翻译成 C [关闭]
【发布时间】:2023-03-11 00:35:01
【问题描述】:

你好:) 我需要有人将这个小 bash 脚本翻译成 C,因为现在它消耗了大量的 CPU,所以我(和其他在谷歌上搜索这个的人)可以学习用 C 走路!这将是最有效的学习和我计划用 C 做很多事情,但我只是一个脚本语言的程序员并且知道非常基本的 java....

*BASH SCRIPT TO TRANSLASTE TO C*
a1=$1
a2=`expr $a1 + 1`
a3=`expr $a1 + 2`
a4=`expr $a1 + 3`
a5=`expr $a1 + 4`
Limit=`expr $a1  +  99999999999`
while [ $a1 -le $Limit ]
do
echo $a1,$a2,$a3,$a4,$a5 | sed 's/9,/9abc/g' >> List$1
a1=`expr $a1  +  500`  
a2=`expr $a2  +  500`
a3=`expr $a3  +  500`
a4=`expr $a4  +  500`
a5=`expr $a5  +  500`
done

..因此,如果我知道 C 语言中的以下内容,那么现在就到此为止了。

  1. 处理变量
  2. 等效于 >> 添加到文件,但最好使用缓存 减少磁盘io!
  3. 相当于 $1 的东西 - 允许用户设置变量 运行 C 程序时。
  4. C 中的正则表达式
  5. 相当于 | C中的管道
  6. 或者如何在 C 程序中执行 bash 命令,因为它 无论如何都不能比 grep,sed 快得多......

非常感谢所有阅读本文的人

【问题讨论】:

  • 书籍有什么问题?
  • 投票结束:问题应该是具体的。 “为我写这个程序”不是真的。
  • 何不先自己尝试,遇到问题再提出问题?
  • 这可能需要在 C 和 bash 方面有经验的人不到 5 分钟,但对于所有首先需要运行高性能的自制脚本开发人员来说,这对于一个虽然不是微不足道但常见的问题会有很大帮助
  • 它是具体的,它仅有的几行是典型 bash 工具的示例。我正在寻求一种适当的方式来翻译它们,以防只需要以尽可能高的性能运行这个简单的脚本

标签: c bash shell


【解决方案1】:

类似这样的:

#ifdef __cplusplus 
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
#else
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
#endif

char * stringReplace(char *string, char *search, char *replace) 
{
    char *tempString, *searchStart;
    int len=0;


    // preuefe ob Such-String vorhanden ist
    searchStart = strstr(string, search);
    if(searchStart == NULL) {
        return string;
    }

    // Speicher reservieren
    tempString = (char*) malloc(strlen(string) * sizeof(char));
    if(tempString == NULL) {
        return NULL;
    }

    // temporaere Kopie anlegen
    strcpy(tempString, string);

    // ersten Abschnitt in String setzen
    len = searchStart - string;
    string[len] = '\0';

    // zweiten Abschnitt anhaengen
    strcat(string, replace);

    // dritten Abschnitt anhaengen
    len += strlen(search);
    strcat(string, (char*)tempString+len);

    // Speicher freigeben
    free(tempString);

    return string;
}




int main(int argc, char* argv[])
{
    int i;
    for(i = 0; i < argc; ++i)
        printf("argv[%d]: %s\n", i, argv[i]);

    char string [256];
    int a1 = atoi((const char*) gets(string));
    int a2= a1 +1;
    int a3 = a1 + 2;
    int a4 = a1 + 3;
    int a5 = a1 + 4;

    int limit = a1 + 99999999999;

    while(a1 <= limit)
    {


        char command[1000];

        sprintf(command, "%d,%d,%d,%d,%d", a1, a2, a3, a4, a5);

        stringReplace(command, "9,", "9abc");

        FILE* pFile = fopen ("myfile.txt","wa");
        fprintf (pFile, "%s\n", command);
        fclose (pFile);


        /*
        sprintf(command, "echo \"%d,%d,%d,%d,%d\" | sed 's/9,/9abc/g' >> List%s", a1, a2, a3, a4, a5, string);


        FILE* pPipe  = popen(command, "r");
        char   psBuffer[128];
        while( !feof( pPipe ) )
        {
            if( fgets( psBuffer, 128, pPipe ) != NULL )
                printf("%s\n", psBuffer );
        }

        pclose(pPipe);
        */

        a1 += 500;
        a2 += 500;
        a3 += 500;
        a4 += 500;
        a5 += 500;
    }

    return EXIT_SUCCESS;
}

【讨论】:

  • 非常感谢!我们可以将“echo”、“>>”和可选的正则表达式翻译成 C 以加快速度并减少 io 吗?
  • int 不限于 2147483647 吗?因为这里我们的规模更大……
  • 只取一个 _int64 代替。或者,如果您使用的是 x64 Linux,则需要很长时间。或者缩小数字。
  • 是的 x64 linux,限制,a1-a5 也需要是 int 64bit 我试过 long long 和 #include 并将它们设为 int64_t 但还没有工作
  • 也非常感谢您抽出宝贵的时间。我将脚本与 a1-a600 和(相应地更高的字符命令 [1000])一起使用。它写入 7000 行/秒,而 shell 版本写入 5(7000 接近驱动器的最大速度)
猜你喜欢
  • 2014-08-13
  • 2014-04-25
  • 2014-11-17
  • 1970-01-01
  • 2010-11-12
  • 2023-03-06
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多