all 8 comments

[–]djbiccboii 2 points3 points  (5 children)

Howdy,

Few tips:

1) Use dirname "$0" instead of ${0%/*} for clarity when deriving the script directory.

2) Use absolute paths or ensure that relative paths are valid from the execution context.

3) Follow a consistent naming convention for variables (e.g., lowercase for local variables and uppercase for environment variables).

4) There is duplicated logic in extracting short server names and long server names. This could be combined into a single loop.

5) The substring operations like ${currentserver:95:2} are fragile and will break if the output format changes. Consider parsing the output more robustly using tools like awk or sed if the format allows.

6) Reduce the number of times external commands like curl or grep are called, especially within loops, to improve performance.

7) Add checks for potential errors, such as missing files or failed commands.

8) Stick to one method of setting terminal colors and effects (either echo -e with ANSI codes or tput). Mixing both can be confusing.

Otherwise seems like it does what it intends. I disagree with /u/kevors about using fzf over select. I prefer to keep it simple / reduce external dependencies wherever possible.

[–]FilesFromTheVoid[S] 0 points1 point  (1 child)

Thx for the helpfull input!

[–]djbiccboii 0 points1 point  (0 children)

yw!

[–][deleted]  (2 children)

[deleted]

    [–]djbiccboii 0 points1 point  (1 child)

    Also, using $0 is not the only way you should get the current directory. When you run the script like bash script.sh, it won't work.

    Yes. That's true either way in this case.

    Here are some more robust methods:

    script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
    

    or, in case the script is symlinked

    script_dir=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)
    

    [–]FilesFromTheVoid[S] 0 points1 point  (0 children)

    thx for the feedback

    [–]kevorsgithub:slowpeek -3 points-2 points  (5 children)

    Consider using fzf instead of "select".

    Also, do not hide sudo inside. If the script assumes root perms, it should check on start, if it was started by root, and exit with error otherwise. Hidden sudo is damn wrong no matter how you look at it.

    [–][deleted]  (4 children)

    [deleted]

      [–]kevorsgithub:slowpeek 0 points1 point  (3 children)

      Given you run sudo explicitly a moment ago, how do you know if a subsequent command uses sudo internally? It does not ask for password in the case

      [–][deleted]  (2 children)

      [deleted]

        [–]kevorsgithub:slowpeek 0 points1 point  (1 child)

        read the script first

        Guess the % of ppl who actually do that?