Board logo

標題: 請教 linux shell bash, 帶variable 入 function... [打印本頁]

作者: mankowk    時間: 2020-3-27 19:22     標題: 請教 linux shell bash, 帶variable 入 function...

用 bash

帶個 variable 入 function

因為會帶唔同既 variable 入去,
所以唔可以在 function 入面寫死個 variable.

想run 完 function 之後, 原本帶果個 variable 要改變...

請教師兄們, 要加邊句 code?
謝謝
  1. #!/bin/bash

  2. tag1="hello"

  3. changeword() {
  4.         tag2=$1"9999"
  5.         ......想將 $tag2 個結果, 改變原本帶入黎既 $tag1........
  6. }

  7. changeword $tag1
  8. echo $tag1


  9. 想結果出 hello9999
複製代碼

作者: sapphire4890    時間: 2020-3-27 22:08

本帖最後由 sapphire4890 於 2020-3-27 22:09 編輯

回覆 1# mankowk
  1. #!/bin/bash

  2. tag1="hello"

  3. changeword() {
  4.     tag1=$1"9999"
  5. }

  6. changeword $tag1
  7. 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 錯左........
  1. #!/bin/bash

  2. tag1="hello"
  3. tag3="Peter"

  4. changeword() {
  5.         tag2=$1"9999"
  6.         ......想將 $tag2 個結果, 改變原本帶入黎既 $tag1........
  7. }

  8. changeword $tag1
  9. changeword $tag3
  10. echo $tag1
  11. echo $tag3

  12. 想結果出下面既...

  13. hello9999
  14. Peter9999
複製代碼

作者: pchai    時間: 2020-3-28 08:55

本帖最後由 pchai 於 2020-3-28 08:57 編輯

Pass by reference, 通常 bash好少咁寫. 太舊既bash唔support
  1. #!/bin/bash

  2. tag1="hello"

  3. changeword() {
  4.   local -n tag2=$1
  5.   tag2=$tag2"9999"
  6. }

  7. changeword tag1
  8. echo $tag1
複製代碼

作者: sapphire4890    時間: 2020-3-28 13:35

回覆 3# mankowk


    會唔會係CHING唸到複雜左 ...
因為function入面整新/update variable. 其實下面execute完print返係會出valid result.
  1. #!/bin/bash

  2. tag1="hello"
  3. tag3="sapphire4890"
  4. changeword() {
  5.   tag1=$1"9999";
  6.   tag2="Hello HKEPC"
  7.   tag3="sapphire4870"
  8. }

  9. changeword $tag1
  10. echo $tag1

  11. echo "$tag2";
  12. echo "$tag3";
複製代碼
result:
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"
  1. #!/bin/bash

  2. worda="A"
  3. wordb="B"
  4. tag1="Peter"
  5. tag2="Sam"

  6. changeword() {
  7.     part"$1"="999"$2
  8.   }

  9. changeword $worda $tag1
  10. changeword $wordb $tag2

  11. echo $"part$worda"
  12. echo $"part$wordb"

  13. 以上結果, 想出
  14. 999Peter
  15. 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
  1. #!/bin/bash

  2. declare -A arr
  3. arr["wordA"]="abc"
  4. arr+=( ["wordB"]="def" ["wordC"]="ghi" )

  5. key="wordA"
  6. echo ${arr[${key}]}

  7. for key in ${!arr[@]}; do
  8.     echo ${key} ${arr[${key}]}
  9. done
複製代碼
result
  1. ./test.sh
  2. abc
  3. wordA abc
  4. wordB def
  5. 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
  1. #!/bin/bash

  2. tag1="helloabc"
  3. tag3="sapphire4890"
  4. changeword() {
  5.   if [ $# > 0 ] ; then
  6.     for (( i=1; i<= $#; i++ ))
  7.     do
  8.       strArg[i]=${i};
  9.       echo ${strArg[i]}$tag1;
  10.     done
  11.   fi
  12. }

  13. changeword $tag1;
  14. echo $tag1;
  15. echo "` changeword $tag1 `";
  16. #echo "$tag2";
  17. echo "$tag3";
複製代碼
我用 #2 樓比既 link 去改..
比個 for loop , if then,  arg handling 同 code 第17行 你參考下
作者: sapphire4890    時間: 2020-3-30 12:19

實際想我有睇唔到有咩需要咁樣寫code.不如你比個實例你想做乜
pchai 發表於 2020-3-28 22:55



    same here.
如果現實寫 ... 一切從簡
寫個btree出黎玩 同 用上手唔同 ... 始終細program傳統array算
作者: it_jobs    時間: 2020-3-31 11:10

實際想我有睇唔到有咩需要咁樣寫code.不如你比個實例你想做乜
pchai 發表於 2020-3-28 22:55



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 編輯
有咩原因要reuse the variable?
pchai 發表於 2020-3-31 11:55



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

回覆  it_jobs

sorry, 我錯. 我完全唔明重點, call by reference 都係我一開頭題出.
世上有千萬把刀, 用 ...
pchai 發表於 2020-4-1 10:30


不如你一次過提你你嘅疑問.

一開始你有咩實例, 我回答.
你根據個回答又引申更多問題, 咁會沒完沒了.
作者: pchai    時間: 2020-4-3 18:50

不如你一次過提你你嘅疑問.

一開始你有咩實例, 我回答.
你根據個回答又引申更多問題, 咁會沒完沒了. ...
it_jobs 發表於 2020-4-3 10:04


你搵錯人.我係唔係開呢條題
作者: 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?





歡迎光臨 電腦領域 HKEPC Hardware (https://www.hkepc.com/forum/) Powered by Discuz! 7.2