在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Shell 替代
Shell 輸入/輸出重定向
Shell 循環(huán)類(lèi)型
Shell是什么?
Shell 特殊變量
Shell 算術(shù)運(yùn)算符示例
Shell 關(guān)系運(yùn)算符示例
Shell 替代
Shell 函數(shù)
Shell 條件語(yǔ)句
Shell 聯(lián)機(jī)幫助
Shell 數(shù)組/Arrays
Shell 布爾運(yùn)算符范例
Shell
Shell if...elif...fi 語(yǔ)句
Shell case...esac 語(yǔ)句
Shell 使用Shell變量
Shell 文件測(cè)試符例子
Shell 基本運(yùn)算符
Korn Shell 運(yùn)算符
Shell 字符串運(yùn)算范例
Shell while 循環(huán)
Shell 引用機(jī)制
Shell if...else...fi 語(yǔ)句
Shell select 循環(huán)
C Shell運(yùn)算符
Shell 循環(huán)控制break/continue
Shell for循環(huán)
Shell until 循環(huán)
Shell if...fi語(yǔ)句

Shell 替代

替代是什么?

Shell當(dāng)它遇到一個(gè)表達(dá)式,其中包含一個(gè)或多個(gè)特殊字符進(jìn)行替代。

例子:

下面的例子,同時(shí)使打印取代的其值的變量的值。同時(shí)“ ”是一個(gè)新行取代:

#!/bin/sh

a=10
echo -e "Value of a is $a 
"

這將產(chǎn)生下面的結(jié)果。這里-e選項(xiàng)可以解釋反斜杠轉(zhuǎn)義。

Value of a is 10

下面是結(jié)果沒(méi)有-e選項(xiàng):

Value of a is 10

這里echo命令可以用在以下轉(zhuǎn)義序列:

轉(zhuǎn)義 描述
backslash
a alert (BEL)
 backspace
c suppress trailing newline
f form feed
new line
carriage return
horizontal tab
v vertical tab

可以使用-E選項(xiàng)禁用解釋反斜杠轉(zhuǎn)義(默認(rèn))。

您可以使用-n選項(xiàng)來(lái)禁用插入新行。

命令替換:

命令替換shell執(zhí)行的機(jī)制,一組給定的命令,然后替代它們的輸出在命令。

語(yǔ)法

執(zhí)行命令替換的命令時(shí),被給定為:

`command`

當(dāng)執(zhí)行命令替換,確保您使用的是反引號(hào),不是單引號(hào)字符。

例子:

命令替換一般是用一個(gè)命令的輸出分配給一個(gè)變量。下面的例子演示命令替換:

#!/bin/sh

DATE=`date`
echo "Date is $DATE"

USERS=`who | wc -l`
echo "Logged in user are $USERS"

UP=`date ; uptime`
echo "Uptime is $UP"

這將產(chǎn)生以下結(jié)果:

Date is Thu Jul  2 03:59:57 MST 2009
Logged in user are 1
Uptime is Thu Jul  2 03:59:57 MST 2009
03:59:57 up 20 days, 14:03,  1 user,  load avg: 0.13, 0.07, 0.15

變量替代:

變量替換可使Shell程序員操縱變量的值,根據(jù)其狀態(tài)。

這里是所有可能的替換如下表:

格式 描述
${var} Substitue the value of var.
${var:-word} If var is null or unset, word is substituted for var. The value of var does not change.
${var:=word} If var is null or unset, var is set to the value of word.
${var:?message} If var is null or unset, message is printed to standard error. This checks that variables are set correctly.
${var:+word} If var is set, word is substituted for var. The value of var does not change.

例子:

下面的例子顯示各種狀態(tài),上述替代:

#!/bin/sh

echo ${var:-"Variable is not set"}
echo "1 - Value of var is ${var}"

echo ${var:="Variable is not set"}
echo "2 - Value of var is ${var}"

unset var
echo ${var:+"This is default value"}
echo "3 - Value of var is $var"

var="Prefix"
echo ${var:+"This is default value"}
echo "4 - Value of var is $var"

echo ${var:?"Print this message"}
echo "5 - Value of var is ${var}"

這將產(chǎn)生以下結(jié)果:

Variable is not set
1 - Value of var is
Variable is not set
2 - Value of var is Variable is not set

3 - Value of var is
This is default value
4 - Value of var is Prefix
Prefix
5 - Value of var is Prefix