bash脚本 - 如何处理逗号分隔列表作为函数的输入参数

时间:2017-10-17 04:50:53

标签: bash unix scripting getopts

我正在尝试弄清楚如何将逗号,分隔列表作为输入传递,并让我的函数一次处理一个值。

我的功能:

addToWhitelist ()
{
   host='127.0.0.1'
   db='mytest'
   _mongo=$(which mongo);
   echo "${_ip}";
   read -a arr <<<${_ip};
   for i in ${arr[@]};
   do
     exp="db.account.update({\"account\":'${_account}'},{\$addToSet:{\"ip_list\": {\$each:['${_ip}'] }}})";
     ${_mongo} ${host}/${db} --eval "$exp"
   done
}

我像这样运行我的脚本:

./myscript.sh -m add -a pizzahut -i 123.456.790.007,123.456.790.008

我的代码正在进行中:

#!/usr/local/bin/bash
set -e
set -x

# Usage for getopts
usage () {
    echo "Example: $0 -m find -a pizzahut"
    echo "Example: $0 -m add -a pizzahut -i 10.10.123.456"
    exit 1;
}

while getopts ":m:a:i:" o; do
  case "${o}" in
    m)
    _mode=${OPTARG}
         if [[ "${_mode}" != find && "${_mode}" != add ]]; then
        usage
        fi
    ;;
    a)
    _account=${OPTARG}
    ;;
    i)
    _ip=${OPTARG}
        set -f
        IFS=,
    ;;
    *)
       usage
       ;;
  esac
done
shift $((OPTIND-1))


getWhitelist ()
{
   host='127.0.0.1'
   db='mytest'
   _mongo=$(which mongo);
   exp="db.account.find({\"account\":'${_account}'},{ip_list: 1}).pretty();";
   ${_mongo} ${host}/${db} --eval "$exp"
}

# Read a list
addToWhitelist ()
{
   host='127.0.0.1'
   db='mytest'
   _mongo=$(which mongo);
   echo "${_ip}";
   read -a arr <<<${_ip};
   for i in ${arr[@]};
   do
     exp="db.account.update({\"account\":'${_account}'},{\$addToSet:{\"ip_list\": {\$each:['${_ip}'] }}})";
     ${_mongo} ${host}/${db} --eval "$exp"
   done
}


case "${_mode}" in
  'find')
      echo "Finding information for the account ${_account}"
      getWhitelist
      ;;
  'add')
      echo "Adding the following IP: ${_ip}"
      addToWhitelist
      ;;
esac

set +x

但是,当我调用该函数时,我遇到的问题是将值作为单个字符串插入:

  "ip_list" : [
    "123.456.790.006",
    "123.456.790.007,123.456.790.008",
    "123.456.790.009"
  ]
}

期待:

  "ip_list" : [
    "123.456.790.006",
    "123.456.790.007",
    "123.456.790.008",
    "123.456.790.009"
  ]
}

2 个答案:

答案 0 :(得分:1)

尝试以下,

  

ip = $(echo $ {OPTARG} | sed“s /,/,\ n / g”)

所以完整的脚本会像这样,

#!/usr/local/bin/bash
set -e
set -x

# Usage for getopts
usage () {
    echo "Example: $0 -m find -a pizzahut"
    echo "Example: $0 -m add -a pizzahut -i 10.10.123.456"
    exit 1;
}

while getopts ":m:a:i:" o; do
  case "${o}" in
    m)
    _mode=${OPTARG}
         if [[ "${_mode}" != find && "${_mode}" != add ]]; then
        usage
        fi
    ;;
    a)
    _account=${OPTARG}
    ;;
     i)
    _ip=$(echo ${OPTARG}|sed "s/,/,\n/g")
        set -f
        IFS=,
    ;;
    *)
       usage
       ;;
  esac
done
shift $((OPTIND-1))
getWhitelist ()
{
   host='127.0.0.1'
   db='mytest'
   _mongo=$(which mongo);
   exp="db.account.find({\"account\":'${_account}'},{ip_list: 1}).pretty();";
   ${_mongo} ${host}/${db} --eval "$exp"
}

# Read a list
addToWhitelist ()
{
   host='127.0.0.1'
   db='mytest'
   _mongo=$(which mongo);
   echo "${_ip}";
   read -a arr <<<${_ip};
   for i in ${arr[@]};
   do
   exp="db.account.update({\"account\":'${_account}'},{\$addToSet:{\"ip_list\": {\$each:['${_ip}'] }}})";
     ${_mongo} ${host}/${db} --eval "$exp"
   done
}


case "${_mode}" in
  'find')
      echo "Finding information for the account ${_account}"
      getWhitelist
      ;;
  'add')
   echo "Adding the following IP: ${_ip}"
      addToWhitelist
      ;;
esac

set +x

答案 1 :(得分:1)

由于您的脚本收到以逗号分隔的IP列表,因此实际上类似于:

_ip="123.456.790.007,123.456.790.008"

您需要按,拆分列表,然后将每个IP用引号括起来,然后再将其提供给Mongo的$each运营商。

首先,仅将IFS重新定义到read命令,然后利用printf在提供更多参数时重用相同格式字符串的行为(扩展数组"${arr[@]}"),通过-v选项将结果存储到变量中,并从结果中删除前导,

IFS=, read -a arr <<<"${_ip}"
printf -v ips ',"%s"' "${arr[@]}"
ips="${ips:1}"

对于上面给出的_ip值,此脚本将存储在ips变量中:

"123.456.790.007","123.456.790.008"

因此,将此应用于您的计划,首先更改您的i)案例,如:

case "${o}" in
    # ...
    i) _ip=${OPTARG};;
esac

然后修复addToWhitelist函数:

addToWhitelist() {
    host='127.0.0.1'
    db='mytest'
    _mongo=$(which mongo)
    IFS=, read -a arr <<<"${_ip}"
    printf -v ips ',"%s"' "${arr[@]}"
    ips="${ips:1}"
    exp="db.account.update({'account': '${_account}'}, {\$addToSet:{'ip_list': {\$each:[$ips]}}})";
    "${_mongo}" "${host}/${db}" --eval "$exp"
}