简体   繁体   English

使用带有空格和多个命令的参数构造命令bash

[英]Construct command with arguments with spaces and multiple commands bash

Ok I have a script I'm writing in bash for cloud foundry to add services to an organization. 好的,我有一个用bash编写的脚本,用于Cloud Foundry向组织添加服务。

As a part of the requirement the user needs to be able to pass in the services as an argument. 作为要求的一部分,用户需要能够传递服务作为参数。 The create service command in CF takes 3 arguments the service name, plan, custom-name. CF中的create service命令使用3个参数作为服务名称,计划,自定义名称。

The arg is passed in like -a "servicename planname customname, nextservice nextplan nextname" arg像-a“ servicename planname customname,nextservice nextplan nextname”一样传入

I was thinking that in order to do a double parse on this that I would have to do it like this 我在想,为了对此进行双重解析,我必须这样做

-a "servicename; planname; customname, nextservice; nextplan; nextname" -a“服务名;计划名;定制名,nextservice; nextplan; nextname”

then we call the command to add it cf create-service servicename planname customname 然后我们调用命令将其添加到cf create-service servicename planname customname

or if it has spaces in the name 或名称中是否包含空格

cf create-service "servicename" "planname" "customname" cf create-service“服务名称”“计划名称”“自定义名称”

So with that said I have my script working just fine and you can give as many services as you want comma delimited. 如此说来,我的脚本就可以正常工作,并且您可以根据需要使用逗号分隔提供尽可能多的服务。 But the problem I have is I have one service in our organization that has a space in the service name and a space in the plan name. 但是我遇到的问题是我的组织中有一项服务,服务名称中有一个空格,而计划名称中有一个空格。

So I've been trying to figure out how to build my function to parse through string to build out the command. 因此,我一直在尝试找出如何构建函数以通过字符串进行解析以构建命令。 I have a feeling I need 2 different for loops one that will parse out the entire string pre comma and then a 2nd for loop to build the command with and if there are any spaces found in the args to wrap them in quotes. 我有一种感觉,我需要2个不同的for循环,一个将解析整个字符串的逗号前,然后是2nd for循环以构建命令,以及在args中是否发现任何空格来将其包装在引号中。

So this is what I have thus far 所以这就是我到目前为止

 add_service() {
  if [ -n "${ADDSERVICEID}" ]
    then
      if [ "${ADDSERVICEID}" == "default" ]
        then
          ....Our Default services are here
          echo "The following services are now enabled in your organization"
          cf services
      else
        IFS=","
        for i in "${ADDSERVICEID[@]}"; do
          if [[ $i =~ \ ]]; then
            $i=\"$i\"
          fi
            for i in "${FULLSERVICE[@]}"; do
               $i
              echo "The following services are now enabled in your organization"
              cf services
            done
          done
      fi
  fi
}

I'm basically working on the else section as my default section since they're all static work just fine. 我基本上是将else部分作为我的默认部分,因为它们都是静态工作。 I'm sure I'm probably over thinking this and the answer is probably staring me right in the face but I'm just having an issue wrapping my head around it. 我敢肯定,我可能会想过这个问题,答案可能正盯着我,但我只是遇到了一个问题。 Also changing the names of the service or plan to not have spaces is not an option for us. 同样,将服务或计划的名称更改为不包含空格也不是我们的选择。

Thank you all for your help and I hope that once we have an answer to this it will help others down the road. 谢谢大家的帮助,我希望一旦我们对此做出答复,将会对其他人有所帮助。

UPDATE: Ok so its now sort of working. 更新:好的,现在它可以正常工作了。 I can add my services with spaces in their names. 我可以在服务名称中添加空格。 Great. 大。

Now however if I give it more than one service to add it just slams it all together in one statement instead of parsing on the , 但是,现在,如果我提供了一项以上的服务来添加它,则只需将它们全部合并到一个语句中,而不是对进行解析,

Here is the updated code 这是更新的代码

  else
    IFS=","
    for arg1 in "${ADDSERVICEID[@]}"; do
      IFS=";"
        CREATESERVICE=""
        for arg2 in "${arg1[@]}"; do
          if [[ $arg2 =~ \  ]]
           then
            $arg2=\'\"$arg2\"\'
          fi
          CREATESERVICE+="${arg2}"
          echo ${CREATESERVICE}
          cf create-service ${CREATESERVICE}
          echo "The following services are now enabled in your organization"
          cf services
        done
    done
  fi

I only pasted in from the else onward 我只是从其他地方粘贴

UPDATE: Duh you gotta remove some quotes no wonder it was picking it all up as one string. 更新:嗯,您必须删除一些引号,也就不足为奇了。 Sigh I think its time for bed igh,我认为该睡觉了

else
  IFS=,
  for arg1 in ${ADDSERVICEID[@]}; do
    IFS=";"
      CREATESERVICE=""
      for arg2 in "${arg1[@]}"; do
        if [[ $arg2 =~ \  ]]
         then
          $arg2=\'\"$arg2\"\'
        fi
        CREATESERVICE+=${arg2}
        echo ${CREATESERVICE}
        cf create-service ${CREATESERVICE}

      done
  done
  echo "The following services are now enabled in your organization"
  cf services
fi

This now works as it should, it takes the args, if there are spaces in them we wrap the arg in quotes, we parse it on the , and the ; 现在这可以正常工作了,它需要使用args,如果其中有空格,则将arg括在引号中,然后在,和上解析它。 of it so it works like a champ now. 因此,它现在就像冠军。

This is the final rendition of this portion of the function for now as it is doing what I wanted it to do 这是函数此部分的最终解释,因为它正在执行我想要的操作

else
  IFS=,
  for arg1 in ${ADDSERVICEID[@]}; do
    IFS=";"
      CREATESERVICE=""
      for arg2 in "${arg1[@]}"; do
        if [[ $arg2 =~ \  ]]
         then
          $arg2=\'\"$arg2\"\'
        fi
        CREATESERVICE+=${arg2}
        echo ${CREATESERVICE}
        cf create-service ${CREATESERVICE}

      done
  done
  echo "The following services are now enabled in your organization"
  cf services
fi

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

相关问题 Bash 多个命令行参数 - Bash multiple command line arguments 如何编写 bash function 来打印和运行命令,当命令有 arguments 带空格或要扩展的东西时 - How to write bash function to print and run command when the command has arguments with spaces or things to be expanded BASH 将命令行参数存储为单独的变量 - BASH store command line arguments as separate variables 在 Bash 中将命令行参数转换为数组 - Convert command line arguments into an array in Bash 如何将一个 bash 脚本中的 arguments 数组传递给另一个 arguments 有空格的脚本 - How to pass array of arguments in one bash script to another where some arguments have spaces 将多个 arrays 作为 arguments 传递给 Bash 脚本? - Pass multiple arrays as arguments to a Bash script? 如何将命令 arguments 解析为数组(命令是用户输入的) - How to parse a commands arguments into an array (command is user inputted) 关联数组键中的空格,同时在Bash中循环遍历多个数组 - Spaces in Associative Array Keys while looping through multiple arrays in Bash 在 bash 中的多个目录中使用空格的列出文件中搜索 substring - Search for a substring within listed files with spaces from multiple directories in bash bash数组的元素作为python的单独命令行参数 - Elements of a bash array as separate command line arguments to python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM