Bash脚本:处理参数

时间:2013-04-26 01:41:57

标签: bash shell arguments

bash / shell大师可以帮我制作一个非常简单的bash脚本来处理以下内容 - 我很难让它按照这些方式工作

输入如下

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

或者更容易..

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

(-m之后的任何内容都是消息)

现在我想循环遍历每个频道,并回显消息,即

for channel in channels
    echo channel $message
fi

感谢

2 个答案:

答案 0 :(得分:0)

如果你正在写它,那就更容易了

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

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

for channel
do
  echo $channel $message
done

答案 1 :(得分: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