2013-04-26 178 views
1

可以一個bash /殼大師幫我做一個非常簡單的bash腳本處理以下 - IM艱難地沿着這些線路bash腳本:處理參數

輸入是如下

./script #channel1,#channel2,#channel3 "This is the message" 
工作

,或者如果更容易..

./script #channel1,#channel2,#channel3 -m This is the message 

(在後-m什麼是消息)

現在,我通過各渠道的要循環和echo消息,即

for channel in channels 
    echo channel $message 
fi 

感謝

+0

你真的有你的'channel' arg用'#'開始?如果是這樣,那可能是問題的核心。 '#'告訴bash從那裏到結尾的所有內容都是註釋。 – Randall 2017-04-07 15:52:30

回答

0

如果你正在寫它,它會更容易做

usage() 
{ 
    echo "usage: $0 <MESSAGE> <CHANNELS>" 
    exit 
} 

[[ $3 ]] || usage 
message=$1 
shift 

for channel 
do 
    echo $channel $message 
done 
+0

嗨感謝您的反饋,但我無法得到它的工作...我輸入的任何東西只是提出使用 ...? – 2013-04-26 02:02:39

+0

./script「這是一條消息」channel1,channel2出現'usage blah'...? – 2013-04-26 02:16:32

+1

@TylerEvans:不要在您的頻道列表中包含逗號。回想一下,$ 3表示第三個參數,並且在您的示例中,您只有兩個參數(因爲shell分隔引用的字符串,然後是空格(而不是逗號))。祝你們好運。 – shellter 2013-04-26 02:23:55

0
channels=$1 
message=$2 
IFS=, 
for channel in $channels 
do echo $channel $message 
done 

例如:

0>./script channel1,channel2,channel3 "This is the message" 
channel1 This is the message 
channel2 This is the message 
channel3 This is the message