2013-04-26 229 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