Sponsored Content
Top Forums Shell Programming and Scripting [BASH] Performance question - Script to STDOUT Post 302907660 by Don Cragun on Monday 30th of June 2014 10:39:34 PM
Old 06-30-2014
Here are a few additional comments on your code to consider:
Line 17:
Using bash version GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13) on Mac OS X, the statement: ME_DIR="${0/${0##/*/}}" gives me the error: bad substitution: no closing `}' in ${0##. This may be a bug in this version of bash, since ksh sets ME_DIR to the directory specified when your script is invoked (or an empty string if no directory was specified). There are several other ways (easier or faster or both) to get the directory from $0 depending on how ME_DIR is to be used, but since ME_DIR is never referenced after being set, the best thing to do is to just delete this line.

Line 37:
This can be more simply written as WIDTH=${COLUMNS:−$(tput cols)}

Line 38:
TOTAL is not used; delete this line.

Line 39:
This can be more simply written: ((WIDTH -= (${#BORDER_LEFT} + ${#BORDER_RIGHT} + 2)))

Lines 52, 54, 56, 73, 109, 114, 120, 135, 172:
If you change var=true and var=false to var=1 and var=0, respectively; you can then simplify if [ $var = true ] to if [ $var ]. These changes won't make a huge performance difference, but assigning one character to a variable is a little bit faster than assigning for or five characters and [ $var ] is a little faster than [ $var = string ].

Line 49:
This comment doesn't seem to be needed. If it is needed, it should appear closer to line 58.

Line 64-73:
I have no idea why you are playing all of these games with your command line arguments. If you want to know how many arguments there are, use $#. I see absolutely no reason why it wouldn't be valid to call tui-printf with two empty strings and a third non-empty string tui-print "" "" "right string" to print empty left and middle headers. To determine if $2 was given on the command line, use [ $# -ge 2 ] not [ ! -z "$2" ] (and not [ -n "$2" ] which would have been a simpler way to code that).

Line 64:
Of course ARGS=(${*}) fails to preserve argument boundaries if any of the args contain spaces. If you insist on using $ARGS instead of just using $1 though $n, you need something like ARGS=("$@").

Line 137-140:
I don't understand why you have such complex code to set adder to a single space. I would have thought you would want to set adder to a space if the length of $2 was odd, or to an empty string if the length was even. You could replace these four lines with the simpler (and probably faster):
Code:
	num=$(($#SECOND / 2 * 2))
	[ $num -ne ${#SECOND} ] && adder=' ' || adder=''

Line 155-159:
The variable len_compare is not needed here and your computation of adder is slow compared to other alternatives. Change lines 156-159 to:
Code:
	adder=$((len_strings % 2))

Note tha I also strongly suggest that you change adder to somehting else. You used adder on lines 139 and 143 to hold a string; while on lines 158, 159, and 161 you use adder to hold a number. Please use different variables for each intended use.

Line 167:
All of your calculations of string lengths are completely wrong if any operand to tui_printf contains any percent characters (%), backslash escapes (such as \n, \t, and \b), literal control characters, or non-printing characters.

If the user could ever want literal \ characters or literal % characters to be included in the printed output, you should change:
Code:
printf "${COLOR_LINE_START}${BORDER_LEFT}${COLOR_LINE_IDENT} ${FIRST}${SECOND}${THIRD} ${COLOR_LINE_CLOSE}${BORDER_RIGHT}${COLOR_LINE_END}"

to:
Code:
printf "${COLOR_LINE_START}${BORDER_LEFT}${COLOR_LINE_IDENT} %s ${COLOR_LINE_CLOSE}${BORDER_RIGHT}${COLOR_LINE_END}" \
	"$FIRST$SECOND$THIRD"

This User Gave Thanks to Don Cragun For This Post:
 

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 10:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy