作者: mankowk 時間: 2020-3-27 19:22 標題: 請教 linux shell bash, 帶variable 入 function...
用 bash
帶個 variable 入 function
因為會帶唔同既 variable 入去,
所以唔可以在 function 入面寫死個 variable.
想run 完 function 之後, 原本帶果個 variable 要改變...
請教師兄們, 要加邊句 code?
謝謝
- #!/bin/bash
- tag1="hello"
- changeword() {
- tag2=$1"9999"
- ......想將 $tag2 個結果, 改變原本帶入黎既 $tag1........
- }
- changeword $tag1
- echo $tag1
- 想結果出 hello9999
作者: sapphire4890 時間: 2020-3-27 22:08
本帖最後由 sapphire4890 於 2020-3-27 22:09 編輯
回覆 1# mankowk
- #!/bin/bash
- tag1="hello"
- changeword() {
- tag1=$1"9999"
- }
- changeword $tag1
- echo $tag1
一係最底果個echo $tag2都得, 如果你用你自己貼出黎果pat野
Online Compiler
https://repl.it/@willsonlaw/FlakyBitterDirectories
作者: mankowk 時間: 2020-3-28 03:26
回覆 2# sapphire4890
多謝師兄回覆...
其實咁既.....
例如: 我打算加埋 tag1 tag3 tag4 tag5 ....
但之後又會用返 改變左後既 tag1 tag3 tag4 tag5 ....
所以我先會咁樣問..........
或者係咁 program logic 錯左........
- #!/bin/bash
- tag1="hello"
- tag3="Peter"
- changeword() {
- tag2=$1"9999"
- ......想將 $tag2 個結果, 改變原本帶入黎既 $tag1........
- }
- changeword $tag1
- changeword $tag3
- echo $tag1
- echo $tag3
- 想結果出下面既...
- hello9999
- Peter9999
作者: pchai 時間: 2020-3-28 08:55
本帖最後由 pchai 於 2020-3-28 08:57 編輯
Pass by reference, 通常 bash好少咁寫. 太舊既bash唔support
- #!/bin/bash
- tag1="hello"
- changeword() {
- local -n tag2=$1
- tag2=$tag2"9999"
- }
- changeword tag1
- echo $tag1
作者: sapphire4890 時間: 2020-3-28 13:35
回覆 3# mankowk
會唔會係CHING唸到複雜左
因為function入面整新/update variable. 其實下面execute完print返係會出valid result.
- #!/bin/bash
- tag1="hello"
- tag3="sapphire4890"
- changeword() {
- tag1=$1"9999";
- tag2="Hello HKEPC"
- tag3="sapphire4870"
- }
- changeword $tag1
- echo $tag1
- echo "$tag2";
- echo "$tag3";
hello999
Hello HKEPC
sapphire4870
作者: mankowk 時間: 2020-3-28 21:13
回覆 4# pchai
多謝 pchai兄 幫忙, 無錯勒, 就係 Pass by Reference,
我到依家都唔係太理解個 concept.
回覆 5# sapphire4890
謝謝 sapphire4890兄 回覆,
但係呢個方法唔係咁好....
因為要在 function 入面寫 n 個 tagX, 失左效果.
作者: mankowk 時間: 2020-3-28 21:58
回覆 4# pchai
想再請教 pchai兄...
下面段 code 係唔 work 既...
可以教下我點樣執執佢....
謝謝..
想帶兩個 variable 入 function,
run 完之後, 搞過新既 variablea(名) 出黎用,
但要係
$"part$worda" -----> 我意思係等於 $partA
$"part$wordb" -----> 我意思係等於 $partB
最後係想再返用 上面個兩個 $"part$worda" $"part$wordb"
- #!/bin/bash
- worda="A"
- wordb="B"
- tag1="Peter"
- tag2="Sam"
- changeword() {
- part"$1"="999"$2
- }
- changeword $worda $tag1
- changeword $wordb $tag2
- echo $"part$worda"
- echo $"part$wordb"
- 以上結果, 想出
- 999Peter
- 999Sam
作者: pchai 時間: 2020-3-28 22:44
回覆 mankowk
會唔會係CHING唸到複雜左 ...
因為function入面整新/update variable. 其實下面e ...
sapphire4890 發表於 2020-3-28 13:35
pass by reference 同 pass by value學program一定要識
你想像variable係一個盒, pass by reference就係成個盒交比個function, 個function做完野成個盒比返你
pass by value就係你比盒個內容佢. 佢知到個內容就話比你知個結果
作者: pchai 時間: 2020-3-28 22:49
回覆 6# mankowk
呢個係 variable variables
https://www.php.net/manual/en/language.variables.variable.php
我無試過係bash咁寫. 如果要寫到咁不如用php或者python
始終bash個syntax 唔太嚴謹,好易出事
作者: pchai 時間: 2020-3-28 22:55
回覆 mankowk
呢個係 variable variables
我無試過係bash咁寫. 如果要寫到咁不如用php或者python
始終 ...
pchai 發表於 2020-3-28 22:49
實際想我有睇唔到有咩需要咁樣寫code.不如你比個實例你想做乜
作者: pchai 時間: 2020-3-28 23:25
我大致上覺得你想寫dictionary
- #!/bin/bash
- declare -A arr
- arr["wordA"]="abc"
- arr+=( ["wordB"]="def" ["wordC"]="ghi" )
- key="wordA"
- echo ${arr[${key}]}
- for key in ${!arr[@]}; do
- echo ${key} ${arr[${key}]}
- done
- ./test.sh
- abc
- wordA abc
- wordB def
- wordC ghi
作者: mankowk 時間: 2020-3-29 21:36
回覆 11# pchai
thank you 師兄, 我研究下先....
其實我行緊部 raspberry pi ,
想寫 d shell script 去 定期 backup 部 raspberry pi image 等等...
又 pat pat 痕.... 想玩下寫 function 黎簡化 d code....
作者: pchai 時間: 2020-3-29 22:25
回覆 12# mankowk
用python
作者: faiwaic 時間: 2020-3-30 10:20
本帖最後由 faiwaic 於 2020-3-30 10:24 編輯
回覆 pchai
thank you 師兄, 我研究下先....
其實我行緊部 raspberry pi ,
想寫 d shell script ...
mankowk 發表於 2020-3-29 21:36
- #!/bin/bash
- tag1="helloabc"
- tag3="sapphire4890"
- changeword() {
- if [ $# > 0 ] ; then
- for (( i=1; i<= $#; i++ ))
- do
- strArg[i]=${i};
- echo ${strArg[i]}$tag1;
- done
- fi
- }
- changeword $tag1;
- echo $tag1;
- echo "` changeword $tag1 `";
- #echo "$tag2";
- echo "$tag3";
比個 for loop , if then, arg handling 同 code 第17行 你參考下
作者: sapphire4890 時間: 2020-3-30 12:19
same here.如果現實寫 ... 一切從簡
寫個btree出黎玩 同 用上手唔同 ... 始終細program傳統array算

作者: it_jobs 時間: 2020-3-31 11:10
name1 = "Peter"
name2 = "Mary"
name3 = "John"
say_hello($name1)
say_hello($name2)
say_hello($name3)
echo $name1
echo $name2
echo $name3
作者: pchai 時間: 2020-3-31 11:55
name1 = "Peter"
name2 = "Mary"
name3 = "John"
say_hello($name1)
say_hello($name2)
say_hello($nam ...
it_jobs 發表於 2020-3-31 11:10
有咩原因要reuse the variable?
作者: it_jobs 時間: 2020-4-1 10:12
本帖最後由 it_jobs 於 2020-4-1 10:17 編輯
name1 = "Peter"
name2 = "Mary"
name3 = "John"
say_hello($name1, $result1)
say_hello($name2, $result2)
say_hello($name3, $result3)
echo $result1
echo $result2
echo $result3
對比
name1 = "Peter"
name2 = "Mary"
name3 = "John"
say_hello($name1)
result1 = $tmp_result
say_hello($name2)
result2 = $tmp_result
say_hello($name3)
result3 = $tmp_result
echo $result1
echo $result2
echo $result3
是否 reuse 不是本題目重點,
應不應 reuse 設計時自有考慮, 君不見現世代 programming language 都是以 var 居多? 而不是 const?
developer 想咁做自有其原因,
如不應 call by reference, 為何要存在這種 syntax?
挑戰應不應用 call by reference, 你應向制定 language spec 嘅人/組織問.
作者: pchai 時間: 2020-4-1 10:30
回覆 18# it_jobs
sorry, 我錯. 我完全唔明重點, call by reference 都係我一開頭題出.
世上有千萬把刀, 用牛內刀切菜唔係唔得, 需唔需要咁解
作者: it_jobs 時間: 2020-4-3 10:04
不如你一次過提你你嘅疑問.
一開始你有咩實例, 我回答.
你根據個回答又引申更多問題, 咁會沒完沒了.
作者: pchai 時間: 2020-4-3 18:50
你搵錯人.我係唔係開呢條題
作者: mankowk 時間: 2020-4-3 23:08
多謝各位師兄們既回應....
在我既 case 黎講, 用 call by reference 去寫, 會變得太過複雜,
所以我已經放棄左用 call by reference ...
都係直接用最基本既寫法去寫就ok了, 係d code 長d ....唔緊要, 都係自己用的..
謝謝各位...
作者: chanman1919 時間: 2020-4-27 14:26
maybe write a python function and then execute by the shell script cmd?

