-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I cannot beautify my code #11
Comments
@kamontat Can you provide me the sample file? |
here link |
The code is no longer available. @kamontat can you post it here please? Otherwise there's nothing to do here. |
So you mean, you also don't know what this error cause? |
here the example code #!/bin/bash
# -------------------------------------------------
# Description: resource management of the download command
# List Command: check_is_resource_exist - check resource file exist
# get_resource - get the value in constant key
# update_resource - update value of resource constants in resource file
# save_resource - create new resource file or update if exist
# Usage: run file in `source` command only
# Create by: Kamontat Chantrachirathumrong
# Since: 09/09/2560 (DD/MM/YYYY)
# -------------------------------------------------
# Version: 1.0
# -------------------------------------------------
# Bug:
# -------------------------------------------------
[ -d "$NOVEL_LOCATION" ] || return 8
[ -n "$NOVEL_RESOURCE_NAME" ] || return 8
# -------------------------------------------------
# Constants
# -------------------------------------------------
# @require - $NOVEL_LOCATION variable
# - $NOVEL_RESOURCE_NAME variable
function get_resource_file {
echo "$NOVEL_LOCATION"/$NOVEL_RESOURCE_NAME
}
DELIMITER="†" # alt+t
# RESOURCE_CONSTANTS
LINK_CONSTANT="Link" # save as string
DATE_CONSTANT="Date" # save as string and can convert to date
VERSION_CONSTANT="Version" # save as string
STORY_NAME_CONSTANT="StoryName" # save as string
FIRST_CHAPTER_CONSTANT="FirstChapter" # save as int
LAST_CHAPTER_CONSTANT="LastChapter" # save as int
IGNORE_CONSTANT="Ignore" # save as boolean
DATE_FORMAT="%d%m%y%H%M%S"
# -------------------------------------------------
# Export constants
# -------------------------------------------------
declare -x DATE="$(date +$DATE_FORMAT)"
# -------------------------------------------------
# Functions
# -------------------------------------------------
# @return - true if resource file exist, otherwise return false
function is_resource_exist {
is_file_exist "$(get_resource_file)"
}
# @params - 1 - RESOURCE_CONSTANTS only
# @return - value of key in resource file
function get_resource {
local temp=$(grep -w "^$1" "$(get_resource_file)")
IFS="$DELIMITER" read -r -a ARR <<< "$temp"
local result=${ARR[1]}
if [[ $1 = "$DATE_CONSTANT" ]]; then
echo "$(date -jf "$DATE_FORMAT" "$result")"
else
echo "$result"
fi
}
# @require - $DELIMITER variable
# @params - 1 - RESOURCE_CONSTANTS only
# 2 - new value of input constant
function update_resource {
local temp=$(grep -nw "^$1" "$(get_resource_file)")
local line_number=${temp%%:*}
if [[ $line_number != "" ]]; then
sed -i -e "${line_number}s/$1.*/$1$DELIMITER$2/" "$(get_resource_file)"
rm -f "$(get_resource_file)-e"
else
echo "$1$DELIMITER$2" >> "$(get_resource_file)"
fi
}
# @require - check_is_resource_exist function
# - get_resource function
# - update_resource function
# - $DELIMITER variable
# - $COMMAND_VERSION variable
# - $NOVEL_LINK variable
# - $NOVEL_NAME variable
# - $NOVEL_START variable
# - $NOVEL_END variable
# - $NOVEL_IS_IGNORE variable
# - $DATE variable
function save_resource {
if $(is_resource_exist); then
[[ "$(get_resource $STORY_NAME_CONSTANT)" = "" ]] && update_resource $STORY_NAME_CONSTANT $STORY_NAME # in case version is too old
[[ "$(get_resource $VERSION_CONSTANT)" != "$COMMAND_VERSION" ]] && update_resource $VERSION_CONSTANT $COMMAND_VERSION # update version (only if not equal)
[[ $NOVEL_START -lt $(get_resource $FIRST_CHAPTER_CONSTANT) ]] && update_resource $FIRST_CHAPTER_CONSTANT $NOVEL_START # update first chapter (only if current less than previous)
[[ $NOVEL_END -gt $(get_resource $LAST_CHAPTER_CONSTANT) ]] && update_resource $LAST_CHAPTER_CONSTANT $NOVEL_END # update last chapter (only if current more than previous)
update_resource $IGNORE_CONSTANT $NOVEL_IS_IGNORE # update to ignore this (only if ignore is 'true')
update_resource $DATE_CONSTANT $DATE # update date to current
else
echo "$VERSION_CONSTANT$DELIMITER$COMMAND_VERSION" >> "$(get_resource_file)"
echo "$LINK_CONSTANT$DELIMITER$NOVEL_LINK" >> "$(get_resource_file)"
echo "$STORY_NAME_CONSTANT$DELIMITER$NOVEL_NAME" >> "$(get_resource_file)"
echo "$FIRST_CHAPTER_CONSTANT$DELIMITER$NOVEL_START" >> "$(get_resource_file)"
echo "$LAST_CHAPTER_CONSTANT$DELIMITER$NOVEL_END" >> "$(get_resource_file)"
echo "$DATE_CONSTANT$DELIMITER$DATE" >> "$(get_resource_file)"
echo "$IGNORE_CONSTANT$DELIMITER$NOVEL_IS_IGNORE" >> "$(get_resource_file)"
fi
} |
@kamontat I have merged @jesselang 's PR which should have a fix for this. I already have the new beautysh version on PyPI, so try updating the package and testing it again. |
Appears to be choking here:
Unclear to me what's causing the error. |
@jesselang oh but all syntax is correct right? 🤔 |
@jesselang Yeah, I've been poking at his code now for a bit and I cannot understand what's causing the error. @kamontat Check your script with shellcheck. |
Here is final code that I fixes all code error warning and suggestion. #!/bin/bash
# -------------------------------------------------
# Description: resource management of the download command
# List Command: check_is_resource_exist - check resource file exist
# get_resource - get the value in constant key
# update_resource - update value of resource constants in resource file
# save_resource - create new resource file or update if exist
# Usage: run file in `source` command only
# Create by: Kamontat Chantrachirathumrong
# Since: 09/09/2560 (DD/MM/YYYY)
# -------------------------------------------------
# Version: 1.0
# -------------------------------------------------
# Bug:
# -------------------------------------------------
[ -d "$NOVEL_LOCATION" ] || return 8
[ -n "$NOVEL_RESOURCE_NAME" ] || return 8
# -------------------------------------------------
# Constants
# -------------------------------------------------
# @require - $NOVEL_LOCATION variable
# - $NOVEL_RESOURCE_NAME variable
function get_resource_file {
echo "$NOVEL_LOCATION"/"$NOVEL_RESOURCE_NAME"
}
DELIMITER="†" # alt+t
# RESOURCE_CONSTANTS
LINK_CONSTANT="Link" # save as string
DATE_CONSTANT="Date" # save as string and can convert to date
VERSION_CONSTANT="Version" # save as string
STORY_NAME_CONSTANT="StoryName" # save as string
FIRST_CHAPTER_CONSTANT="FirstChapter" # save as int
LAST_CHAPTER_CONSTANT="LastChapter" # save as int
IGNORE_CONSTANT="Ignore" # save as boolean
DATE_FORMAT="%d%m%y%H%M%S"
# -------------------------------------------------
# Export constants
# -------------------------------------------------
export DATE
DATE="$(date +$DATE_FORMAT)"
# -------------------------------------------------
# Functions
# -------------------------------------------------
# @return - true if resource file exist, otherwise return false
function is_resource_exist {
is_file_exist "$(get_resource_file)"
}
# @params - 1 - RESOURCE_CONSTANTS only
# @return - value of key in resource file
function get_resource {
local temp, result
temp=$(grep -w "^$1" "$(get_resource_file)")
IFS="$DELIMITER" read -r -a ARR <<< "$temp"
result=${ARR[1]}
if [[ $1 = "$DATE_CONSTANT" ]]; then
date -jf "$DATE_FORMAT" "$result"
else
echo "$result"
fi
}
# @require - $DELIMITER variable
# @params - 1 - RESOURCE_CONSTANTS only
# 2 - new value of input constant
function update_resource {
local temp, line_number
temp=$(grep -nw "^$1" "$(get_resource_file)")
line_number=${temp%%:*}
if [[ $line_number != "" ]]; then
sed -i -e "${line_number}s/$1.*/$1$DELIMITER$2/" "$(get_resource_file)"
rm -f "$(get_resource_file)-e"
else
echo "$1$DELIMITER$2" >> "$(get_resource_file)"
fi
}
# @require - check_is_resource_exist function
# - get_resource function
# - update_resource function
# - $DELIMITER variable
# - $COMMAND_VERSION variable
# - $NOVEL_LINK variable
# - $NOVEL_NAME variable
# - $NOVEL_START variable
# - $NOVEL_END variable
# - $NOVEL_IS_IGNORE variable
# - $DATE variable
function save_resource {
if is_resource_exist; then
[[ "$(get_resource $STORY_NAME_CONSTANT)" = "" ]] && update_resource $STORY_NAME_CONSTANT "$STORY_NAME" # in case version is too old
[[ "$(get_resource $VERSION_CONSTANT)" != "$COMMAND_VERSION" ]] && update_resource $VERSION_CONSTANT "$COMMAND_VERSION" # update version (only if not equal)
[[ $NOVEL_START -lt $(get_resource $FIRST_CHAPTER_CONSTANT) ]] && update_resource $FIRST_CHAPTER_CONSTANT "$NOVEL_START" # update first chapter (only if current less than previous)
[[ $NOVEL_END -gt $(get_resource $LAST_CHAPTER_CONSTANT) ]] && update_resource $LAST_CHAPTER_CONSTANT "$NOVEL_END" # update last chapter (only if current more than previous)
update_resource $IGNORE_CONSTANT "$NOVEL_IS_IGNORE" # update to ignore this (only if ignore is 'true')
update_resource $DATE_CONSTANT "$DATE" # update date to current
else
{
echo "$VERSION_CONSTANT$DELIMITER$COMMAND_VERSION"
echo "$LINK_CONSTANT$DELIMITER$NOVEL_LINK"
echo "$STORY_NAME_CONSTANT$DELIMITER$NOVEL_NAME"
echo "$FIRST_CHAPTER_CONSTANT$DELIMITER$NOVEL_START"
echo "$LAST_CHAPTER_CONSTANT$DELIMITER$NOVEL_END"
echo "$DATE_CONSTANT$DELIMITER$DATE"
echo "$IGNORE_CONSTANT$DELIMITER$NOVEL_IS_IGNORE"
} >> "$(get_resource_file)"
fi
} |
in my code it fails to process lines with |
This should now be fixed. @zone01-org Can you confirm the latest version solves this? |
The current master branch exhibits the reported behavior on the code submitted by kamontat on October 18. Commit 9e98979 doesn't fix it. Here's a smaller example that exhibits the same behavior: #!/bin/bash
function get_resource {
IFS="$DELIMITER" read -r -a ARR <<< "$temp"
} The problem is indeed the |
I encounter this issue as well, which is also reproduced with bashbeautify, which seems to have similar codebase: bergwerf/bashbeautify#12 Here's the stacktrace:
|
Same here:
|
Hi, I still have some problems with here string formatting in beautybash: function install() {
echo "Certificate is ready, downloading..."
CERTIFICATES=$(curl api.zerossl.com/certificates/"$ID"/download/return?access_key="$ACCESS_KEY")
echo "Installing new certificates"
jq -r '.["certificate.crt"]' <<< "$CERTIFICATES" > "$APACHE_CONF_DIR/ssl/test.cert"
jq -r '.["ca_bundle.crt"]' <<< "$CERTIFICATES" > "$APACHE_CONF_DIR/ssl/test_boundle.cert"
} this would give an error: Couldn't format the document:File (stdin): error: indent/outdent mismatch: 1. if I change the lines with the here string to be in double quotes |
I am still getting this error. Here is my code, which runs fine #!/usr/bin/env bash
# Program which will capitalize the first letter of all sentences in all files in the directory txt.
for file in txt/*; do
replaced=$(< "$file" tr '\n' '\r' | sed -Ee "s/(^|[.?!]['’\"”)]?\s+)(['‘\"“(]?)([a-z])/\1\2\U\3/g" | tr '\r' '\n')
echo "$replaced" > "$file"
done |
I get the same error, and my script works just fine. |
It throw this error out, but i'm not sure what it mean??
The text was updated successfully, but these errors were encountered: