自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳細(xì)介紹Linux shell腳本基礎(chǔ)學(xué)習(xí)(六)

系統(tǒng) Linux
Linux shell腳本基礎(chǔ)學(xué)習(xí)我們這里就差不多講完了,最后一部分內(nèi)容是關(guān)于函數(shù)的,這就差不多把基礎(chǔ)部分介紹完了,后面還會(huì)有實(shí)例。

4)函數(shù)

如果您寫了一些稍微復(fù)雜一些的程序,您就會(huì)發(fā)現(xiàn)在程序中可能在幾個(gè)地方使用了相同的代碼,并且您也會(huì)發(fā)現(xiàn),如果我們使用了函數(shù),會(huì)方便很多。一個(gè)函數(shù)是這個(gè)樣子的:

functionname()

{

# inside the body $1 is the first argument given to the function

# $2 the second ...

body

}

您需要在每個(gè)程序的開始對函數(shù)進(jìn)行聲明。

下面是一個(gè)叫做xtitlebar的腳本,使用這個(gè)腳本您可以改變終端窗口的名稱。

這里使用了一個(gè)叫做help的函數(shù)。正如您可以看到的那樣,這個(gè)定義的函數(shù)被使用了兩次。

#!/bin/sh

# vim: set sw=4 ts=4 et:

help()

{

cat <

xtitlebar -- change the name of an xterm, gnome-terminal or kde konsole

USAGE: xtitlebar [-h] "string_for_titelbar"

OPTIONS: -h help text

EXAMPLE: xtitlebar "cvs"

HELP

exit 0

}

# in case of error or if -h is given we call the function help:

[ -z "$1" ] && help

[ "$1" = "-h" ] && help

# send the escape sequence to change the xterm titelbar:

echo -e "33]0;$107"

#

在腳本中提供幫助是一種很好的編程習(xí)慣,這樣方便其他用戶(和您)使用和理解腳本。

命令行參數(shù)

我們已經(jīng)見過$* 和 $1, $2 ... $9 等特殊變量,這些特殊變量包含了用戶從命令行輸入的參數(shù)。迄今為止,我們僅僅了解了一些簡單的命令行語法(比如一些強(qiáng)制性的參數(shù)和查看幫助的-h選項(xiàng))。 但是在編寫更復(fù)雜的程序時(shí),您可能會(huì)發(fā)現(xiàn)您需要更多的自定義的選項(xiàng)。通常的慣例是在所有可選的參數(shù)之前加一個(gè)減號(hào),后面再加上參數(shù)值 (比如文件名)

。

有好多方法可以實(shí)現(xiàn)對輸入?yún)?shù)的分析,但是下面的使用case表達(dá)式的例子無遺是一個(gè)不錯(cuò)的方法。

#!/bin/sh

help()

{

cat <

This is a generic command line parser demo.

USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2

HELP

exit 0

}

while [ -n "$1" ]; do

case $1 in

-h) help;shift 1;; # function help is called

-f) opt_f=1;shift 1;; # variable opt_f is set

-l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2

--) shift;break;; # end of options

-*) echo "error: no such option $1. -h for help";exit 1;;

*) break;;

esac

done

echo "opt_f is $opt_f"

echo "opt_l is $opt_l"

echo "first arg is $1"

echo "2nd arg is $2"

您可以這樣運(yùn)行該腳本:

cmdparser -l hello -f -- -somefile1 somefile2

返回的結(jié)果是:

opt_f is 1

opt_l is hello

first arg is -somefile1

2nd arg is somefile2

這個(gè)腳本是如何工作的呢?腳本首先在所有輸入命令行參數(shù)中進(jìn)行循環(huán),將輸入?yún)?shù)與case表達(dá)式進(jìn)行比較,如果匹配則設(shè)置一個(gè)變量并且移除該參數(shù)。根據(jù)unix系統(tǒng)的慣例,首先輸入的應(yīng)該是包含減號(hào)的參數(shù)。

【編輯推薦】

  1. 詳細(xì)介紹Linux shell腳本基礎(chǔ)學(xué)習(xí)(一)
  2. 詳細(xì)解析Linux服務(wù)器攻擊的分級(jí)防御
  3. Linux系統(tǒng)安全提高之安全配置
  4. Linux系統(tǒng)安全提高之系統(tǒng)優(yōu)化
  5. 詳述怎么使用Linux救援模式

 

責(zé)任編輯:小霞 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-03-05 10:24:33

2010-03-05 10:50:17

2010-03-05 11:58:52

2010-03-05 13:09:23

Linux shell

2010-03-05 11:18:52

Linux shell

2010-03-05 13:19:23

2010-03-05 12:50:38

Linux shell

2011-09-27 13:52:41

2010-06-23 16:05:36

Linux Bash

2016-12-08 22:26:28

2010-06-23 15:55:36

Linux Bash

2011-06-21 14:42:37

ECMAScriptJavaScript

2019-08-12 07:45:44

Linux腳本shell

2011-06-22 12:57:54

JVM

2010-02-23 10:32:20

Python 腳本

2024-11-27 09:19:25

2010-06-18 17:35:16

Linux Anacr

2010-06-22 09:28:31

Linux at命令

2009-12-18 11:48:24

網(wǎng)絡(luò)安裝 Linux

2021-02-15 17:29:46

LinuxShell腳本
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)