[英]Execute Command In Multiple Directories
我的Linux服務器上有大量的單一安裝WordPress網站。 我想創建一個文本文件,其中包含我的網站群組的目錄名稱。
例如,我所有的網站都位於/var/www/vhosts
而我可能希望將100個網站集中在一個文本文件中,例如
site1
site2
site3
site4
如何編寫僅循環訪問組文本文件中指定的目錄並執行命令的腳本。 我的目標是符號鏈接某些WordPress插件,如果我可以創建組並在該組目錄中執行命令,則我不想手動逐個目錄。
對於組文件中的每個站點,請轉至/wp-content/plugins
文件夾並執行指定的symlink命令。
根據您的目標,您可以使用find
和-exec
操作使用單行代碼來實現。 我傾向於將其作為Bash循環來進行,因為添加其他命令比處理冗長而笨拙的命令要容易得多,並且可以處理錯誤。
我不知道這是否是您的打算,但這是一個建議。
#!/bin/bash
# Receives site group file as argument1
# Receives symlink name as argument 2
# Further arguments will be passed to the command called
sites_dir="/var/www/vhosts"
commands_dir="wp-content/plugins"
if ! [[ -f $1 ]] ; then
echo "Site list not found : $1"
exit
fi
while IFS= read -r site
do
site_dir="$sites_dir/$site"
if ! [[ -d $site_dir ]] ; then
echo "Unknown site : $site"
continue
fi
command="$site_dir/$commands_dir/$2"
if ! [[ -x $command ]] ; then
echo "Missing or non-executable command : $command"
continue
fi
"$command" "${@:3}"
done <"$1"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.