在PowerShell中巧用 OFS

2021-07-11 12:58:08 字數 2448 閱讀 2927

今天我們來看看powershell中的$ofs變數。說起$ofs,我們首先要知道的是在一門語言中都會有一些自動變數,這在powershell中自然也一樣,在powershell中包含了許多自動變數,我們可以通過使用get-variable cmdlet來檢視當前powershell中的自變數,如下:

ps c:\users\administrator> get-variable

name value

---- -----

$ gv

? true

^ gv

a a

args {}

b 6/11/2016 7:50:07 am

confirmpreference high

consolefilename

debugpreference silentlycontinue

error

pscommandpath

psculture en-us

psdefaultparametervalues {}

psemailserver

pshome c:\windows\system32\windowspowershell\v1.0

psscriptroot

pssessionconfigurationname

pssessionoption system.management.automation.remoting.pssessionoption

psuiculture en-us

psversiontable

pwd c:\users\administrator

shellid microsoft.powershell

stacktrace at system.management.automation.commanddiscovery.lookupcommandinfo(string commandn...

true true

verbosepreference silentlycontinue

warningpreference continue

whatifpreference false

如你所看到的,powershell中的自變數非常的多,這裡我們就不一一說明它們的作用了,今天我們主要想看的是$ofs這個變數的作用。

首先要說明的是$ofs是乙個儲存了乙個字串的特殊的變數,你可以用$ofs來作為欄位的分隔符來輸出,也就是說當你把乙個陣列轉化成字串的時候可以用這個變數來做分割輸出。一般在預設情況下$ofs變數不包含任何值,你可以通過傳統賦值的方式給$ofs賦值,那麼接下來我們來看看怎麼操作$ofs:

首先我們定義了乙個str字串陣列,它包含1,2,3,4,5,6 一共六個元素。

ps c:\users\administrator> $str = 1,2,3,4,5,6

ps c:\users\administrator> $str12

3456

接著我們直接給$ofs賦值,這裡我們緊接著用分號分隔,繼續把我們之前定義的str變數強制申明為string型別,這個時候你就會發現每乙個元素被我們所定義的分隔符所插入,並緊挨著元素分開了。

ps c:\users\administrator> $ofs = ">>";[string]$str

1>>2>>3>>4>>5>>6

當然我們也可以分開寫,只要定義好了$ofs變數值後,你接下來的操作都會影響每乙個字串陣列元素,而這些元素間隔都會被插入$ofs變數的值。

ps c:\users\administrator> $ofs = "+"

ps c:\users\administrator> [string]$str

1+2+3+4+5+6

在HTML網頁中巧用URL

首先,先放出乙個位址給大家測試 1,時下流行的 可能是吧,因為最近問的人比較多 就是沒有任何檔名的url位址。比如htmlpro 其實這和伺服器設定的預設檔名有關,比如index.htm,index.html,index.asp等等,不信你試試我朋友的乙個 asp的 2,本處的檔案為index.ht...

在HTML網頁中巧用URL

首先,先放出乙個位址給大家測試 1,時下流行的 可能是吧,因為最近問的人比較多 就是沒有任何檔名的url位址。比如htmlpro 其實這和伺服器設定的預設檔名有關,比如index.htm,index.html,index.asp等等,不信你試試我朋友的乙個 asp的 2,本處的檔案為index.ht...

在HTML網頁中巧用URL

1,時下流行的 可能是吧,因為最近問的人比較多 就是沒有任何檔名的url位址。比如htmlpro 其實這和伺服器設定的預設檔名有關,比如index.htm,index.html,index.asp等等,不信你試試我朋友的乙個 asp的 2,本處的檔案為index.html,同時後面帶有引數,頁面效果...