Posts: 148
figosdev
Joined: 29 Jun 2017
#1
if you find code or a script written"in bash" it is probably written for a number of common utilities for gnu/linux.

bash has some native commands, but most are actually external programs. some are not standard, and you can call programs youve written yourself from bash scripts. take this code:

Code: Select all

echo"find text:";e="$(echo -e \\033)";read p;find -type f|egrep -v"\ |^\.\/sys\/|^\.\/proc\/|^\.\/dev\/|^\.\/run\/|headers|modules|gnu|cert|icons|^\.\/live\/"|xargs grep"$p"|cat -A|sed"s/\^\[/$e/g"
what you actually have here is:

a few echo commands (echo writes out text)
a read command (gets input from the keyboard)
some variables ($p and $e)
the find command (searches for files)
the egrep command (searches through text for multiple items at once)
the xargs command (turns a list of files or text into input for another command)
the cat command (writes out text, but has slightly different input than echo)
the sed command (does many things; used here to make changes to text)

this code:

Code: Select all

echo"find text:";e="$(echo -e \\033)";read p;find -type f|egrep -v"\ |^\.\/sys\/|^\.\/proc\/|^\.\/dev\/|^\.\/run\/|headers|modules|gnu|cert|icons|^\.\/live\/"|xargs grep"$p"|cat -A|sed"s/\^\[/$e/g"
does the same thing as this code:

Code: Select all

echo"find text:"
e="$(echo -e \\033)"
read p
find -type f|egrep -v"\ |^\.\/sys\/|^\.\/proc\/|^\.\/dev\/|^\.\/run\/|headers|modules|gnu|cert|icons|^\.\/live\/" |
xargs grep"$p" |
cat -A |
sed"s/\^\[/$e/g"
which does this:

* change colour to yellow, say"find text:" and gets input
* finds files, skipping filepaths that begin with /sys/ or /proc/ or /dev/ or /run/ or ./live/ or that contain any of these:"headers""modules""gnu""cert""icons"
* feeds that list to grep to search each file for the thing you typed in
* feeds all of those searches through cat -A which transforms characters that would give problems to plain keyboard text
* changes all isntances of ^[ to a code that says"make this text yellow." (it reduces noise in the output.)

bash can be easier than this code, and this isnt even a good script. its a bash one-liner, which sometimes exchanges easily read code for the sake of"putting all code on a single line."

i like bash one-liners, because theyre easy to organise. you can even fill your bash history with them, start each one with its own name, and call them easily as needed.

its a better and more conventional practice to assign such things to aliases in .bashrc (a list of lines to run when bash starts, or when you go to the command line.)