Skip to content

Issue with bc -l Calculation Due to Invalid Characters in Time Variables #28

@Secur1ty0

Description

@Secur1ty0

Description:

I encountered an issue when trying to calculate the time difference using the bc -l command. The error occurs due to invalid characters, particularly N, in the time_end and COMMAND_TIME_BEIGIN variables. This results in the following error message:

[[10:25:14] [~] ❱❱❱ ls
PycharmProjects
(standard_in) 1: illegal character: N                                           
(standard_in) 1: illegal character: N
[10:25:15] [cost s] 251  ls

Run the script containing the calculation logic, which uses bc -l to compute the difference between time_end and COMMAND_TIME_BEIGIN.
Ensure that time_end or COMMAND_TIME_BEIGIN contains non-numeric characters (e.g., N).
Observe the error in the output, specifically related to the invalid character in bc.
Expected Behavior: The calculation should execute without errors, even if time_end or COMMAND_TIME_BEIGIN contains extraneous characters.

Proposed Fix: To resolve this issue, we should clean the time_end and COMMAND_TIME_BEIGIN variables by removing any non-numeric characters (except the decimal point). This can be done using tr to filter the variables before performing the calculation with bc.

Fix Implementation:

fix file: ~/.oh-my-zsh/themes/passion.zsh-theme

# Clean invalid characters from time_end and COMMAND_TIME_BEIGIN
local cleaned_time_end=$(echo "$time_end" | tr -d -c '[:digit:].')
local cleaned_command_time_begin=$(echo "$COMMAND_TIME_BEIGIN" | tr -d -c '[:digit:].')
# Perform the calculation with cleaned values
local cost=$(bc -l <<<"${cleaned_time_end}-${cleaned_command_time_begin}")```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions