小编JA3*_*A3N的帖子

将命令行参数与字符串进行比较

这是我的代码:

#!/bin/bash
if [ "$#" -ne 2 ] ; then
        echo "$0: exactly 2 arguments expected"
        exit 3
fi

if [$1 != "file" -a $1 != 'dir'] ; then
        echo "$0: first argument must be string "file" or "dir""
        exit 1
elif [-e $2 -a -r $2]; then
        if ["$1" = "file" -a -f $2] ; then
                echo YES
        elif ["$1" = "dir" -a -d $2] ; then
                echo YES
        else
                echo NO
        fi
        exit 0
else
        echo "$0: $2 …
Run Code Online (Sandbox Code Playgroud)

unix bash

30
推荐指数
2
解决办法
5万
查看次数

文件中行数的变量

我试图将文件中的行数放入整数变量中.

这就是我在做的方式

typeset -i x
x=`wc -l $1`
Run Code Online (Sandbox Code Playgroud)

其中$ 1是命令行arg

问题是wc -l给出了一个数字和文件名,如:5 blah

有没有办法只将数字放入x?

unix bash

4
推荐指数
2
解决办法
1万
查看次数

Makefile基础知识

我在makefile的基础知识方面遇到了麻烦.

我用gcc编译

我需要做的是创建一个名为labMakefile的makefile,目标是

lab
labM.o
labG.o
clean
Run Code Online (Sandbox Code Playgroud)

已经在我正在处理的文件夹中的文件包含

labM.c
labM.o
labG.c
labG.o
Run Code Online (Sandbox Code Playgroud)

我一直在看makefile教程但我似乎无法找到创建makefile的正确方法

我试过的

labMakefile: labM.o labG.o
Run Code Online (Sandbox Code Playgroud)

但它只是说 labMakefile:: command not found

c makefile

3
推荐指数
1
解决办法
1100
查看次数

scanf无限循环

该程序获取文件中的第一个数字,并指示其后的数字,然后使用后面的数字执行各种其他操作.

看起来scanf在尝试从文件中读取时会导致无限循环.当我运行该程序时,即使是1号检查也无效

这是代码:

#include <stdio.h>

int main(void) {
        int N, a, n;
        int x=0;
        int t=0;
        printf("1"); //Check
        scanf("%d", &N);
        printf("2"); //Check
        int nums[N];
        int i;
        printf("%d", &N);  //Check
        for (i=0; i<N; i++)
        {
                scanf("%d", &nums[i]);
                t+=nums[i];

                if (nums[i] > x) x=nums[i];

                if (i=0 || nums[i] < n) n = nums[i];
        }
        a = t/N;   
        printf("Number Processed: \t%d\n", &N);
        printf("Maximum: \t%d\n", &x);
        printf("Minimum: \t%d\n", &n);
        printf("Total: \t%d\n", &t);
        printf("Average: \t%d\n", &a);
}
Run Code Online (Sandbox Code Playgroud)

我运行程序的方式是

gcc -lab16
./a.out <in1
Run Code Online (Sandbox Code Playgroud)

其中in1是文本并且有数字

7
6 …
Run Code Online (Sandbox Code Playgroud)

c scanf

0
推荐指数
1
解决办法
362
查看次数

标签 统计

bash ×2

c ×2

unix ×2

makefile ×1

scanf ×1