linux c編程2

1.    shell裏的函數返回值........................................................................................... 1

2.    Test語句中的文件判定:................................................................................... 2

3.    Shell中的冒號:.................................................................................................. 2

4.    Continuebreak後加參數表示外條的層數..................................................... 3

5.    .的作用................................................................................................................. 3

6.    各種環境變量的意義:...................................................................................... 3

7.    Echo-e使轉義字符生效................................................................................. 4

8.    eval賦值............................................................................................................... 4

9.    Exec執行命令後關閉當前shell.......................................................................... 4

10.     Exit..................................................................................................................... 4

11.     Export................................................................................................................. 4

12.     Expr.................................................................................................................... 4

13.     Set的作用......................................................................................................... 6

14.     Shift.................................................................................................................... 6

15.     Trap.................................................................................................................... 6

16.     $((…))高效實現運算........................................................................................ 7

17.     參數擴展:....................................................................................................... 7

18.     即時文檔........................................................................................................... 8

 

1.    shell裏的函數返回值

[root@legend chapter02]# vim JAY

#!/bin/sh

foo()

{

   echo JAY;

   echo LEGEND;

}

result="$(foo)"              #注意引用方式$(foo),因爲foo不是變量,不能直接用$foo

echo $result

~                                                                              

"JAY" 8L, 78C 已寫入                                          

[root@legend chapter02]# ./JAY

JAY LEGEND

[root@legend chapter02]# vim my_name

 

#!/bin/sh

 

yes_or_no() {

  echo "Is your name $* ?"

  while true

  do

    echo -n "Enter yes or no: "

    read x

    case "$x" in

      y | yes ) return 0;;

      n | no )  return 1;;

      * )       echo "Answer yes or no"

    esac

  done

}

 

echo "Original parameters are $*"

 

if yes_or_no "$1"  #此時向函數傳遞的參數只有wang,而且yes_or_no可以直接做條件,不用test命令。經測試if $1 “$2” 也可以正常將函數名作爲命令參數傳入。同時if後的test命令也可作爲參數傳入。但是注意,雖然yes_or_no返回1(失敗)和0(成功)可以成爲條件語句,但是直接用10卻不能作爲條件語句。但是可以用truefaulse

then

  echo "Hi $1, nice name"

else

  echo "Never mind"

fi

 

exit 0

                                                                                                                                  

~                                                                                                                                  

[root@legend chapter02]# ./my_name wang liang

Original parameters are wang liang

Is your name wang ?

Enter yes or no: y

Hi wang, nice name

2.    Test語句中的文件判定:

File Conditional Result

-d file True if the file is a directory.

-e file True if the file exists. Note that, historically, the -e option has not

been portable, so -f is usually used.

-f file True if the file is a regular file.

-g file True if set-group-id is set on file.

-r file True if the file is readable.

-s file True if the file has nonzero size.

-u file True if set-user-id is set on file.

-w file True if the file is writable.

-x file True if the file is executable.

3.    Shell中的冒號:

可以作爲一條空語句,可以作爲條件語句,作用相當於true

同時可以爲變量條件化設置

$(var:=value)   #如果var設置了一個值  就保留,如果沒有或者爲空  設置成value,注意$(var:=value)並不能當作一條命令來執行,應該如下作爲:的參數。或者爲echo的參數。

[root@legend chapter02]# : ${var5:?default}

$(var:-value) var不會有任何改變,只是整個$var:-value)的值會受var狀態的影響,如果爲空或不存在,則返回value,有則返回該值。

[root@legend chapter02]# : ${var6:?default}   #這樣會在var6不存在時輸出var6: default

-bash: var6: default                        #存在時忽略此命令

[root@legend chapter02]# : ${var7:+default}   #var7存在並不爲空時返回bar,注意只是返回。不是輸出。

4.    Continuebreak後加參數表示外條的層數

這個作用很少用,因爲大大降低了程序的可讀性。

5.    .的作用

[root@legend chapter02]# vim classic  #設置環境變量腳本

 

#!/bin/sh

 

version=classic

PATH=/usr/local/old_bin:/usr/bin:/bin:.

PS1="classic> "

~                                                                               ~                                                                               

[root@legend chapter02]# vim latest

 

#!/bin/sh

 

version=latest

PATH=/usr/local/new_bin:/usr/bin:/bin:.

PS1=" latest version> "

~                                                                              

