Estou usando uma caixa de diálogo para modificar um único valor em um arquivo:
#!/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
Mas executando este script eu chego test.md
nestes valores:
# 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
Como posso remover quaisquer caracteres de controle UPSTREAM_VERSION
anteriores echo
a ele no arquivo?
Chamar
prompt_and_save
gatilhosclear
para gerar caracteres de controle antes que qualquer valor seja impresso porecho
Eles são atribuídos à
UPSTREAM_VERSION
variável.Para resolver o problema eu removo
clear
daprompt_and_save
função:Depois de chamar
prompt_and_save
, você pode chamarclear
, como seu script faz:O problema é que você está chamando
clear
. Isso funciona enviando sequências de escape parastdout
.Você pode ver isso observando a saída:
Quando você fizer
UPSTREAM_VERSION=$(prompt_and_save ...)
isso, toda a saída deprompt_and_save
será salva na variável. Então agora você$UPSTREAM_VERSION
incluirá esses caracteres.Mova a
clear
parte externa da função ou envie-a à força para a tela comclear > /dev/tty