Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2091552
  • 博文数量: 249
  • 博客积分: 1305
  • 博客等级: 军士长
  • 技术积分: 4733
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-17 10:37
个人简介

不懂的东西还有很多,随着不断的学习,不懂的东西更多,无法消灭更多不懂的东西,那就不断的充实自己吧。 欢迎关注微信公众号:菜鸟的机器学习

文章分类

全部博文(249)

文章存档

2015年(1)

2014年(4)

2013年(208)

2012年(35)

2011年(1)

分类: LINUX

2012-12-20 18:10:40


1、网络接口关闭与激活
   
   #ifdown eth0 //关闭网络
   #ifup eth0 //启动网络
   或
   #ifconfig eth0 up
   #ifconfig eth0 down

2、网络服务启动与关闭

   方法一:
   #service network stop  //关闭网络服务
   #service network start //启动网络服务
   #service network restart  //重启网络服务

   方法二:
   #/etc/init.d/network stop //关闭网络服务
   #/etc/init.d/network start //启动网络服务
   #/etc/init.d/network restart //重启网络服务

3、网络状态查询

   #service network status

4、临时配置网卡信息,无需重启

   #ifconfig eth0 10.1.10.10 netmask 255.255.255.0
   #ifconfig 网络端口 IP地址 hw MAC地址 netmask 掩码地址 broadcast 广播地址 [up/down]

5、查看网卡接口信息,默认列出所有接口

   #ifconfig

6、查看当前路由及网关信息

   #netstat -r

7、网络相关配置文件

   /etc/sysconfig/network
   /etc/resolv.conf //DNS配置相关

8、其他
   
   ip命令、route命令
   
   z+R:将vim中折叠的行展开
   z+M:将vim中行进行折叠



 
   梦醒潇湘love
                                                          2012-12-20 18:30








函数getopt的学习


一、使用getopt函数
 
   下面是getopt函数的使用细节,使用getopt函数必须包含头文件

1、变量:int opterr
   
    如果opterr非零,当遇到未声明的选项字符或者选项字符后面缺失了参数,则打印错误消息到标准错误流。如果设置opterr为零,则不会打印错误消息,但是会返回一个?号表示一个错误。

2、变量:int optopt
   
    当getopt检测到一个未知选项字符或者某个选项没有带参数的时候,optopt将会存储该参数的值,我们可以输出此值作为诊断信息。

3、变量:int optind
   
    这个变量被getopt设置,表示下一个将被处理的argv数组中的元素。一旦getopt找到了所有的选项,则optind表示剩下的非选项参数的起点位置,该变量的初始值为1。

4、变量:char *optarg
   
    这个变量表示接受参数的选项的参数。


函数:int getopt(int argc, char **argv, const char *options)

参数:options的格式为"ab:c::",选项字符后面可以跟参数,如果选项字符后面是':',表示该选项需要       接受参数,如果是'::',表示参数可选。

二、getopt解析参数的例子

    下面是一个getopt解析参数的典型例子,关键点如下:
  • 一般情况下,getopt作为一个循环调用,当getopt返回-1,表示没有选项可以检查,循环终止。
  • switch语句用来区分getopt的返回值。
  • 第二个循环用来打印剩下的非选项参数。
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>

  5. int main(int argc, char *argv[])
  6. {
  7.     int aflag = 0;
  8.     int bflag = 0;
  9.     char *cvalue = NULL;
  10.     int index;
  11.     int c;

  12.     opterr = 0;
  13.     
  14.     while((c = getopt(argc, argv, "abc:")) != -1)
  15.     {
  16.         switch(c)
  17.         {
  18.             case 'a':
  19.                 aflag = 1;
  20.                 break;
  21.             case 'b':
  22.                 bflag = 1;
  23.                 break;
  24.             case 'c':
  25.                 cvalue = optarg;
  26.                 break;
  27.             case '?':
  28.                 if(optopt == 'c')
  29.                 {
  30.                     fprintf(stderr, "Option-%c requires an argument.\n",optopt);
  31.                 }
  32.                 else if(isprint(optopt))
  33.                 {
  34.                     fprintf(stderr, "Unknown option '-%c'.\n", optopt);
  35.                 }
  36.                 else
  37.                 {
  38.                     fprintf(stderr, "Unknown option character'\\x%x'.\n", optopt);
  39.                 }
  40.                 return 1;
  41.             default:
  42.                 abort();
  43.         }
  44.     }

  45.     printf("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue);

  46.     for(index = optind; index < argc; index++)
  47.     {
  48.         printf("Non-option argument %s\n", argv[index]);
  49.     }
  50.     return 0;
  51. }
    测试结果:
    # ./test_getopt
    aflag = 0, bflag = 0, cvalue = (null)
 
    #./test_getopt  -a -b
    aflag = 1, bflag = 1, cvalue = (null)

    # ./test_getopt  -ab
    aflag = 1, bflag = 1, cvalue = (null)

    # ./test_getopt  -c foo
    aflag = 0, bflag = 0, cvalue = foo

    # ./test_getopt  -cfoo
    aflag = 0, bflag = 0, cvalue = foo

    #./test_getopt arg1
    aflag = 0, bflag = 0, cvalue = (null)
    Non-option argument arg1

    # ./test_getopt  -a arg1
    aflag = 1, bflag = 0, cvalue = (null)
    Non-option argument arg1

    # ./test_getopt  -c foo argv
    aflag = 0, bflag = 0, cvalue = foo
    Non-option argument argv
    
    # ./test_getopt  -a -- -b
    aflag = 1, bflag = 0, cvalue = (null)
    Non-option argument -b

    # ./test_getopt  -a -
    aflag = 1, bflag = 0, cvalue = (null)
    Non-option argument -





   梦醒潇湘love
                                                          2012-12-23 20:30





阅读(2101) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~