0

My current snippet of code looks like this ...

#Location of network config files nfds="/etc/sysconfig/network-scripts/" #Standard prefer of network config files fil="ifcfg-" #Array variable that feeds "$nic" cards= array loop built from "nic=$(ls /sys/class/net | grep en)" #Set color for Divice labile div="\033[38;5;39m" #Set Fix format and colour info fix="\033[38;5;118m" #Set color for OK ok="\033[38;5;28m" #Clear All font and color info ctf="\033[0m" function currentCardDefRoute(){ defr=$(grep DEFROUTE $nfds$fil$cards | cut -d = -f 2) if [[ $defr = "yes" ]] || [[ $defr = "no" ]]; then echo -e " "$div$cards$ctf"'s current default route is\t"$div$defr$ctf"\t\t\t\t ["$ok"OK"$ctf"]" $st else echo -e " "$div$cards$ctf"'s current default route is \t"$fix"Missing"$ctf"\t\t\t ["$fix"PLEASE FIX"$ctf"]" $st fi } 

I indent 1 space on all echo lines for readability and consistent formatting. Keeping output readable and easy to understand.

Im looking to us the "columns" option and make the output more dynamic and have the format consistent no matter the screen size or var result. I would love to also get rid of all the "\t"s in my code. I have tried printf to no success.

I googled a lot of different ways and not seen the specific answer Im looking for or a variation I can draw an answer from.

Thank you for your help. btw. This is the first code I have ever written so go easy guys :)

6
  • please edit your Q with actual data/sizes/etc that put a scope to "make the output more dynamic and have the format consistent no matter the screen size or var result". For instance, if you have an 80 col screen, but 120 wide data, what do you want to happen? But basically, printf should be where you're spending your time on research. Once you understand 20-30% of the man printf doucmentation (by doing small samples), the rest will be obvious. Some printf man pages even have a section labeled examples. Good luck. Commented Mar 3, 2016 at 1:44
  • As you have a c tag, I'll mention that the printf in the c language is the "parent" of the shell printf functions. I'm not sure if you think you need to code in c to use printf, but you don't. (This one looks pretty good, but the features may differ depending on shell and OS versions : ss64.com/bash/printf.html ) . Good luck. Commented Mar 3, 2016 at 1:46
  • This is neither related to C nor to printf as defined by the tag (which is related to the C functions C). Commented Mar 3, 2016 at 1:56
  • The output is never more then 80 col I made sure of that in the rest of my code. I have researched printf and watched a lot of videos on youtube Kris Occhipinti and various others. Columns seams to be the syntax I need the help on, I will read more pages on what you suggested and see what I can find. Thank you Commented Mar 3, 2016 at 1:57
  • 1
    Side note: you can use variable references like $var inside double-quoted strings. You don't have to open and close the strings around them. Commented Mar 3, 2016 at 2:00

1 Answer 1

2

You may want to try using the column utility. It's sole purpose is for formatting output into columns. That may be easier than trying to do the same thing with echo or printf.

If you have to use printf, you'll want to use a format specifier like "%25.25s". The first number is the "minimum field width", which (in this case) causes the output to be at least 25 characters wide. If the output is shorter, it's padded with whitespace. The second number indicates the maximum number of characters to print. When these two numbers are the same, it effectively says to print the string in a field that's exactly 25 characters wide. You can use this to force varying-length strings to take up the same amount of space on the screen.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.