Sponsored Content
Top Forums Shell Programming and Scripting [BASH] Performance question - Script to STDOUT Post 302907540 by sea on Sunday 29th of June 2014 09:16:04 PM
Old 06-29-2014
RedHat [BASH] Performance question - Script to STDOUT

Hello Coders

Some time ago i was asking about python and bash performances, and i was told i could post the regarding code, and someone would kindly help to make it faster (if possible).

If you have noted, i'm on the way to finalize, finish, stable TUI - Text(ual) User Interface.
It is a framework, aiming to be of help when trying to achieve an Interface on Text level, so it is ment to be used by Scripts, to make it easier for the actual end-user.

This said, the complete package -> all required files are located at: https://github.com/sri-arjuna/tui
Note that it is most optimized for redhad based systems, if you have issues on your system, please let me know about it - and its solution, so i can fix this for others/later.

However, i already have had rewrote it in the aim for performance gain, i'll only include THAT file, representing the core-key-functions for all the presentations done within that package.

The (my) 'logic' behind this is like: printf
Print the information as required, 1 to 3 strings, left, center or right aligned, depending upon arguments, it'll get colored, and depening upon string lengths, even become multilined (not wanted, but required).

Functions are cool, but files/applications are cooler, but took me quite a while to actualy get there -> 2 years...
And i' do like to play with self-supporting-solving-scripts.

So like in most temals, printf is available, and prints on the same line until the line is full.
For my package TUI, tui-printf is the core component for all displaying tasks.

Therefor, it must not only handle several calls, while staying on the very same console line (where possible -> string lenghts), it also must provide color and newlines upon other calls, furthhermore, if the string lenghts provided are too long to be displayed on the current terminal line (whether gui-window or virtual-terminal) it must 'split' them first by arguments-locations (left,cetner,right) then by cutting the actual lenght of a single/certain string, using itself to solve this task.

As a hobby-auto-didactic-learning-kind-of-lazy-guy, i've reached the limits of my brain at this stage for this task (performance).

After this much talking, i'm still not posting the script, as you need to know HOW the script is retrieving its data....
By ENV - so most of the variables required by this TUI-package, are loaded by /etc/profile.d/tui.sh, which not only sources the systemwide configuration in /etc/tui/files, but also in the custom files within $HOME/.config/tui/*.
That way, the ENV is 'filled' with variables(19) like (but not limited to): $DONE (string: [ DONE ] in text, OR [ ✔ ] in GUI.

I'm aware that with the use of tui-status, i could also make another script, returning such similar strings, in which case, in turn, some RET_XY variables would need to be shared by env, which is penetrant within my mind, beeing aware of that, i'm open for suggestions, other than to rewrite it completly to (#)C(++), Python, or whatever.

Finaly, the script, with the most important question:
Do you see anything to increase performance?
File: tui-printf -- Actualy - please see https://github.com/sri-arjuna/tui/bl...bin/tui-printf for a readable structure....
Code:
#!/bin/bash
#
#	Author: 	Simon Arjuna Erat (sea)
#	Contact:	erat.simon@gmail.com
#	License:	GNU Lesser General Public License (LGPL)
#	Created:	2013.05.03
#	Changed:	2014.06.10
	script_version=0.9
#	Description:	Descriptional text goes here
#
#	Variables
#
	#source tui
	[ -z "$BORDER_LEFT$HEAD_BG" ] && source /etc/profile.d/tui.sh
	TITLE="tui-printf"
	ME="${0##*/}"
	ME_DIR="${0/${0##/*/}}"
	help_text="
$ME ($script_version)
$TITLE
Usage: 	$ME [options] [arguments]
	$ME		Prints up to 3 arguments. 
			1: Left
			2: Left, Right
			3: Left, Center, Right
Arguments will tell $ME if and where to colorize, or if to do a linebreak.
Options are:
	-h(elp) 	This screen
	-E(cho)		Simulates 'echo', prints up to 3 strings and a newline.
	-T(itle)	Prints 1st argument centred, if more - aligment like tui-echo
			The inner background is colored white with blue font by default
	-H(header)	Full line has a blue background and white font as default color