[root@legend chapter02]# . ./classic  #執行該腳本後的環境並沒有消失,因爲它沒有新建一個shell,而是直接用的原來rootshell,所以能夠看見腳本里的變量設置在腳本退出後仍然有效。

classic> echo $version

classic

classic> . latest

 latest version> echo $version

latest

latest version> exit    #輸入exit後退出該環境,到了legend環境。

logout

 

[legend@legend ~]$

6.    各種環境變量的意義:

[root@legend chapter02]# echo $PS1 #PS1表示輸入命令前的提示字符

[/u@/h /W]/$                    #/u 用戶 /h 主機 /w 目錄 /$ 權限

[root@legend chapter02]# echo $PS2 #PS2表示換行符

[root@legend chapter02]#

7.    Echo-e使轉義字符生效

[root@legend chapter02]# echo "string to output/c"

string to output/c

[root@legend chapter02]# echo -e "string to output/c"

string to output[root@legend chapter02]#

當然也可以用-n參數直接實現

[root@legend chapter02]# echo -n "string to output" 

string to output[root@legend chapter02]#

8.    eval賦值

[root@legend chapter02]# foo=100

[root@legend chapter02]# x=$foo    #此時將foo認作變量,$foo認作變量值

[root@legend chapter02]# echo $x 

100

[root@legend chapter02]# foo=10

[root@legend chapter02]# x=foo 

[root@legend chapter02]# y="$"$x     #將字符串$foo給了y,此時的$foo僅爲字符串

[root@legend chapter02]# echo $y     

$foo

[root@legend chapter02]# eval y="$"$x      #$foo的值10給了y,通過eval$foo變爲變量值

[root@legend chapter02]# echo $y    

10

[root@legend chapter02]#

9.    Exec執行命令後關閉當前shell

[root@legend chapter02]# exec wall "dfjkdfj"  #執行了wall “lkfj”之後關閉了rootshell。腳本中在此命令之後的命令都不會再執行了。

 

Broadcast message from root (pts/0) (Fri Jul 24 14:03:47 2009):

 

dfjkdfj

[legend@legend ~]$

10.            Exit

Exit 0 代表正常退出,剩下的1-125都表示各種出錯代碼。

例:[ -f file ] && exit 0 || exit1

11.            Export

Export foo或者export foo=”kdfjkd”

foo導出到所有當前shell的子shell及其孫shell之後的所有shell

Set –a或者set –allexport可將所有變量導出。

12.            Expr

[root@legend chapter02]# x=1

[root@legend chapter02]# expr $x + 1      #計算值,注意運算符前後有空格。

2

[root@legend chapter02]# x=10

[root@legend chapter02]# x=`expr $x + 1`   #注意此鍵爲反引號反引號的用法即替代命令,同時可以用x=$(($x+1))達到同樣的目的

[root@legend chapter02]# echo $x

11

[root@legend chapter02]# x=10

[root@legend chapter02]# x='expr $x + 1'  #單引號則識別爲字符串

[root@legend chapter02]# echo $x

expr $x + 1

[root@legend chapter02]# x=10

[root@legend chapter02]# x=$(expr $x + 1)   #$()和反引號的作用一樣,將裏面的運行輸出作爲參數表。

[root@legend chapter02]# echo $x

11

[root@legend chapter02]#value=12

 

[root@legend chapter02]#expr $value + 10 > /dev/null 2>&1 #控制錯誤信息和正常輸出全部消失

[root@legend chapter02]#echo $?        #存儲當前最新的命令的返回代碼

0                                                      #說明返回正常,計算結果是一個數。

[root@legend chapter02]#value=hello

[root@legend chapter02]#expr $value + 10 > /dev/null 2>&1

[root@legend chapter02]#echo $?

2                                                                                                             #返回不正常,非數字參數

其中表達式有以下使用方式:注意用的時候如果有< > 前面必須加/轉義符,不然將作爲重定向符引起邏輯錯誤

expr1 | expr2 expr1 if expr1 is nonzero, otherwise expr2 前零則後,否則爲前。

expr1 & expr2 Zero if either expression is zero, otherwise expr1 有零則零,無零則前。

expr1 = expr2 Equal

expr1 /> expr2 Greater than       #執行後輸出0(大於),1(不大於)

expr1 />= expr2 Greater than or equal to

expr1 /< expr2 Less than

