解析 jq 过滤器的参数
parsing arguments to jq filter
这是我的配置:
{
"students": ""
}
还有我的bash:
#!/bin/bash
# Default values of arguments
SHOULD_INITIALIZE=0
CACHE_DIRECTORY="/etc/cache"
ROOT_DIRECTORY="/etc/projects"
OTHER_ARGUMENTS=()
# Loop through arguments and process them
for arg in "$@"; do
case $arg in
-i | --initialize)
SHOULD_INITIALIZE=1
shift # Remove --initialize from processing
;;
-c=* | --cache=*)
CACHE_DIRECTORY="${arg#*=}"
shift # Remove --cache= from processing
;;
-r | --root)
ROOT_DIRECTORY=""
shift # Remove argument name from processing
shift # Remove argument value from processing
;;
*)
OTHER_ARGUMENTS+=("")
shift # Remove generic argument from processing
;;
esac
done
echo "${OTHER_ARGUMENTS[0]}" # ScheduleSQL.json
echo "${OTHER_ARGUMENTS[1]}" # root
echo "${OTHER_ARGUMENTS[2]}" # .students
echo "${OTHER_ARGUMENTS[3]}" # Andrei
# PRJNAME='prj1'
#
# echo $PRJNAME
#
# jq --arg v "$PRJNAME" '.dev.projects."$v"' config.json
# jq '.dev.projects.prj1' config.json
cd "${OTHER_ARGUMENTS[1]}"
# jq '.students += Andrei' ScheduleSQL.json
jq --arg entry "${OTHER_ARGUMENTS[3]}" --arg destination "${OTHER_ARGUMENTS[2]}" '$destination[$entry]' "${OTHER_ARGUMENTS[0]}"
#jq '"${OTHER_ARGUMENTS[2]}" + += + ""${OTHER_ARGUMENTS[3]}"" + "${OTHER_ARGUMENTS[0]}"
我只想用一个变量填充“学生”,为此我使用 jq
,但出现此错误:
$ jq --arg entry ".students" --arg destination "Andrei" '$destination[$entry]' ScheduleSQL.json
jq: error (at ScheduleSQL.json:2): Cannot index string with string ".students"
实际上,想要修改 JSON 并得到:
这是我的配置:
{
"students": "Andrei"
}
我做错了什么?
jq 键和值的变量:
jq --arg KEY "students" --arg VALUE "Andrei" '{($KEY):$VALUE}' file
输出:
{
"students": "Andrei"
}
这是我的配置:
{
"students": ""
}
还有我的bash:
#!/bin/bash
# Default values of arguments
SHOULD_INITIALIZE=0
CACHE_DIRECTORY="/etc/cache"
ROOT_DIRECTORY="/etc/projects"
OTHER_ARGUMENTS=()
# Loop through arguments and process them
for arg in "$@"; do
case $arg in
-i | --initialize)
SHOULD_INITIALIZE=1
shift # Remove --initialize from processing
;;
-c=* | --cache=*)
CACHE_DIRECTORY="${arg#*=}"
shift # Remove --cache= from processing
;;
-r | --root)
ROOT_DIRECTORY=""
shift # Remove argument name from processing
shift # Remove argument value from processing
;;
*)
OTHER_ARGUMENTS+=("")
shift # Remove generic argument from processing
;;
esac
done
echo "${OTHER_ARGUMENTS[0]}" # ScheduleSQL.json
echo "${OTHER_ARGUMENTS[1]}" # root
echo "${OTHER_ARGUMENTS[2]}" # .students
echo "${OTHER_ARGUMENTS[3]}" # Andrei
# PRJNAME='prj1'
#
# echo $PRJNAME
#
# jq --arg v "$PRJNAME" '.dev.projects."$v"' config.json
# jq '.dev.projects.prj1' config.json
cd "${OTHER_ARGUMENTS[1]}"
# jq '.students += Andrei' ScheduleSQL.json
jq --arg entry "${OTHER_ARGUMENTS[3]}" --arg destination "${OTHER_ARGUMENTS[2]}" '$destination[$entry]' "${OTHER_ARGUMENTS[0]}"
#jq '"${OTHER_ARGUMENTS[2]}" + += + ""${OTHER_ARGUMENTS[3]}"" + "${OTHER_ARGUMENTS[0]}"
我只想用一个变量填充“学生”,为此我使用 jq
,但出现此错误:
$ jq --arg entry ".students" --arg destination "Andrei" '$destination[$entry]' ScheduleSQL.json
jq: error (at ScheduleSQL.json:2): Cannot index string with string ".students"
实际上,想要修改 JSON 并得到: 这是我的配置:
{
"students": "Andrei"
}
我做错了什么?
jq 键和值的变量:
jq --arg KEY "students" --arg VALUE "Andrei" '{($KEY):$VALUE}' file
输出:
{ "students": "Andrei" }