"
	FIRST=""
	SECOND=""
	THIRD=""
	WIDTH=$( [ -z $COLUMNS ] && tput cols || printf $COLUMNS )	# Retrieve env or calc it new
	TOTAL="$WIDTH"
	WIDTH=$[ $WIDTH - ${#BORDER_LEFT} - ${#BORDER_RIGHT} - 2 ]	# Subtract default 'borders'
	WIDTH=$[ $WIDTH / 2 * 2 ]					# Make it even 
	EMPTY="$(printf '%*s' $WIDTH)"					# Get 'width' spaces
	doEcho=false
	doHeader=false
	doTitle=false
	optStyle=""
#
#	Catching Arguments
#
	#[[ -z $1 ]] && printf "$help_text" && exit $RET_HELP
	while getopts "EHTh?" name
	do 	case $name in
		E|echo) 	doEcho=true
				optStyle="-E"	;;
		H|header)	doHeader=true
				optStyle="-H"	;;
		T|title)	doTitle=true
				optStyle="-T"	;;
		h|"?"|*)	printf "$help_text"
				exit $RET_HELP
				;;
		esac
	done
	shift $(($OPTIND - 1))
	ARGS=(${*})			# Remaining arguments	| These two fail on spaced-strings
	ARGS_COUNT_org=${#ARGS[@]}	# Amount of remaining
	#FIRST="${ARGS[0]}"
	#SECOND="${ARGS[1]}"
	#THIRD="${ARGS[2]}"
	ARGS_COUNT=0
	[ ! -z "$1" ] && FIRST="$1" && ((ARGS_COUNT++))
	[ ! -z "$2" ] && SECOND="$2" && ((ARGS_COUNT++))
	[ ! -z "$3" ] && THIRD="$3" && ((ARGS_COUNT++))
	[ ! -z "$4" ] && doMore=true || doMore=false
#
#	Verify all fit on 1 line
#
	len=$[ ${#FIRST} + ${#SECOND} + ${#THIRD} ]
	if [[ $len -gt $WIDTH ]]
	then 	len2=$[ ${#FIRST} + ${#SECOND} ]
		#echo $len $len2 $WIDTH
		if [[ $len2 -le $WIDTH ]]
		then 	# first & second match on one line
			tui-printf -E "$FIRST" "$SECOND"
			tui-printf $optStyle "$THIRD"
		else	# it needs further spliting:
			if [[ ${#FIRST} -le $WIDTH ]]
			then 	tui-printf -E "$FIRST"
				tui-printf $optStyle "$SECOND" "$THIRD"
			else	half=$[ ${#FIRST} / 2 ]
				part1="${FIRST:0:$half}"
				part2="${FIRST:$half}"
				tui-printf -E "$part1"
				[[ -z $SECOND ]] && \
					tui-printf -E "$part2" || \
					tui-printf -E "$part2" "$SECOND"
				[[ -z $THIRD ]] || \
					tui-printf $optStyle "$THIRD"
			fi
		fi
		# Since this is special handling, 
		# calling itself with reduced string lengths
		# so we can exit after this is done
		exit
	# else # just continue with the script below
	fi
#
#	Prepare Colors & Strings
#
	if [ $doEcho = true ]
	then	COLOR_LINE_START="\r${TUI_RESET}"
		COLOR_LINE_IDENT=""
		COLOR_LINE_CLOSE=""
		COLOR_LINE_END="\n"
	elif [ $doHeader = true ]
	then	# Full line
		COLOR_LINE_START="\r${HEAD_BG}${HEAD_FG}"
		COLOR_LINE_IDENT=""
		COLOR_LINE_CLOSE=""
		COLOR_LINE_END="${TUI_RESET}\n"
	elif [ $doTitle = true ]
	then	# 'Inlay'
		COLOR_LINE_START="\r"
		COLOR_LINE_IDENT="${TITLE_FG}${TITLE_BG}"
		COLOR_LINE_CLOSE="${TUI_RESET}"
		COLOR_LINE_END="\n"
	else	# The default
		COLOR_LINE_START="\r${TUI_RESET}"
		COLOR_LINE_IDENT=""
		COLOR_LINE_CLOSE=""
		COLOR_LINE_END=""
	fi
	case $ARGS_COUNT in
	0)	FIRST="$EMPTY"
		;;
	1)	if [ $doTitle = true ]
		then 	SECOND="$FIRST"
			num_o="${#SECOND}"
			num=$[ $num_o / 2 * 2  ]
			[ $num_o -gt $num ] && adder=" "||adder="  "
			EMPTY="${EMPTY:${num}+2}"	# The +2 is a temp bugfix
			half=$[ ${#EMPTY} / 2  ]
			FIRST="${EMPTY:$half}"
			THIRD="$FIRST$adder"
		else	# Usual arangement
			SECOND=$"${EMPTY:${#FIRST}}"
		fi
		;;
	2)	THIRD="$SECOND"
		SECOND="${EMPTY:${#FIRST}+${#SECOND}}"
		;;
	*)	# 3 (BETA - and more - handler)
		EMPTY="${EMPTY:${#FIRST}+${#SECOND}+${#THIRD}}"
		half=$[ ${#EMPTY} / 2 ]

		len_strings=$[ ${#FIRST} + ${#SECOND} + ${#THIRD} ]
		len_compare=$[ $len_strings / 2 * 2  ]
		[ $len_compare -lt $len_strings ] && \
			adder=1 || \
			adder=0

		SECOND="${EMPTY:$half}$SECOND${EMPTY:$half+$adder}"
		;;
	esac
#
#	Display & Action
#
	printf "${COLOR_LINE_START}${BORDER_LEFT}${COLOR_LINE_IDENT} ${FIRST}${SECOND}${THIRD} ${COLOR_LINE_CLOSE}${BORDER_RIGHT}${COLOR_LINE_END}"
#
#	BETA "if more"
#
	#unset ARGS[0] ARGS[1] ARGS[2]
	if [ $doMore = true ]
	then	# there are remains..
		C=0
		for A in "${ARGS[*]}";do 
			[ "$1" = "$A" ] && unset ARGS[$C] && echo $A
			[ "$2" = "$A" ] && unset ARGS[$C] && echo $A
			[ "$3" = "$A" ] && unset ARGS[$C] && echo $A
			((C++))
		done
		tui-title "DEBUG"
		tui-echo  "Too many arguments:" "$ARGS_COUNT($ARGS_COUNT_org)=${ARGS[*]}"
		# DEBUG
		echo $ME $optStyle "${ARGS[*]}"
		exit
	fi

Please understand, i've had no understand about lincenses, and still its a jungle to me.. .it'll change, i'm just not sure to what - atm...
I'm aiming to be as FOSS as possible, so i was told GPLv3 was the way to go/change?

Thank you in advance for any constructive input. Smilie
Moderator's Comments:
Mod Comment Tabs are maintained in CODE tags and seem to work just fine in the code shown. PHP tags have been changed to CODE tags. There are, however, lots of places where the code would be much more readable if it contained more tabs to highlight the structure of your code.

Last edited by sea; 07-24-2014 at 07:22 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH shell script question

I want to make shell script that takes a list of host names on my network as command line arguments and displays whether the hosts are up or down, using the ping command to display the status of a host and a for loop to process all the host names. Im new to shell scripting so Im not quite sure... (3 Replies)
Discussion started by: ewarmour
3 Replies

2. Shell Programming and Scripting

one question for bash shell script

Just one question for bash shell script. In bash script, you can use *.txt to call any files in current folder that ends with .txt, like rm *.txt will remove all txt file in current folder. My question is can you actually remember or use the file name among *.txt, I know file=*.txt will not... (9 Replies)
Discussion started by: zx1106
9 Replies

3. Shell Programming and Scripting

BASH script question

Hi, I want to create a script that gets a filename as an argument. The script should generate a listing in long list format of the current directory, sorted by file size. This list must be written to a new file by the filename given on the command line. Can someone help me with this? ... (6 Replies)
Discussion started by: I-1
6 Replies

4. Shell Programming and Scripting

bash script question

Can anybody be kind to explaing me what the lines below mean ? eval line=$line export $line ] || echo $line (2 Replies)
Discussion started by: jville
2 Replies

5. Shell Programming and Scripting

bash, help with stdout manipulation.

Hey all, Im kind of lost on how to do what I want so I figured I would ask. I want to pipe STDOUT of an app to a log file, but I want to prepend each line of that output with the date and time. Im drawing a complete blank on how to do this?? Any ideas? i.e. output is currently this:... (9 Replies)
Discussion started by: trey85stang
9 Replies

6. Shell Programming and Scripting

Mkbootfs writing to stdout in bash script

Hi, I need to automate some repacking tasks of a boot image for Android When in command line, I can use this command: mkbootfs /path/to/root > /path/to/ramdisk-recovery.cpio;However, if I try to run the command from a shell script under Ubuntu, it fails and outputs to stdout instead of the... (27 Replies)
Discussion started by: Phil3759
27 Replies

7. Shell Programming and Scripting

Question in bash script.

Hi All, I need an assistance with the issue below. I wrote big script in "bash" that automatically install an LDAP on Clients. I'd be happy to know in order to avoid duplication of entries in files, How i can define into the script, if the specific expressions already exist in the file, do... (7 Replies)
Discussion started by: Aviel.shani
7 Replies

8. Shell Programming and Scripting

Question about writing a bash script

Hello, I want to write a bash script to delete the content after '#'. However, if '#' appears in a string with "", ignore this. For example, input file: test #delete "test #not delete" Output file: test "test #not delete" Does anyone know how to write this script? Thanks (1 Reply)
Discussion started by: jeffwang66
1 Replies

9. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

10. Shell Programming and Scripting

Bash script search, improve performance with large files

Hello, For several of our scripts we are using awk to search patterns in files with data from other files. This works almost perfectly except that it takes ages to run on larger files. I am wondering if there is a way to speed up this process or have something else that is quicker with the... (15 Replies)
Discussion started by: SDohmen
15 Replies
All times are GMT -4. The time now is 06:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy