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

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

<< < (2/10) > >>

jazzbiker:
Hi Bruno,

Thanks for the masterclass! As far a s I will be able I'll learn it. Some questions arose, caused with my bad knowledge of shell:

1)

--- Code: ---"${logs[@]}"

--- End code ---
- what's this? CardealRusso also used this construct in his example.
2) logs are declared as local

--- Code: ---run_redo()
{
  local logs=( "$name.%d.log" )
...

--- End code ---
but are used in main():

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

--- End code ---
What does "local" means in shell?

Thank You for the time spent and efforts!

GNUser:
Hi, Andrey. It seems CardealRusso is a bash guy. Bash is not my cup of tea, but "different strokes for different folks" :)

1) "${foo}" is the safer (better handling of spaces) equivalent of ${var}
$var is shorthand for ${var}
So far, so good.

--- Code: ---"${foo[@]}"
--- End code ---
is a bashism that means "all elements of the array foo". POSIX shells (e.g., dash and busybox ash) do not support arrays.

2) local is not POSIX-defined
In shell, variables are global by default. If you want a variable to be local to a function, some shells allow the local varname (or the even less portable local varname=value) construct.

To avoid breakage and maximize portability, I'd avoid using bashisms and POSIX-undefined keywords in shell scripts.

Refs:
https://tldp.org/LDP/abs/html/arrays.html
https://mywiki.wooledge.org/Bashism (see "Arrays" and "Builtins")

----------

P.S. When I was learning shell, I found that these two excellent, succinct pages gave me the most bang for my buck:
https://mywiki.wooledge.org/BashPitfalls
https://mywiki.wooledge.org/Bashism

If you are extremely interested in shell, there is this exhaustive resource (caveats: time consuming, bash-specific):
https://tldp.org/LDP/abs/html/

jazzbiker:

--- Quote from: CardealRusso on July 18, 2023, 07:53:19 PM ---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.

--- End quote ---
Have You done this for yourself and have practical results?

I expect shells are heavy legacy load from ancient times. I use for my personal purposes Lua as scripting language. I've started it as a kind of exercise. And was astonished that common tasks written with the interpreted language are accomplished faster than the same tasks done by specialized utilities written in compiled language - C. I have no explanaitions, simply got this as the fact.

In my opinion if You are interested in some replacement for shell, it must not be compiled language, it should be dynamically typed interpreted with garbage collector. Otherwise - C can do everything.

GNUser:

--- Quote from: jazzbiker on July 19, 2023, 10:21:46 AM ---common tasks written with the [Lua] interpreted language are accomplished faster than the same tasks done by specialized utilities written in compiled language - C.

--- End quote ---
I'm astonished by this. I looked at Lua a few years ago when you clued me in. Although shell (system administration) and awk (text munching) can handle all my household's programming needs, I'll take a look at Lua again.

CardealRusso:

--- Quote from: jazzbiker on July 19, 2023, 10:21:46 AM ---...it must not be compiled language...

--- End quote ---

I totally agree with you. To be honest, I hadn't considered Lua.
Usually linux distributions use python, but it seems that it would be inappropriate to integrate the python runtime into tinycore (since python is the same size as tinycore).
I considered Nim for its syntax. I strongly believe that concise and readable code greatly facilitates community development. Currently, CoreScripts bash scripts are neither easy to read nor user friendly (though understandable).

  but yes, you are correct about lua, it is a very good and light option.
And you are also correct in requesting examples. I'll try to formulate an example of what it would look like, for example (it could be another one) tce-update in nim or lua (although I don't have a good command of lua)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version