From ec648e9c806c457059f4cefa2c8d314fced1287e Mon Sep 17 00:00:00 2001 From: loyess Date: Mon, 30 May 2022 17:40:44 +0800 Subject: [PATCH] Replace null with empty when jq fails to get value. --- service/go-shadowsocks2.sh | 12 ++++++------ service/rabbit-tcp.sh | 8 ++++---- service/shadowsocks-rust.sh | 4 +++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/service/go-shadowsocks2.sh b/service/go-shadowsocks2.sh index b5e66b9..bb24b3d 100644 --- a/service/go-shadowsocks2.sh +++ b/service/go-shadowsocks2.sh @@ -73,13 +73,13 @@ get_config_args(){ exit 1 fi - ServerPort=$(cat ${JsonFilePath} | jq -r .server_port) + ServerPort=$(cat ${JsonFilePath} | jq -r '.server_port // empty') [ -z "$ServerPort" ] && echo -e "Configuration option 'server_port' acquisition failed" && exit 1 - Password=$(cat ${JsonFilePath} | jq -r .password) + Password=$(cat ${JsonFilePath} | jq -r '.password // empty') [ -z "$Password" ] && echo -e "Configuration option 'password' acquisition failed" && exit 1 - Method=$(cat ${JsonFilePath} | jq -r .method) + Method=$(cat ${JsonFilePath} | jq -r '.method // empty') [ -z "$Method" ] && echo -e "Configuration option 'method' acquisition failed" && exit 1 - Mode=$(cat ${JsonFilePath} | jq -r .mode) + Mode=$(cat ${JsonFilePath} | jq -r '.mode // empty') [ -z "$Mode" ] && echo -e "Configuration option 'Mode' acquisition failed" && exit 1 if [[ ${Method} == "aes-128-gcm" ]]; then @@ -99,9 +99,9 @@ get_config_args(){ fi if $(cat ${JsonFilePath} | grep -qE 'plugin|plugin_opts'); then - Plugin=$(cat ${JsonFilePath} | jq -r .plugin) + Plugin=$(cat ${JsonFilePath} | jq -r '.plugin // empty') [ -z "$Plugin" ] && echo -e "Configuration option 'plugin' acquisition failed" && exit 1 - PluginOpts=$(cat ${JsonFilePath} | jq -r .plugin_opts) + PluginOpts=$(cat ${JsonFilePath} | jq -r '.plugin_opts // empty') [ -z "$PluginOpts" ] && echo -e "Configuration option 'plugin_opts' acquisition failed" && exit 1 fi } diff --git a/service/rabbit-tcp.sh b/service/rabbit-tcp.sh index 36c904d..6e38420 100644 --- a/service/rabbit-tcp.sh +++ b/service/rabbit-tcp.sh @@ -59,13 +59,13 @@ if [ ! "$(command -v jq)" ]; then exit 1 fi -Mode=$(cat ${CONF} | jq -r .mode) +Mode=$(cat ${CONF} | jq -r '.mode // empty') [ -z "$Mode" ] && echo -e "Configuration option 'mode' acquisition failed" && exit 1 -RabbitAddr=$(cat ${CONF} | jq -r .rabbit_addr) +RabbitAddr=$(cat ${CONF} | jq -r '.rabbit_addr // empty') [ -z "$RabbitAddr" ] && echo -e "Configuration option 'rabbit_addr' acquisition failed" && exit 1 -PassWord=$(cat ${CONF} | jq -r .password) +PassWord=$(cat ${CONF} | jq -r '.password // empty') [ -z "$PassWord" ] && echo -e "Configuration option 'password' acquisition failed" && exit 1 -Verbose=$(cat ${CONF} | jq -r .verbose) +Verbose=$(cat ${CONF} | jq -r '.verbose // empty') [ -z "$Verbose" ] && echo -e "Configuration option 'verbose' acquisition failed" && exit 1 diff --git a/service/shadowsocks-rust.sh b/service/shadowsocks-rust.sh index 53e7eef..36aec17 100644 --- a/service/shadowsocks-rust.sh +++ b/service/shadowsocks-rust.sh @@ -84,7 +84,9 @@ get_config_args(){ exit 1 fi - NameServer=$(cat ${JsonFilePath} | jq -r .nameserver) + # ref: https://stackoverflow.com/questions/53135035/jq-returning-null-as-string-if-the-json-is-empty + # ref: https://github.com/stedolan/jq/issues/354#issuecomment-43147898 + NameServer=$(cat ${JsonFilePath} | jq -r '.nameserver // empty') [ -z "$NameServer" ] && echo -e "Configuration option 'nameserver' acquisition failed" && exit 1 }