[英]Bash pipe delimited string into command's arguments
讓我們加密允許您指定多個允許的域:
certbot certonly -d foo.example.com -d bar.example.com
有沒有一種方法來傳遞逗號分隔的字符串,以便每個分隔的元素都可以用作參數? 就像是:
echo 'foo.example.com,bar.example.com' | magic_function 'certbot certonly {-d}'
這感覺類似於xargs
,但是我希望所有令牌最終都分配給同一進程。
(事實證明,certbot只會接受以逗號分隔的域列表,但是如果不接受,該怎么辦?)
我認為這需要使用數組來實際構建命令。 假設您有一個以逗號分隔的URL列表作為輸入。 首先將它們讀入數組
inputStr='foo.example.com,bar.example.com'
IFS=, read -ra urlList <<<"$inputStr"
現在,使用-d
開關使用數組構造命令。
domainList=()
for url in "${urlList[@]}"; do
domainList+=(-d "$url")
done
現在將構造的數組傳遞給命令
certbot certonly "${domainList[@]}"
對此進行擴展,只需簡單地使其成為需要一個URL列表並在其上運行命令的函數
runCertbot() {
(( $# )) || { printf 'insufficient args provided\n' >&2; return 1; }
IFS=, read -ra urlList <<<"$@"
domainList=()
for url in "${urlList[@]}"; do
domainList+=(-d "$url")
done
certbot certonly "${domainList[@]}"
}
並如下調用函數
runCertbot 'foo.example.com,bar.example.com'
怎么樣
certbot certonly -d $(echo 'foo.example.com,bar.example.com' | sed -e 's/,/ -d /')
'sed'用'-d'替換每個逗號。 您只需要添加前導“ -d”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.