InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Parsing Output with a Function Call in bash
Bash

Parsing Output with a Function Call in bash

InfinityCoder February 21, 2017

You want to parse the output of some program into various variables to be used elsewhere in your program.

Arrays are great when you are looping through the values, but not very readable if you want to refer to each separately, rather than by an index.

Use a function call to parse the words:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env bash
# cookbook filename: parseViaFunc
#
# parse ls -l via function call
# an example of output from ls -l follows:
# -rw-r--r-- 1 albing users 126 2006-10-10 22:50 fnsize
 
function lsparts ( )
{
   PERMS=$1
   LCOUNT=$2
   OWNER=$3
   GROUP=$4
   SIZE=$5
   CRDATE=$6
   CRTIME=$7
   FILE=$8
}
 
lsparts $(ls -l "$1")
 
echo $FILE has $LCOUNT 'link(s)' and is $SIZE bytes long.

Here’s what it looks like when it runs:

1
2
3
$ ./fnsize fnsize
fnsize has 1 link(s) and is 311 bytes long.
$

We can let bash do the work of parsing by putting the text to be parsed on a function call.

Calling a function is much like calling a shell script. bash parses the words into separate variables and assigns them to $1, $2, etc.

Our function can just assign each positional parameter to a separate variable.

If the variables are not declared locally then they are available outside as well as inside the function.
We put quotes around the reference to $1 in the ls command in case the filename supplied has spaces in its name.

The quotes keep it all together so that ls sees it as a single filename and not as a series of separate filenames.
We use quotes in the expression ‘link(s)’ to avoid special treatment of the parentheses by bash.

Alternatively, we could have put the entire phrase (except for the echo itself) inside of double quotes—double, not single, quotes so that the variable substitution (for $FILE, etc.) still occurs.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Keeping Some Output, Discarding the Rest in bash
You need a way to keep some of your output …

Keeping Some Output, Discarding the Rest in bash

Comparing Two Documents in bash
It is easy to compare two text files (see Recipe …

Comparing Two Documents in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap