小编lis*_*per的帖子

当作为选项传递时,shell代码扩展不起作用

我发现波浪扩展不适用于以下情况:

$ set -x

$ ./print_arg.pl destdir=~/test
+ ./print_arg.pl 'destdir=/root/test'
destdir=/root/test

$ ./print_arg.pl dest-dir=~/test
+ ./print_arg.pl 'dest-dir=~/test'
dest-dir=~/test

$ ./print_arg.pl -destdir=~/test
+ ./print_arg.pl '-destdir=~/test'
dest-dir=~/test
Run Code Online (Sandbox Code Playgroud)

print_arg.pl的内容是

#!/usr/bin/perl
print $ARGV[0],"\n";
Run Code Online (Sandbox Code Playgroud)

根据Shell的处理顺序,壳牌将在"波浪扩张"之前分词.而且我注意到单词分裂实际上是不同的.结果不同的原因是什么?

shell tilde-expansion

5
推荐指数
1
解决办法
392
查看次数

如何确保malloc/free fopen/fclose匹配?

我认为以下代码是正常的(并且malloc/free类似):

int foo(){

   FILE *fp = fopen("test.in", "r");

   int i;   

   for(i = 0; i < NUM; i ++){
       if(Match(fp, i)){    
            fclose(fp);
            return i;
       }
   }

   fclose(fp);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我们可以看到fclose(fp)在代码中出现两次.如果函数foo中有其他return语句,它会显得更多.但是,我必须写fclose(fp)很多次很麻烦.一种解决方案只是一个函数的一个返回.但是,多次返回有时是有用的.还有其他解决方案吗?

PS:据我所知,lisp中有一个宏(带有打开文件).

(with-open-file (stream-var open-argument*) 
    body-form*)
Run Code Online (Sandbox Code Playgroud)

它可以为我们打开和关闭文件.

c fopen

2
推荐指数
2
解决办法
1958
查看次数

标签 统计

c ×1

fopen ×1

shell ×1

tilde-expansion ×1