bash - 如何将字符串数组传递给 Bash 脚本并加入该数组?
问题描述
我想在 string 上加入一个字符串数组"%2C+"
。我的 shell 脚本launch
看起来像这样。
#!/bin/bash
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
selectQuery=$(join_by "%2C+" $1)
echo selectQuery
但是当我运行时./download-data $("state_code" "county_code")
,我在终端中收到此错误:bash: state_code: command not found
.
我需要将参数作为数组传递,因为我计划稍后传递更多数组。类似的东西./download-data $("state_code" "county_code") $("more" "string")
。
解决方案
让您的脚本在单独的参数中接受多个字符串:
#!/bin/bash
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
selectQuery=$(join_by "%2C+" "$@")
echo "$selectQuery"
然后使用多个参数运行它:
./download-data "state_code" "county_code"
推荐阅读
- parsing - 语法中的 SR 冲突 - 如何解决?(柠檬/yacc)
- java - 对快速排序和归并排序进行基准测试可以得出归并排序更快
- reactjs - 从 react-scripts 3.2.0 更新到 3.3.0 时,为什么我的 react 项目会崩溃?
- android - 似乎不支持在模态页面中进行正常导航 [Xamarin.Forms]
- javascript - 混淆 javascript 错误:无法读取 null 的属性“类型”
- javascript - 在猫鼬中更新文档的最佳方法是什么?
- r - 当excel文件存在或工作表存在时如何在R中使用write.xlsx
- java - 选择带有偏移量支持的一定数量的最近日期的时间戳记录
- javascript - 使用数组索引 0 更新 chart.js 不起作用
- rust - 如何在自定义全局分配器中保持状态?