Better sed

This commit is contained in:
Tiago Almeida 2019-12-27 15:39:21 +00:00
parent 5d5521c048
commit 7c7fe0928d
2 changed files with 13 additions and 28 deletions

View File

@ -34,8 +34,8 @@ G_DEPS="unzip sed"
G_FILE="${HOME}/.nanorc" G_FILE="${HOME}/.nanorc"
G_REPO_MASTER="https://github.com/scopatz/nanorc/archive/master.zip" G_REPO_MASTER="https://github.com/scopatz/nanorc/archive/master.zip"
G_REPO_RELEASE="https://github.com/scopatz/nanorc/archive/${G_VERSION}.zip" G_REPO_RELEASE="https://github.com/scopatz/nanorc/archive/${G_VERSION}.zip"
G_NANO_VERSION="4.6.0" # G_NANO_VERSION="4.6.0"
G_NANO_NRC_DIR="" # G_NANO_NRC_DIR=""
unset G_LITE G_UNSTABLE G_VERBOSE G_DIR G_THEME unset G_LITE G_UNSTABLE G_VERBOSE G_DIR G_THEME
# Exit Values Help # Exit Values Help
@ -175,6 +175,9 @@ f_install(){
begin="# BEGIN" begin="# BEGIN"
end="# END" end="# END"
theme="${G_DIR}/themes/${G_THEME}/" theme="${G_DIR}/themes/${G_THEME}/"
# Sed rules : skip over the line "$begin" tag and before "$end" tag
sed_lite="$(printf "/%s/,/%s/{ /%s/n /%s/ !{ s/*// r '%s/config' r '%s' d }}" "$begin" "$end" "$begin" "$end" "$theme" "$(f_get_nanorc)")"
sed_no_lite="$(printf "/%s/,/%s/{ /%s/n /%s/ !{ s/*// r '%s/config' r 'include /nanorc/*' d }}" "$begin" "$end" "$begin" "$end" "$theme")"
if cd "$HOME"; then if cd "$HOME"; then
printf "\n Error: Cannot open or access '%s' directory." "${HOME}" printf "\n Error: Cannot open or access '%s' directory." "${HOME}"
@ -215,25 +218,9 @@ f_install(){
printf "\n %s \n %s \n" "$begin" "$end" >> "$G_FILE" printf "\n %s \n %s \n" "$begin" "$end" >> "$G_FILE"
if [ "$G_LITE" = true ]; then if [ "$G_LITE" = true ]; then
sed -n -i.bkp '/'"$begin"'/,/'"$end"'/ { sed -n -i.bkp '$sed_lite' "$G_FILE"
/'"$begin"'/n # skip over the line that has "$begin" on it
/'"$end"'/ !{ # skip over the line that has "$end" on it
s/*//
r '"${theme}/config"'
r f_get_nanorc
d
}
}' "$G_FILE"
else else
sed -n -i.bkp '/'"$begin"'/,/'"$end"'/ { sed -n -i.bkp '$sed_no_lite' "$G_FILE"
/'"$begin"'/n # skip over the line that has "$begin" on it
/'"$end"'/ !{ # skip over the line that has "$end" on it
s/*//
r '"${theme}/config"'
# TODO: add a line with only "include /nanorc/*"
d
}
}' "$G_FILE"
_update_nanorc _update_nanorc
fi fi
} }

View File

@ -42,30 +42,28 @@ f_compare_version(){
# Test Functions # Test Functions
f_test_nano_version() { f_test_nano_version(){
local version local version
version="$(nano --version | cut -d ' ' -f 5 | head -n 1)" version="$(nano --version | cut -d ' ' -f 5 | head -n 1)"
echo $G_NANO_VERSION
echo $version
f_compare_version "$G_NANO_VERSION" "$version" f_compare_version "$G_NANO_VERSION" "$version"
return $? return $?
} }
f_test_shellcheck_version() { f_test_shellcheck_version(){
local version local version
version="$(shellcheck --version | cut -d ' ' -f 2 | head -n 2 | tail -n 1)" version="$(shellcheck --version | cut -d ' ' -f 2 | head -n 2 | tail -n 1)"
echo $G_SHELLCHECK_VERSION
echo $version
f_compare_version "$G_SHELLCHECK_VERSION" "$version" f_compare_version "$G_SHELLCHECK_VERSION" "$version"
return $? return $?
} }
# MAIN TESTS
printf "=================\n" printf "=================\n"
printf "TESTS\n" printf "TESTS\n"
printf "=================\n" printf "=================\n"
f_test_nano_version f_test_nano_version
printf "Nano Version ok? %s (1 = ok)\n" "$?" printf "Nano Version ok? %s (0 = ok)\n" "$?"
f_test_shellcheck_version f_test_shellcheck_version
printf "Shellcheck Version ok? %s (1 = ok)\n" "$?" printf "Shellcheck Version ok? %s (0 = ok)\n" "$?"
# ....shellcheck -f diff *.sh | git apply | git commit -a -m "Shellcheck fast corrections" # ....shellcheck -f diff *.sh | git apply | git commit -a -m "Shellcheck fast corrections"
shellcheck -- *.sh shellcheck -- *.sh