legend

思考助我前行

linux c编程1

1.    数据运算转换的问题.......................................................................................... 1

2.    查找文件.............................................................................................................. 1

3.    查找文件内容:.................................................................................................. 1

4.    gcc手动加include路径:.................................................................................... 2

5.    gcc手动添加库文件:........................................................................................ 2

6.    静态库和共享库:.............................................................................................. 2

7.    Top命令查看任务,类似于任务管理器........................................................... 2

8.    File * 查看当前路径下所有文件的文件类型.................................................... 2

9.    Shell中给变量赋值时如果字符串里包含空格,该字符串必须前后加引号,而且=两旁不能有空格,否则系统会将s认作命令。.............................................................................................. 2

10.     Shell中的readecho.................................................................................. 2

11.     关于脚本参数................................................................................................... 3

12.     iftest或者[ ]中的问题:......................................................................... 4

13.     For的用法......................................................................................................... 4

 

1.    数据运算转换的问题

int I=2,t=1;

float f=0;

f=f+t/I;

这三句中实际上在运算时先运算T/I,但是他们均为整形,不用转换,结果为0;然后f+0,先把0转换为0.0000000,然后运算,结果f一直不变,表达式失去意义。正确的做法应该是将让i或者tfloat形式参与运算,得到0.500000之后再参加运算。

2.    查找文件

[root@legend include]# find /etc -name ifcfg-eth0        

/etc/sysconfig/network-scripts/ifcfg-eth0

/etc/sysconfig/networking/profiles/default/ifcfg-eth0

/etc/sysconfig/networking/devices/ifcfg-eth0

3.    查找文件内容:

[root@legend include]# grep EXIT_ *.h

argp.h:#define ARGP_HELP_EXIT_ERR       0x100 /* Call exit(1) instead of returning.  */

argp.h:#define ARGP_HELP_EXIT_OK        0x200 /* Call exit(0) instead of returning.  */

argp.h:  (ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR)

argp.h:  (ARGP_HELP_SHORT_USAGE | ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR)

argp.h:  (ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG | ARGP_HELP_EXIT_OK /

newt.h:    enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT, NEWT_EXIT_FDREADY,

newt.h:    NEWT_EXIT_TIMER } reason;

stdlib.h:#define        EXIT_FAILURE    1       /* Failing exit status.  */

stdlib.h:#define        EXIT_SUCCESS    0       /* Successful exit status.  */

4.    gcc手动加include路径:

gcc –I /usr/openwin/include fred.c

5.    gcc手动添加库文件:

gcc –o fred fred.c /usr/lib/libm.a

等效于:

Gcc –o fred fred.c –lm

6.    静态库和共享库:

静态库:形如libxxx.a

共享库:形如libxxx.so(编译时只是加入调用链接,在真正运行时在载入代码,和静态库相比节约了内存空间,因为它只有一个拷贝存于内存)

生成静态库:

首先编辑bill.c文件包含bill的函数实现,然后gcc –c bill.c生成bill.o的目标文件,通过命令:ar crv libbill.a bill.o 生成libbill.a的静态库文件。

引用静态库:

main.c里首先引用bill.h的函数申明文件,在main函数中调用bill函数,通过命令:gcc –o main main.c libbill.a即可完成静态库链接。或者gcc –o main main.c –L. –lfoo(其中-L的作用是添加静态库搜索路径,此命令中添加了当前路径)

当然也可以先gcc –c main.c生成main.o,gcc –o main main.o –L. –lfoo

7.    Top命令查看任务,类似于任务管理器

8.    File * 查看当前路径下所有文件的文件类型

9.    Shell中给变量赋值时如果字符串里包含空格,该字符串必须前后加引号,而且=两旁不能有空格,否则系统会将s认作命令。

[root@legend chapter02]# s=hello

[root@legend chapter02]# echo $s

hello

[root@legend chapter02]# s="hello world"

[root@legend chapter02]# echo $s

hello world

[root@legend chapter02]# s=7+6

[root@legend chapter02]# echo $s

7+6

10.            Shell中的readecho

Read myvar执行后系统等待用户输入myvar的值,然后继续执行脚本。

Echo后的引号:双引号里的$则会替换相应变量的内容,单引号则不替换。

如:[root@legend chapter02]# vim variable

 

#!/bin/sh

 

myvar="Hi there"

 

echo $myvar

echo "$myvar"

echo '$myvar'

echo /$myvar

 

echo Enter some text

read myvar

 

echo '$myvar' now equals $myvar

 

exit 0

~                                                                              

~                                                                              

~                                                                              

~                                                                               

~                                                                              

~                                                                              

[root@legend chapter02]# ./variable

Hi there

Hi there

$myvar

$myvar

Enter some text

legend

$myvar now equals legend

11.            关于脚本参数

[legend@legend chapter02]$ IFS=''               #设置分隔符为空

[legend@legend chapter02]$ set foo bar bar        #设置参数

[legend@legend chapter02]$ echo "$@"             #查看参数列表,不受IFS影响

foo bar bar

[legend@legend chapter02]$ echo "$*"             #IFS影响

foobarbar

[legend@legend chapter02]$ unset IFS

[legend@legend chapter02]$ echo "$*"

foo bar bar

[legend@legend chapter02]$ vim try_var

 

#!/bin/sh

 

salutation="Hello"

echo $salutation

echo "The program $0 is now running"        #打出脚本名

echo "The second parameter was $2"          #打出第二个参数

echo "The first parameter was $1"

echo "The parameter list was $*"                #打出所有参数

echo "The user's home directory is $HOME"

echo "Please enter a new greeting"

read salutation

echo $salutation

echo "The script is now complete"

exit 0

~                                                                              

~                                                                              

~                                                                               

~                                                                              

"try_var" 17L, 340C                                           1,1          全部

                                                                                                     

[legend@legend chapter02]$ ./try_var foo bar bar              #执行脚本,并且引入参数123

Hello

The program ./try_var is now running

The second parameter was bar

The first parameter was foo

The parameter list was foo bar bar

The user's home directory is /home/legend

Please enter a new greeting

hello

hello

The script is now complete

12.            iftest或者[ ]中的问题:

注意string1 = string2 ]中的空格四个一个都不能少,否则会导致逻辑错误,但是系统又不会报错。

[ "$timeofday" = "yes" ][ $timeofday = "yes" ]的区别是当用户不键入字符给变量赋值而直接回车时,变量值为空,前者识别为[ "" = "yes" ],是合法的,后者识别为 = "yes" ]是不合法的,所以报错。

13.            For的用法

 [legend@legend chapter02]$ vim for1

 

#!/bin/sh

 

for foo in bar fud 43

do

  echo $foo

done

 

exit 0

                                                                                                                                  

[legend@legend chapter02]$ ./for1

bar

fud

43

[legend@legend chapter02]$ vim for2

 

#!/bin/sh

 

for file in $(ls f*); do           #ls f*返回的所有字符串在$( )中被看作了变量集合

    echo $file

done

 

exit 0

 

                                                                                                                                

~                                                                                                                                  

"for2" 8L, 65C 已写入

[legend@legend chapter02]$ ./for2

first

for1

for2

function

阅读更多
个人分类: study_log
想对作者说点什么? 我来说一句

Linux C编程小知识

2010年08月23日 8.23MB 下载

linux 下c编程

2011年05月30日 461KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