我正在使用对话框来将单个值修改到文件中:
#!/usr/bin/env bash
prompt_and_save() {
local file=$1
local message=$2
local value=""
# Check if file exists and read value
if [ -f "$file" ]; then
value=$(cat "$file")
fi
# Prompt user with dialog
value=$(dialog --inputbox "$message" 8 50 "$value" 3>&1 1>&2 2>&3)
# Save the value if not empty
if [ ! -z "$value" ]; then
echo "$value" > "$file"
fi
clear
# Return the value
echo "$value"
}
UPSTREAM_VERSION=$(prompt_and_save "VERSION" "Bump the version (or keep it the same)")
clear
NEW_ENTRY="# Version $UPSTREAM_VERSION $DATE"
RELEASE_NOTES=$(cat RELEASE_NOTES)
echo -e "$NEW_ENTRY\n${RELEASE_NOTES}" > test.md
但运行这个脚本我得到了test.md
这些值:
# Version ^[[H^[[2J^[[3J0.2.0
1. Split codebase into multiple files.
2. Use a seperate version file and define built version upon compile.
4. [BUGFIX] If input file is same as output file copy input file into a temporary one.
5. Improved Documentation
6. [BUGFIX] Out of bounds argument parsing
7. [BUGFIX] Values should not be an Argument
我怎样才能从文件中删除它UPSTREAM_VERSION
之前的所有控制字符?echo
在打印任何值之前,调用
prompt_and_save
触发器来输出控制字符clear
echo
这些被分配给
UPSTREAM_VERSION
变量。为了解决这个问题,我
clear
从prompt_and_save
功能中删除了:调用 之后
prompt_and_save
,您可以调用clear
,就像您的脚本所做的那样:问题是你正在调用
clear
。这是通过向 发送转义序列来实现的stdout
。您可以通过查看输出来看到这一点:
当你
UPSTREAM_VERSION=$(prompt_and_save ...)
这样做时,所有的输出都prompt_and_save
将保存在变量中。所以现在你$UPSTREAM_VERSION
将包括这些字符。将其移到
clear
函数外部,或者使用以下命令强制将其发送到屏幕clear > /dev/tty