简体   繁体   English

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

[英]bash scripting - how to process a comma separated list as input parameters to a function

I'm trying to figure out how to pass a comma , separated list as inputs and to have my function process the values one at a time.我试图弄清楚如何将逗号分隔的列表作为输入传递,并让我的函数一次处理一个值。

My function:我的功能:

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
}

I run my script like this:我像这样运行我的脚本:

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

My code in progress:我的代码正在进行中:

#!/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

However, the problem I'm having when I call that function is that inserts the values as a single string:但是,我在调用该函数时遇到的问题是将值作为单个字符串插入:

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

Expecting:期待:

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

Since your script receives a comma-separated list of IPs, effectively something like:由于您的脚本接收以逗号分隔的 IP 列表,因此实际上类似于:

_ip="123.456.790.007,123.456.790.008"

you need to split the list by , and wrap each IP with quotes before giving it to Mongo's $each operator.您需要将列表拆分为,并用引号将每个 IP 包装起来,然后再将其提供给 Mongo 的$each运算符。

First, localize redefinition of IFS to the read command only, then exploit printf 's behaviour of reusing the same format string when more arguments are provided (the expanded array "${arr[@]}" ), store the result to a variable via -v option, and strip the leading , from the result:首先,将IFS重定义仅本地化为read命令,然后利用printf在提供更多参数时重用相同格式字符串的行为(扩展数组"${arr[@]}" ),将结果存储到变量通过-v选项,并从结果中去除前导,

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

For the _ip value given above, this script will store in ips variable:对于上面给出的_ip值,此脚本将存储在ips变量中:

"123.456.790.007","123.456.790.008"

So, applying this to your program, first change your i) case like:因此,将此应用于您的程序,首先更改您的i)案例,例如:

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

then fix the addToWhitelist function:然后修复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"
}

Try below,下面试试,

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

So the complete script will come like this,所以完整的脚本会是这样的,

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM