Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Bash String Tokenizing

Bash has some functionality to enable building a string tokenizer, by means of four modifiers:
  • #
  • ##
  • %
  • %%
We can then use to to get the first or last elements, from the end or the start of the string:

#!/bin/bash
TEST="i/am/a/very/big/dir"
echo ${TEST#*/}
echo ${TEST##*/}
echo ${TEST%/*}
echo ${TEST%%/*}

# am/a/very/big/dir
# dir
# i/am/a/very/big
# i

echo ${TEST#*/*/}
echo ${TEST%/*/*}

# a/very/big/dir
# i/am/a/very

I have omitted a leading slash in this string, which was a directory, so that it would print "i" instead of nothing. So you can use echo ${TEST%%/*}  to get the first element and saving echo ${TEST#*/} on every iteration of a cycle that tokenizes the string into an array.

The last two expressions serve to show that if you're dealing with some hardcoded delimiters, you can further expand it so that it matches what you want precisely.

BASH - Executing Commands Parsed From File

Say you need to parse some executable files from wherever - they may come from an "ls" or from a "cat" to a file - and then run them. We can do this pipe, for instance:

ls -lR dir/to/pipe  | grep \\-rwxr\-xr\-x | awk '{ print $9 }' | bash

Which will pipe the executables from the "ls" command to awk for treatment and then to "bash" to be executed.
If you need to execute files treated from another way, just be sure they are arranged as one per line.