locker icon indicating copy to clipboard operation
locker copied to clipboard

[Optimize] replace tr

Open CreativeCodeCat opened this issue 3 years ago • 4 comments

FIRST

echo "$(tr ' ' '#' <<< "$dir_hash")"

can be replaced with

echo ${dir_hash// /#}

SECOND

echo "$(echo "$1" | tr '[:lower:]' '[:upper:]')"

can be replaced with

echo ${1^^}

THIRD

echo "$(echo "$1" | tr '[:upper:]' '[:lower:]')"

can be replaced with

echo ${1,,}

Could do this too

printf "[*] Passed OPERATION is: %s\n" "$(get_upper $OP)"

could just be changed to

printf "[*] Passed OPERATION is: %s\n" "${OP^^}"

And

OP=$(get_lower $1)

could be

OP=${1,,}

With these last 2 you can ditch the get_upper and get_lower functions

CreativeCodeCat avatar Jan 09 '22 16:01 CreativeCodeCat

This is basically turning this into a even more bashism shell-script, thus removing even more the capacity to run this on any other shell.

Considering the shebang is #!/usr/bin/sh I'd ask to remain the way it is right now and remove the other two bashisms on the script.

BeyondMagic avatar Jan 09 '22 21:01 BeyondMagic

From what I have tested these work totally fine with the sh shebang too

CreativeCodeCat avatar Jan 09 '22 21:01 CreativeCodeCat

That sh shebang can be a symlink to any other shell, including dash/fish/ash/sh/..., on many systems that don't even have bash installed. Those variable substitutions will decrease further compatibility.

It is to the maintainer to decide if this script is meant only to run on bash, and if so, the shebang needs to be changed to #!/usr/bin/env bash.

image

BeyondMagic avatar Jan 09 '22 22:01 BeyondMagic

@The-Repo-Club Based on what @BeyondMagic is saying, it won't be a good idea to move to not using truncate if we want to keep support for more than bash.

deepjyoti30 avatar Jan 10 '22 12:01 deepjyoti30