考慮以下腳本(即用於local_var2算術語法是無關這種情況下):差分
#!/bin/ksh
function my_func1
{
typeset local_var1=one
typeset local_var2
((local_var2 = 1))
echo my_func1: local_var1 = $local_var1, local_var2 = $local_var2
}
my_func2()
{
typeset local_var1=two
typeset local_var2
((local_var2 = 2))
echo my_func2: local_var1 = $local_var1, local_var2 = $local_var2
}
local_var1=0
local_var2=0
echo before functions: local_var1 = $local_var1, local_var2 = $local_var2
my_func1
echo after my_func1: local_var1 = $local_var1, local_var2 = $local_var2
my_func2
echo after my_func2: local_var1 = $local_var1, local_var2 = $local_var2
當運行它產生以下輸出:
before functions: local_var1 = 0, local_var2 = 0
my_func1: local_var1 = one, local_var2 = 1
after my_func1: local_var1 = 0, local_var2 = 0
my_func2: local_var1 = two, local_var2 = 2
after my_func2: local_var1 = two, local_var2 = 2
(這不是預期的!)
如果我在bash中運行相同的腳本,輸出是:
before functions: local_var1 = 0, local_var2 = 0
my_func1: local_var1 = one, local_var2 = 1
after my_func1: local_var1 = 0, local_var2 = 0
my_func2: local_var1 = two, local_var2 = 2
after my_func2: local_var1 = 0, local_var2 = 0
(這是預期!)
測試並確認輸出!雖然,除了說ksh糟糕之外,我沒有對此的解釋;-) – zmo
在代碼中有一個'set -vx'(並且每個函數都依賴於shell),你會發現varible的內容顯然與預期的不同首先使用第二個電話 – NeronLeVelu