簡體   English   中英

在shell腳本中轉​​義星號

[英]Escape asterisk in shell script

下面的數組中的星號符號在分配給數組時會擴展到文件列表中。

u='*','john'
IFS=$',';q=($u)
for j in "${!q[@]}"
do
        echo "drop user ${q[j]}"
done

輸出是:

drop user abc
drop user test.sh
drop user test1.sh
drop user john

我打算得到的是:

drop user *
drop user john

我怎么能逃過星號?

您可以使用此腳本:

u='*,john'

# read comma delimited string into an array
IFS=, read -ra q <<< "$u"

# check content of array q
declare -p q

# loop through array q
for j in "${q[@]}"
do
    echo "drop user $j"
done

輸出:

declare -a q=([0]="*" [1]="john")
drop user *
drop user john

暫無
暫無

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

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