Off-Topic > Off-Topic - Tiny Tux's Corner

[Solved] Asking for recomendations on the shell script formatting.

(1/10) > >>

jazzbiker:
Greetings!

I have some script. It do everything it must do. But as for me it looks like a heap of junk. What can be done to make it look a little bit better?


--- Code: ---TNAME=${1##*/}
BNAME=${2##*/}
TDIR=${1%$TNAME}

depends-on ${TDIR}.do..${TNAME}

JOBS=${JOBS:-2}

LOGS=$(lua -e "for i = 1, $JOBS do print(('$TNAME.%d.log'):format(i)) end")

RDIR=$(pwd)

test -n "$TDIR" && cd $TDIR

export MAP_DIR=$(pwd)

REDO_DIRPREFIX=

for LOG in $LOGS
do
        redo -l $LOG -m $TNAME $BNAME &
done

wait

{ echo 'return {'; cat $LOGS; echo '}'; } > $TNAME.log

cd $RDIR

lua log2map.lua $1.log > $3


--- End code ---

Thanks in advance!

CardealRusso:
There's not too much you can do about it, bash are ugly by nature. Beauty is not the purpose but to just work.
I even ask a member of the TinyCore team here if it wouldn't be interesting to migrate some corescripts to Nim. Nim is a high-level programming language that goes very well with TinyCore; Result will be native binaries, fast, small (some binaries may be smaller than some bash scripts) and easily readable source code.


--- Code: ---function run_redo(name, base) {
  local logs=( "$name.%d.log" )
  for log in "${logs[@]}"; do
    redo -l "$log" -m "$name" "$base" &
  done
  wait
}

function main() {
  local name=$1
  local base=$2
  local output=$3

  run_redo "$name" "$base"

  echo "return {$(cat "${logs[@]}")}" > "$output"
}

main "$@"
--- End code ---
/\ this is a bash script btw

jazzbiker:
Hi CardealRusso,

Thanks for reply! Yes, bash looks like overshooting for such chunk. I even try to make such pieces dash-able. So enclosing some code into the function may improve the outlook, agreed. But Your version misses some ugly details, which will damage the nice picture when added (

Nim is compiled into C, so I don't understand what may be its advantages.

Thanks again and best regards!

jazzbiker:
After removing some boilerplate with traveling through directories my ugly script looks like

--- Code: ---TNAME=${1##*/}
TDIR=${1%$TNAME}

depends-on ${TDIR}.do..${TNAME}

JOBS=${JOBS:-2}

LOGS=$(lua -e "for i = 1, $JOBS do print(('$1.%d.log'):format(i)) end")

export MAP_DIR=$(test -n "$TDIR" && cd $TDIR; pwd)

for LOG in $LOGS
do
        redo -l $LOG -m $1 $2 &
done

wait

{ echo 'return {'; cat $LOGS; echo '}'; } > $1.log

lua log2map.lua $1.log > $3

--- End code ---
which seems to be slightly less ugly.

GNUser:
Hi, Andrey. I have hundreds of shell scripts. If they look like junk, then I won't understand them or know how to use them if I haven't used them in a while.

Class A recommendations:
- Use a main function
- Group related steps into functions
- Use function names that include a verb and clearly describe what the function does, so that comments become superfluous
- Put script name, version, purpose, and usage example at top of script
- Maximize simplicity and portability by avoiding bashisms unless you absolutely need bash
- Keep user variables and internal script variables (i.e., ones user should not mess with) separate
- Use clear variable names that include a noun
- After your script is done, check it with checkbashisms and shellcheck

Class B recommendations:
- Put main function at the top
- Use Allman braces style because it is trivial to use correctly (only rules are "each brace on its own line", "brace pairs equally indented")

I'm not sure what your script does, so I can't give the script or its functions good names. Here is a first attempt at cleaning it up. Note that this is rough pseudocode that has several obvious errors and doesn't actually work (plus, its stated purpose and usage example are completely made up)--it's just to illustrate the principles:


--- Code: ---#!/bin/sh

# dependency-checker v1.0
# Purpose: Find all of an extension's dependencies
# Usage example: $ dependency-checker brave-browser

# user variables:
log=/home/andrey/logs/dependency-checker.logs

main()
{
  run_redo "$name" "$base"
  echo "return {$(cat "${logs[@]}")}" > "$output"
}

run_redo()
{
  local logs=( "$name.%d.log" )
  for log in "${logs[@]}"; do
    redo -l "$log" -m "$name" "$base" &
  done
  wait
}

# internal variables:
TNAME=${1##*/}
BNAME=${2##*/}
TDIR=${1%$TNAME}

main "$@"
--- End code ---


I try my hardest to make my shell scripts beautiful. The Pragmatic Programmer is the book that I have found to be the most helpful in this regard.

Navigation

[0] Message Index

[#] Next page

Go to full version