Visit Our UNIX and Linux User Community


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)
# 15  
Old 04-13-2020
Allthhough I havent had SWARM working for what feels like 2 weeks, I believe it's going to get alot more robust than any previous working versions.

This said, what always bothered me, was that which is not installed on systems by default, thus I had to work around it.
I used to do that by just call any expected app-name with the --help argument, figured, that it is not reliable.

So this time around, I wrote a which 'handler', if it isnt installed:
Code:
	swarm.util.which() { # COMMAND [CMD1 CMD2 ....]
	# Returns 'path/cmd' and true if COMMAND was found in PATH
	# This function is assigned to WHICH - IF no 'which' was found.
		swarm.protect "$FUNCNAME" "${@}" && exit 1
		# Get an arry
		local array=($($PRINTF "$PATH"|$SED s,':','\n',g))
		# Parse for args
		case $# in
		0)	swarm.util.usage swarm.util.which ;;
		1)	case "$1" in
			"-"|"--")
				while read cmd
				do
					$FUNCNAME "$cmd"
				done
				return 0
				;;		
			# *)	continue	;;
			esac
			;;
		*)	for arg in "${@}"
			do
				$FUNCNAME "$arg"
			done
			return 0
			;;
		esac
		# Parse for application
		for Arr in "${array[@]}"
		do
			[[ -f "$Arr/$1" ]] && \
				$PRINTF '%s\n' "$Arr/$1" && \
				return 0
		done
		return 1
	}

Further, I've written the edit function, which will open a different editor for GUI or TTY envornments.
Tough, still need to add language strings for the first time selection.

Hope I cant stay focused on cfg.set today.
Challenging part is to keep (or set) identions, force or remove quotes of the values and to force lower or upper case for the variable names.

cfg.get is working nice already Smilie
Code:
[~/prjs/SWARM] 1 $ time cfg.get ~/.bashrc PS1
[\w] \$? \\$ 

real	0m0.003s
user	0m0.003s
sys	0m0.001s
[~/prjs/SWARM] 0 $


And pick the select emulator, but for that I'd like to make a list function first.

Once that's done I'll focus on getting the init working again...
Though, I can not test most of the functions just yet, as SWARM is not working during the 'init procedure' change.

Runtime + Cross-Platform = Headache

Heading scripting now, happy easter and stay healthy

Last edited by sea; 04-14-2020 at 09:07 AM.. Reason: Commented out a wrong case statement
# 16  
Old 04-14-2020
Oh well, I could not focus on cfg.set, but I did complete pick yesterday.
And, I get SWARM loaded again, that is something.. allthough not yet properly.

