Blog-Thread: Creating a Shell Wrapper and Runtime Modifier (SWARM)

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Blog-Thread: Creating a Shell Wrapper and Runtime Modifier (SWARM)
# 1  
Old 03-11-2020
Blog-Thread: Creating a Shell Wrapper and Runtime Modifier (SWARM)

SWARM = Shell Wrapper and Runtime Modifier

But as a matter of fact, it's more like a Text User Interface framework for scripts.
Which was the original title (TUI) of the first split-up iteration from Script-Tools.

Script-Tools is still the 'end/final-project', SWARM is just the way to get there.
And for that project - Script Tools - I'd like to ask to cooperate with others, once SWARM is stable that is.

I've had it (Script-Tools) working with things like: st tweak grub 2 theme or st dev ssh con or even st set lm gdm rndbg.
As you see by the command syntax, it has several sub section, all of which are directories containing directories and files which can be called and arguments/options passed to.

This (functionality of SWARM) allows one to 'sort' one's script into directories and still be able to call them directly.
I belive this is something alot of us want.

Further, I love the console, it's very powerfull, but... also boring in it's appearance.
There's alot of unused space as everything is left oriented, SWARM offers you to output text left, center or right oriented.
And even better yet, it allows for 2 different 'liners' called 'header' and 'title' which bring some (theme'able) colors and designs to the regular terminal.

SWARM also offers 'handlers' for browsers and editors (and more), for both, CLI and GUI applications alike.
So you, as a script author/writer dont have to worry which text editor an enduser has installed.
Further it simplifies repeative tasks like yes/no question and status information wether or not a task has been successfully executed (yesno "question", status $? "text").

So, in an all short saying:
SWARM allows you to easy write application-like scripts.

A comment from a GNU guy i've sent TUI/SWARM to review, sorry lost the email and dont recall the name:
"A very new approach to the console!"

For general feedback, please refer this specific thread:
Feedback wanted for upcoming script language
But of course if we can keep it short, you're welcome to post here too, but I dont want hughe discussions in this thread.

If this sounds interesting to you so far, and if you want to know what kind of challenges I'll be facing, stay tuned Smilie
But a warning, I've completed all tasks already, what I need to do now is to restructure and rewrite/adjust the code for the new projectname SWARM (runtime only, no install required anymore).

What I want to do in/with this thread:
* Write my 'todays goal'
* Write how it went, because it's been 5 years when I last 'viewed' my code.
* This is to push myself, because redoing what you've already done is.. annoying...

--- Post updated at 19:31 ---

Yesterday I finaly completed the basic loading procedure.
Had an issue with traps, due to blindly copy-paste code without considering from where I copied (a file generating script, thus some variables were escaped, which obviously didnt work in an executed/sourced file).

Traps aside, the Theme procedure works now, at least the Theme-Array contained the required data.

And I learned something new.
If you want to fill a variable/array from a function for the main script, use declare -G varname[=value]

Also some other interesting point, the main reasons I initial had decided to go for an installation was that not all Distro's support "COLUMNS" or "LINES", thus when setting those variables per call, they wouldnt update in a runtime only apporach, but when beeing single files each call will have the updated values, so the aligment of the output is according to the terminal windows dimensions (if called in a GUI).
Now, I do have some more experiences, so it wasnt a problem to simply make it a subshell (is that the proper word?) which will reset those 2 values and export them to the mainscript.

Heck, I've even gave it an option to set the intervall.
Writing this reminds me of something, this value should be 'set'able' from the outside so a script can define how fast the values COLUMS and LINES are beeing updated.
Maybe even a check wether we're in GUI or not.. as for a console (TTY) only, those values wont change, so the intervall could be disabled alltogether...

(this is precicly why I wanted a blog, writing about something helps to find new things, and share new discoveries/learned things)

So much for yesterday.
Today I want to get swarm.print.border done (all swarm.* functions are for internal use only, bash'ism), maybe even the basic printl function.
But boy.... thats 500 lines of code (1 function) to review and adjust, wish me luck Smilie
These 4 Users Gave Thanks to sea For This Post:
# 2  
Old 03-11-2020
Getting somewhere, alot faster and much more elegant (IMO, code wise) than with TUI.

The border, 'is there' - somewhere... the basic text orientation is slowly falling into place...
[sea@manjaro-desktop SWARM]$ time . ./runtime 
TODO : swarm.sanity.env
 ----------- TEST AREA -----------
TODO case
# | SWARM 0.1                                                                                  2020.03.12 : 00:33

TODO case
# |                                                   my title                                               | # 

# |                                                                                                               | #
# | left                                               middle                                               right | #
 ----------- TEST AREA -----------

real	0m0.090s
user	0m0.125s
sys	0m0.018s

Incredible, the very first time, it took me WEEKS to get 'here'...
With TUI, it (still) took me DAYS.
Now, so far, it's been a task of just a couple of HOURS! Smilie Smilie

Though, colors are still missing and the orientations still need to be 'ironed out', but it's getting somewhere Smilie
And I still have some hours today Smilie

Oh yeah and did I mention that I love traps?
Just pressing CTRL+C in a terminal to stop all subprocess I started is just... awesomely comfortable! Smilie

[sea@manjaro-desktop SWARM]$ ^Ctodo cleanup
bash: unset: `swarm.*': Ist kein gültiger Bezeichner.

[1]   Getötet                ( while ${PS:-\ps} ${PPID:-$PID} | ${GREP:-\grep} ${PPID:-$PID} >> /dev/zero; do
    export WIDTH="$(eval $TPUT cols)"; export LINES="$(eval $TPUT lines)"; $doLogExt && init.log "Updating geometrics for SWARM PID: ${PPID:-$PID}"; sleep ${SWARM_INTERVALL_GEOMETRY:-15};
done )
[2]   Getötet                ( while ${PS:-\ps} ${PPID:-$PID} | ${GREP:-\grep} ${PPID:-$PID} >> /dev/zero; do
    export WIDTH="$(eval $TPUT cols)"; export LINES="$(eval $TPUT lines)"; $doLogExt && init.log "Updating geometrics for SWARM PID: ${PPID:-$PID}"; sleep ${SWARM_INTERVALL_GEOMETRY:-15};
done )
[3]   Getötet                ( while ${PS:-\ps} ${PPID:-$PID} | ${GREP:-\grep} ${PPID:-$PID} >> /dev/zero; do
    export WIDTH="$(eval $TPUT cols)"; export LINES="$(eval $TPUT lines)"; $doLogExt && init.log "Updating geometrics for SWARM PID: ${PPID:-$PID}"; sleep ${SWARM_INTERVALL_GEOMETRY:-15};
done )
[4]   Getötet                ( while ${PS:-\ps} ${PPID:-$PID} | ${GREP:-\grep} ${PPID:-$PID} >> /dev/zero; do
    export WIDTH="$(eval $TPUT cols)"; export LINES="$(eval $TPUT lines)"; $doLogExt && init.log "Updating geometrics for SWARM PID: ${PPID:-$PID}"; sleep ${SWARM_INTERVALL_GEOMETRY:-15};
done )
[5]-  Getötet                ( while ${PS:-\ps} ${PPID:-$PID} | ${GREP:-\grep} ${PPID:-$PID} >> /dev/zero; do
    export WIDTH="$(eval $TPUT cols)"; export LINES="$(eval $TPUT lines)"; $doLogExt && init.log "Updating geometrics for SWARM PID: ${PPID:-$PID}"; sleep ${SWARM_INTERVALL_GEOMETRY:-15};
done )
[6]+  Getötet                ( while ${PS:-\ps} ${PPID:-$PID} | ${GREP:-\grep} ${PPID:-$PID} >> /dev/zero; do
    export WIDTH="$(eval $TPUT cols)"; export LINES="$(eval $TPUT lines)"; $doLogExt && init.log "Updating geometrics for SWARM PID: ${PPID:-$PID}"; sleep ${SWARM_INTERVALL_GEOMETRY:-15};
done )

Just saying if anyone thinks of 'dirty-coding' Smilie
-> Yes' I'm aware of that unset issue, I'll take care of that later, that part is not urgent for the mood.
-> Nor is that swarm.sanity.env, as thats for when the very basic core is ready and shall be tested on different OS'

lol... do you see the mistake I just seen?
Quite an obvious one... width instead of columns... :doh:

Last edited by sea; 03-11-2020 at 08:35 PM..
# 3  
Old 03-11-2020
Told you, just a couple of hours. Smilie
Colors are due tomorrow, with a screenshot as soon it works!

[sea@manjaro-desktop SWARM]$ time . ./runtime 
TODO : swarm.sanity.env
 ----------- TEST AREA -----------
# | SWARM 0.1                                                                                  2020.03.12 : 00:57 | #
# |                                                   my title                                                    | #
# |                                                                                                               | #
# | left                                               middle                                               right | #
real	0m0.099s
user	0m0.114s
sys	0m0.035s

Yay, good time to say good night
This User Gave Thanks to sea For This Post:
# 4  
Old 03-12-2020
Colors are working now! Smilie
Gave it a shot this afternoon for a few minutes (first image), but since the sun was shining, I decided to not sit in front of the computer Smilie

So I then continued like 1 hr and 5 minutes ago....

And a a few adjustments later, voila, the result is screenshot:

Allthough I had code written already for that, I could only re-use 2 lines of code, the other 140 lines (total, incl yesterday) were/are freshly written.

I'm so happy, now that the core visuals are done (already!!!), I can focus on the cool part....
Convenience functions, like: ask, choose, edit, progress, download and all the other small things Smilie

You have NO IDEA how much I MISS my TUI!!
But I force myself to continue SWARM until I can replace all my TUI scripts.

Console/terminal feels sooo 1985 again...

Soon, very soon.. I hope Smilie

And just saying.. colors and borders are THEME'able! Smilie

But for the roadmap ahead, I'll do these tomorrow:
* ask "Question?" ; the (yn) will be supported by language file & code!
* choose $list or "${ARRAY[@]}" ; the select wrapper
* and if, which probably will be the case, I have enough time, status $? "left text" "center text"

As this would complete what I consider the absolute minimal core requirements of SWARM.

Last edited by sea; 03-12-2020 at 08:40 PM..
# 5  
Old 03-13-2020
Well, ask "Is this cool?" works now.

 ----------- TEST AREA -----------
# | SWARM 0.3                                                                                  2020.03.13 : 03:38 | #
# |                                                   my title                                                    | #
# | left                                               middle                                               right | #
# | This                                                 is                                                 piped | #
# | Is this cool? (yn)                                                                                          y | #
 ----------- TEST AREA -----------

real	0m1.585s
user	0m0.122s
sys	0m0.044s
[sea@manjaro-desktop prjs]$ time . SWARM/runtime 
TODO : swarm.sanity.env
 ----------- TEST AREA -----------
# | SWARM 0.3                                                                                  2020.03.13 : 03:39 | #
# |                                                   my title                                                    | #
# | left                                               middle                                               right | #
# | This                                                 is                                                 piped | #
# | Is this cool? (yn)                                                                                            | #
 ----------- TEST AREA -----------

real	0m0.131s
user	0m0.141s
sys	0m0.043s

EDIT: /* Seems I was a bit too fast on pressing 'n' that it was printed during the loading screen, thus not at it's 'regular' position.. but just check the return number Smilie */

It even has a 'fallback' mode, using '+' and '-', just in case anything with possible translations should not work.

And the best part, you dont have to press enter - at least not as long your 'read' does support this feature Smilie
Well, the 2nd part is what I'm going to adapt now.

But first I'll write a quick 'help handler' and probably prepare a dyanamic 'man(page) handler' as well.

But if you're interest in the BASIC version, it's first iteration (without the actual handling of the different read abilities , here's the code:
	ask() { # "What question?"
	# Ask a yes/no question, returns 0/true for yes and 1/false for no
	# y/n are taken from language file and are used in code
		# Check if 'yesno' is empty, if so, fill with fallback values
		[[ -z "${SWARM_MSG_ASK_YES}${SWARM_MSG_ASK_NO}" ]] && \
			SWARM_MSG_ASK_YES="+" && \

		swarm.print.border -e
		# Print the text
		$PRINTF "$(swarm.print.goto $(( ${#SWARM_THEME_DATA[border-left]} + 2  )))$1 (${SWARM_MSG_ASK_YES}${SWARM_MSG_ASK_NO})"
		# Move the cursor
		$PRINTF "$(swarm.print.goto $(( $COLUMNS / 2 * 2 - ${#SWARM_THEME_DATA[border-left]} - 2  )))"
		read -n1 answer
		$PRINTF "$posEND\n"

		case "$answer" in
				return 0
				return 1
		*)		return 1

Lets share the code as long it's simple Smilie

Last edited by sea; 03-13-2020 at 12:15 AM..
# 6  
Old 03-15-2020
Currently listening: End of Line by Daft Punk, Tron Legacy OST

Oh what whall I say...
That "-> <-" was really costing my nerves!
Thanks here again to Made In Germany (MIG) for his 'echo wrappers' (and the additional help)!

Luckily at least ask seemed to work 'out of the box' for the pipe tweaks today. Smilie

Allthough the average load & output time was around 0.150 s (0.097-0.210), it recently became 'jumps' up to 0.300 or higher.
As a natural tweaker, I wanted to fix this, one breaks things along the way - which helped to make the load sequence more stable.

As I had prepared a subshell process if no $COLUMNS and no $LINES should be available,
to have them calculated and exported, but since my sytem has them at hand, well, I just assumed the code would work,
since there were subprocess and I had seen with set -x that the code indeed was executed in the shell.

Regardless, the exported variables were not to be found in the shell I sourced the file with the the sub process in from.
Something like this:
[sea/ .bin]$ cat testfile 
	declare -g my_test_a="a test"
	export my_test_b="an exported beer"
[sea/ .bin]$ . testfile 
[sea/ .bin]$ echo $my_test_a

[sea/ .bin]$ echo $my_test_b

Well, something like this, but with a looped sleep while grep checked the ps output for the $PPID as actual sub process.

That was quite, disencouraging.
This would have been so cool, real multi-tasking in console!
Guess I'm too much of a dreamer Smilie

Next will be alot of small utility functions, and to finalize the current core commands with 'help' functionality - before adding new core (usualy slitly bigger/ more complex command functions..
This User Gave Thanks to sea For This Post:
# 7  
Old 03-16-2020

Perhaps I missed something, but variables are created/set when from a function:
#!/usr/bin/env bash

# @(#) s1       Demonstrate use of option "-g" in "declare".

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

        declare -g my_test_a="a test"
        export my_test_b="an exported beer"

unset my_test_a my_test_b
pl " Results, not in function:"
echo $my_test_a
echo $my_test_b

f1 ()
    declare -g my_test_a="a test"
    export my_test_b="an exported beer"

unset my_test_a my_test_b
pl " Results, in function f1:"
echo $my_test_a
echo $my_test_b


$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30

 Results, not in function:

 Results, in function f1:
a test
an exported beer

Seen in man bash:
-g create global variables when used in a shell function; otherwise
and noted with shellcheck:
In s1 line 21:
echo $my_test_a
     ^-- SC2031: my_test_a was modified in a subshell. That change might be lost. 

Apologies if I missed it ... cheers, drl

( currently listening: Bartok: String Quartets, but soon Yes )
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script and Progress Bar or GUI Wrapper

Hi, I have shell script that I am running under Ubuntu as root. Is it possible to hide the command window and show the user some sort of progress /random progress bar / or other form of GUI interaction? On MAC, I have been using Platypus but on Ubuntu I am not sure what to do. (4 Replies)
Discussion started by: naveedanwar4u
4 Replies

2. Homework & Coursework Questions

Shell Script average runtime

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Make a bash script that calculates average runtime for the first two scripts you made. The average should be... (17 Replies)
Discussion started by: navlelo
17 Replies

3. Shell Programming and Scripting

Wrapper Script in Perl Or shell

Hello, My requirement is based on Oracle where we run a perl script and it asked some questions.I want to write a wrapper which will answer all these questions. How is it possible. Thanks (16 Replies)
Discussion started by: cotton
16 Replies

4. Shell Programming and Scripting

Shell Runtime Statistics

Hi, I am trying to capture runtime stats of a shell script (c shell). Are there system variables to call? Or should I create a date variable at the start of the script and at the end of the script? I am trying to capture the time if the script stops or ends with error. Please help. ... (4 Replies)
Discussion started by: CKT_newbie88
4 Replies

5. Programming

creating multiple threads using single thread id

Hi all, Can I create multiple threads using single thread_id like pthread_t thread_id; pthread_create(&thread_id, NULL, &print_xs, NULL); pthread_create(&thread_id, NULL, &print_ys, NULL); pthread_create(&thread_id, NULL, &print_zs, NULL); pthread_join(thread_id, NULL); what... (2 Replies)
Discussion started by: zing_foru
2 Replies

6. Web Development

Creating a blog site on a local computer

Hello! I would like to create a blog website on a web domain of mine. The blog will be used for publishing economics-lated articles. I tried to use a few open source packages for blog creation (WorldPress, b2evolution, Movable type) which I wanted to test on a local computer before arranging... (5 Replies)
Discussion started by: degoor
5 Replies

7. Shell Programming and Scripting

Korn Shell Wrapper script

Hi Guys, I am trying write a wrapper script but I don't have any idea. I have 4 different korn shell scripts and all of them needs some parameters from command line (positional parameter). My script cant be interactive because its supposed to be automated. I am confused how can I write a wrapper... (6 Replies)
Discussion started by: pareshan
6 Replies

8. Shell Programming and Scripting

crontab is not creating runtime files which are in script..

this is the output i am getting here.. cp: cannot create /wls_domains/eoigw/eoigwsA/deliv/cron/MailingScript/eoigwsA_Health_Status_Report.html: Permission denied /wls_domains/eoigw/eoigwsA/deliv/cron/MailingScript/ /wls_domains/eoigw/eoigwsA/deliv/cron/MailingScript/ (6 Replies)
Discussion started by: surekha268
6 Replies

9. Shell Programming and Scripting

korn shell version at runtime?

How can I check what kornshell version I am using at runtime from within a kornshell script? (3 Replies)
Discussion started by: qanda
3 Replies
Login or Register to Ask a Question