expr1 /<= expr2 Less than or equal to

expr1 != expr2 Not equal

expr1 + expr2 Addition

expr1 - expr2 Subtraction

expr1 * expr2 Multiplication

expr1 / expr2 Integer division

expr1 % expr2 Integer modulo

expr的這些表達式均可作爲條件語句,而且不能加反引號。

[root@legend chapter02]# vim legend2

 

x=10

if `expr $x > 1`  #注意這個寫法事實上是把expr $x的輸出重定向到1這個文件裏去了

   then

   echo "in if"

   else

      echo "in else"

      fi

      exit 0

~                                                                              

"legend2" 8L, 97C 已寫入                                     

[root@legend chapter02]# ./legend2 

in if

[root@legend chapter02]#

正確寫法:

[root@legend chapter02]# vim legend2

 

x=10

if expr $x /> 1  #必須加轉義字符

   then

   echo "in if"

   else

      echo "in else"

      fi

      exit 0

~                                                                                                                                   

~                                                                                                                                  

"legend2" 8L, 96C 已寫入

[root@legend chapter02]# ./legend2 

1           

in if

13.            Set的作用

設置當前參數列表

[root@legend chapter02]# set $PATH

[root@legend chapter02]# echo $@

/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

[root@legend chapter02]# set date

[root@legend chapter02]# echo $@

date

[root@legend chapter02]# set $(date)

[root@legend chapter02]# echo $@   

2009 07 24 星期五 16:23:27 EDT

14.            Shift

從大到小左移參數列表,不加參數爲移一位,加n則移n位。$0將保留,但$1等將沖掉。

[root@legend chapter02]# set $(date)

[root@legend chapter02]# echo $@   

2009 07 24 星期五 16:27:06 EDT

[root@legend chapter02]# shift 2   

[root@legend chapter02]# echo $@   

24 星期五 16:27:06 EDT

[root@legend chapter02]#

15.            Trap

[root@legend chapter02]# vim _trap

 

#!/bin/sh

 

trap 'rm -f /tmp/my_tmp_file_$$' INT  #執行ctrl+c發出INT信號時執行刪除語句

echo creating file /tmp/my_tmp_file_$$ 

date > /tmp/my_tmp_file_$$

 

echo "Press interrupt (Ctrl-C) to interrupt...."

while [ -f /tmp/my_tmp_file_$$ ]; do

    echo File exists

    sleep 1

done

echo The file no longer exists

 

trap – INT                     #發出信號時執行默認的中斷語句,後面的東西都不執行

echo creating file /tmp/my_tmp_file_$$

date > /tmp/my_tmp_file_$$

 

echo "Press interrupt (Ctrl-C) to interrupt...."

while [ -f /tmp/my_tmp_file_$$ ]; do

    echo File exists

    sleep 1

done

 

echo We never get here

 

exit 0

 

~                                                                                                                                  

~                                                                                                                                   

[root@legend chapter02]# ./_trap  

creating file /tmp/my_tmp_file_4468

Press interrupt (Ctrl-C) to interrupt....

File exists

File exists

File exists

File exists

^CThe file no longer exists

creating file /tmp/my_tmp_file_4468

Press interrupt (Ctrl-C) to interrupt....

File exists

File exists

File exists

File exists

File exists

^C

[root@legend chapter02]#

16.            $((…))高效實現運算

X=$(($x+1))

17.            參數擴展:

[root@legend chapter02]# vim legend3

 

#!/bin/sh

touch super2

touch super3

for i in 2 3

   do

      rm super${i}    #不用大括號此時也不會有什麼影響,但是如果文件名爲2super,若用$isuper則有問題,系統認爲isuper是一個變量而報錯

      done

      exit 0

[root@legend chapter02]# vim param

 

#!/bin/sh

 

unset foo

echo ${foo:-bar}  

 

foo=fud

echo ${foo:-bar}

 

foo=/usr/bin/X11/startx

echo ${foo#*/}    #至少刪去一個*/

echo ${foo##*/}   #刪到最後一個*/

 

bar=/usr/local/etc/local/networks

echo ${bar%local*}   #刪掉最後一個local*

echo ${bar%%local*}  #刪到最前一個local*

 

exit 0

~                                                                              

 [root@legend chapter02]# ./param

bar

fud

usr/bin/X11/startx

startx

/usr/local/etc/

/usr/

18.            即時文檔

 

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