我有以下 Bash 脚本:
#!/bin/bash
input=true
input="$($(IFS=, read -r s ; echo "${s}bool") <<< "$input")"
IFS=,; set -f; set -- $input; out=
for i in "$@"; do
case "$i" in
"")
echo "empty input not allowed"
exit 0
;;
*bool)
if [[ "${s}" == "true" || "${s}" == "false" ]]; then
out="$out,${i/%bool/}"
else
echo "value not allowed" && exit 0
fi
;;
esac
done
echo "${out:1}"
输入来自 TUI 界面,输出将作为布尔值用于 SQL 插入语句。这是一个较大脚本的简化版本,因此有 for 循环和 case 语句。如果我运行该脚本,则不会得到任何输出,也不会出现任何错误消息。
我想要实现的目标是真的或者错误的作为输出,取决于 $input 是否真的或者错误的。请"")
在case语句中忽略。
有人可以帮忙吗?
答案1
如果我运行该脚本,则不会得到任何输出,也不会有任何错误消息。
你应该(除非你是bash
专家)利用ShellCheck – shell脚本分析工具当您的脚本遇到问题时。
通过 ShellCheck 运行脚本会返回以下错误:
$ shellcheck myscript
Line 5:
input="$((IFS=, read -r s ; echo "${s}bool" )<<<"$input")"
^-- SC1102: Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.
^-- SC2030: Modification of s is local (to subshell caused by (..) group).
Line 7:
IFS=,; set -f; set -- $input; out=
^-- SC2086: Double quote to prevent globbing and word splitting.
Did you mean: (apply this, apply all SC2086)
IFS=,; set -f; set -- "$input"; out=
Line 12:
*bool) if [[ "${s}" == "true" || "${s}" == "false" ]] ; then out="$out,${i/%bool/}" else echo "value not allowed" && exit 0 ; fi;;
^-- SC2031: s was modified in a subshell. That change might be lost.
^-- SC2031: s was modified in a subshell. That change might be lost.
$
修复错误然后再次检查脚本。
冲洗并循环直到你的脚本正常运行。