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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章