Code:
[~/prjs/SWARM] 2 $ time  ./runtime 
.........SWARM 0.4-16
 
  | 2020-04-14 / 09:55:54\033[0m


\033[7mA short intro\033[0m


TODO: Visuals, then explain, configure and stuff...



one


                                                                                                            [  ✓   ]

two

                                                                                                            [  -   ]



                                                                                                            [  -   ]



                                                                                                            [  -   ]

real	0m0.129s
user	0m0.122s
sys	0m0.097s

As you 'see', there are no borders, colors are missing as well and orientation only works partialy (left / right)...
Also thanks to some 'manual debug code' (echo "blabla" >&2) I did figure that I accidently invoked 1 function 3 times (caused by the re-structuring) as that function checks/prepares some environment variables.

The current most annoying part is, well the next 'challenge', to get each output on a single line again, as currently each output uses 3 lines....
Kinda makes me think of pipes beeing the cause, since those require 3 lines....

I hope that while getting the output on a single line, the other escape codes are going to get properly interpreted again as well.

Hope dies last, but having such issues 'all the time' is energy- and motivation consuming...
# 17  
Old 04-14-2020
Some quick positive update - and a question:

The config file is written properly now (again).
For some reason (order), I had an issue to properly identify curl/wget and more/less, for the latter it used the fallback value 'cat'- while both, less and more were installed.. OMG...
Well, it all (that part) works now again as expected. Smilie


What currently confuses me, that it DOES print output (text), but neither swarm.print.border nor swarm.print.text seem to actualy get called...
By which I mean, they both SHOULD call swarm.update.geometry around which I placed set -x and set +x accordingly, yet, I dont see that part....
Code:
	swarm.print.border() { # MODE
	# Prints basic border for all lines, this should only be executed if COLUMNS > 25
	# The calling function has to handle the NEWLINE required by 'printe' (-E)
		swarm.protect "$FUNCNAME" "${@}" && exit 1
		# Makes no sense to print visuals on such short lines
		[[ ${COLUMNS} -lt 25 ]] && return 1

		local MODE="" #name=""
		case "${1/-}" in
		e|p)	MODE="basic"	;;
		h)		MODE="header"	;;
		t)		MODE="title"	;;
		esac

		# Make sure values are udpated
		set -x
		swarm.update.geometry
		set +x

		# Now prepare the seperate handling
		case "$MODE" in
		"basic")
			PRINT_LEFT_IN="$c_reset"
			PRINT_LEFT_OUT="${clrCL}${c_front}${c_back}"
			PRINT_RIGHT_IN="${c_front}${c_back}"
			pos_cor_num=0
			;;
		"header")
			local filler_num=$(( $numEND - $(( ${#BORDER_RIGHT} * 2 )) + 2 ))
			local filler_str=$($PRINTF "%*s" ${filler_num})
			PRINT_LEFT_IN="$filler_str" #$c_reset"
			PRINT_LEFT_OUT="${clrCL}${c_front}${c_back}"
			PRINT_RIGHT_IN="" #${c_front}${c_back}"
			pos_cor_num=0
			;;
		"title")
			local filler_num=$(( $(( $COLUMNS / 2 * 2)) - $(( ${#BORDER_RIGHT} * 2 )) + 2 ))
			local filler_str=$($PRINTF "%*s" ${filler_num})
			PRINT_LEFT_IN="${c_invert}$filler_str" #$c_reset"
			PRINT_LEFT_OUT="${clrCL}${c_front}${c_back}"
			PRINT_RIGHT_IN="${c_reset}${c_front}${c_back}"
			pos_cor_num=0
			;;
		esac

		# Get some numbers
		local identRight=$(( $lenRight + ${#clrCL}  ))
		local posLEFT="\33[$(( ${#BORDER_LEFT} - 2 ))G"
		local posRIGHT="\33[$(( $numEND - $lenRight - $pos_cor_num ))G"

		# Well, left aligned it works. isnt too hard
		$PRINTF "\r${PRINT_LEFT_OUT}${posLEFT}${BORDER_LEFT}${PRINT_LEFT_IN}" >&2

		# The right side is much more challenging
		$PRINTF "${posRIGHT}${PRINT_RIGHT_IN}${BORDER_RIGHT}${PRINT_RIGHT_OUT}${c_reset}" >&2
		$PRINTF "${posEND}"
	}

	swarm.update.geometry() { #
	echo " ------------- " >&2
		export COLUMNS="$($TPUT cols)"
		export LINES="$($TPUT lines)"

		# Set basic values
		export BORDER_LEFT="${SWARM_THEME_DATA[border-left]}"
		BORDER_RIGHT="${SWARM_THEME_DATA[border-right]}"	# Do not export just yet
		export clrFRONT="${SWARM_THEME_DATA[color-front]}"
		export clrBACK="${SWARM_THEME_DATA[color-back]}"

		# Check if border-right was set:
		[ -z "$BORDER_RIGHT" ] && \
				for((i=${#BORDER_LEFT}-1;i>=0;i--)); do BORDER_RIGHT="$BORDER_RIGHT${BORDER_LEFT:$i:1}"; done
		export BORDER_RIGHT

		# Get some numbers
		export lenLeft="${#BORDER_LEFT}"
		export lenRight="${#BORDER_RIGHT}"
		export lenClrFont=${#clrFRONT}
		export lenClrBack=${#clrBACK}
		export identRight=$(( $lenRight + ${#clrCL}  ))

		# Pre-Calculate positions
		export numHALF=$(( $COLUMNS / 2 ))
		export numEND=$(( $numHALF * 2 ))
		export posEND="\33[${numEND}G"
		# swarm.print.border still needs extra handling for posLEFT
		export posLEFT="\33[$(( $lenLeft + 2 ))G"

		# Set colors
		export c_front=$(swarm.color.fg ${SWARM_THEME_DATA[color-front]})
		export c_back=$(swarm.color.bg ${SWARM_THEME_DATA[color-back]})

		# If user wants extended logs, he shall have it
		! $isRO && $doLogExt && init.log "$SWARM_MSG_INIT_PID_GEOMETRY: ${PPID:-$PID}"
	}
	export -f swarm.update.geometry

Code:
[~/prjs/SWARM] 0 $ time  ./runtime  
.........SWARM 0.4-17
 
  | 2020-04-14 / 12:54:56\033[0m


\033[7mWelcome to a short introduction\033[0m


SWARM is not supposed to be called like this.



Instead it is to be sourced by your script to get access to it's functions.




\033[7m\033[0m


When calling SWARM like you just did, you should provide arguments.
They are:
./runtime config

./runtime help
./runtime tarball


one


                                                                                                            [  ✓   ]

two

                                                                                                            [  -   ]

real	0m0.079s
user	0m0.048s
sys	0m0.034s
[~/prjs/SWARM] 0 $

So, usualy, I would expect some ++ lines showing the working code, and the output of: echo " ------------- " >&2 from the update.geometry function itself...
But as you see, there is nothing.

Please, anyone has an idea as of: why?

Thank you in advance
# 18  
Old 04-14-2020
After some blind tryouts, like sourcing the SWARM/runtime and THEN use type swarm.border.print, those code-debug preparations I did, it actualy got executed...
Upon the type that is...
Like, WTF!?

Anyhow, that helped me to figure out that the swarm.update.geometry probably failed to load, due to an empty $TPUT variable.
That obviously raised the error of unkown command 'cols' by the code COLUMNS=$( $TPUT cols ).
Upon changing that to ${TPUT:-\tput}, it failed due to unkown programm \tput, while that worked as expected in the regular command prompt.

So what I did to solve this?
I've export'ed all commandname-variables (first tested with 'TPUT=tput' only) to avoid such a behaviour in the future.
Strange/Weird...

So, now I have colors and some sort of orientation back, kinda....
Image


EDIT:

Weird behaviour.... first 'part' was regular executed script.
Luckily... Been there, done that... it is a 'load order' issue, I just need to figure the NEW loadorder - allthough that should not have had changed... grml...
Maybe figuring the loader order issue might also solve my missing borders and those weird symbols issues...
Lets hope Smilie

Image

Last edited by sea; 04-14-2020 at 11:24 AM.. Reason: Didnt want to make a new post for that
# 19  
Old 04-14-2020
Yeehhaaa...
Well.. it was NOT a load order issue, but something simpler....
I still had export -f swarm.update.geometry set in the file, which seems to have been the cause of those errors.
Also, for some reason, I did had declare -AG <VAR> rather than declare -Ag <VAR>.

With those 2 issues solved, it finaly works again! Smilie Smilie

Image

Just 7 hrs... If I was employed, I would call it a day now Smilie

Last edited by sea; 04-14-2020 at 02:50 PM.. Reason: 7 not 8 hrs
# 20  
Old 04-15-2020
Yesterday late evening, I've figured that, yes have the function pick, but it wasnt complete.
So I started working on that again.

I now 'remember' why I waited so long during TUI with the select emulator/wrapper...
Allthough it is quite simple, as soon you start implementing options, things get complicated....

For example, regular grep tasks, suddenly become false positives, thus making it impossible to properly parse the users input.
https://www.unix.com/shell-programmi...-new-post.html

I'm obviously too stupid to get it working as 'runtime'....
Start from (what feels like) square 1 on a daily basis is just toooooooo much furstrating....
Specialy if you thought you had it just fixed... and it was working... for 24 hrs...

Few hours ago, I was thinking I could ask some people to test it - once I would have 'pick' working...
Now.. nothing works... again...

SCREW IT.....
To quote iron maiden.... Wasted Years....
I'm obviously too stupid to create such an application....
I mean, i'm just a hobby linux enduser... all my linux-know-how is due to TUI/SWARM... which ... obviously isnt enough to make/complete said project..
# 21  
Old 04-16-2020
Hi.

I sympathize with with your frustration. My work trail is littered with abandoned projects, and I have about 100 items on my current design / coding / testing / organizing list. Every now and then I see, hear, or otherwise come across something that allows me to push a project ahead. Time separation often seems to help me.

I hope that perhaps someday you and I and everyone else can come back to such projects and move forward.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #653
Difficulty: Easy
Rocky Raccoon is the mascot of MINIX 3.
True or False?

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/GenerateReport.sh:... (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

Featured Tech Videos