簡體   English   中英

如何在bash中的變量中存儲參數列表?

[英]How can I store a list of arguments in a variable in bash?

是否可以通過不必輸入 19 個不同的變量來使該文件更清晰。 我嘗試將所有路徑放在同一個變量中,但出現權限錯誤(編輯以顯示我在底部使用的代碼)。 使它們為每個路徑設置變量消除了該權限問題。 然后正如您在底部評論中看到的那樣,我正在尋找一種方法來包含所有變量,而無需輸入當前正在運行的內容。 不確定這是否有效。

# Reverts permissions from updating

ocpath='/var/www/html/core'
ocpath1='/var/www/html/lib'
ocpath2='/var/www/html/ocs'
ocpath3='/var/www/html/ocs-provider'
ocpath4='/var/www/html/resources'
ocpath5='/var/www/html/settings'
ocpath6='/var/www/html/AUTHORS'
ocpath7='/var/www/html/CHANGELOG.md'
ocpath8='/var/www/html/console.php'
ocpath9='/var/www/html/COPYING'
ocpath10='/var/www/html/cron.php'
ocpath11='/var/www/html/db_structure.xml'
ocpath12='/var/www/html/index.html'
ocpath13='/var/www/html/index.php'
ocpath14='/var/www/html/occ'
ocpath15='/var/www/html/public.php'
ocpath16='/var/www/html/remote.php'
ocpath17='/var/www/html/robots.txt'
ocpath18='/var/www/html/status.php'
ocpath19='/var/www/html/version.php'

htuser='someuser'
htgroup='someusergroup'

chown -R ${htuser}:${htgroup} $ocpath $ocpath2 $ocpath3 $ocpath4 $ocpath5 $ocpath6 $ocpath7 $ocpath8 $ocpath9 $ocpath10 $ocpath11 $ocpath12 $ocpath13 $ocpath14 $ocpath$15 $ocpath16 $ocpath17 $ocpath18 $ocpath19

# chown -R ${htuser}:${htgroup} $ocpath1:$ocpath19
$ocpath='/var/www/html/filename' '/var/www/html/filename2' '/var/www/html/filename3'

這就是數組的用途。

ocpaths=(
  '/var/www/html/core'
  '/var/www/html/lib'
  '/var/www/html/ocs'
  '/var/www/html/ocs-provider'
  '/var/www/html/resources'
  # etc
)

htuser='someuser'
htgroup='someusergroup'

chown -R "${htuser}:${htgroup}" "${ocpaths[@]}"

您可以使用那些新奇的數組,但如果您僅限於使用沒有數組的輕量級外殼,或者需要符合不允許此類結構的標准,您可以執行以下操作:

#!/bin/sh

set -- # Clear the positional arguments
for t in core lib ocs ocs-provider resources settings AUTHORS \
                CHANGELOG.md console.php COPYING cron.php \
                db_structure.xml index.html \
                index.php occ public.php remote.php \
                robots.txt status.php \
                version.php; do
        set -- "$@" "/var/www/html/$t"
done
htuser=someuser
htgroup=someusergroup

chown -R ${htuser}:${htgroup} "$@"

請注意,作為副作用,這會清除位置參數 Alhtough,老實說,您可能只想要chown "$htuser:$htgroup" -R /var/www/html/* ,或者(更合理地)在循環中調用chown並避免引用規則的所有骯臟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM