It is currently Sun Aug 20, 2017 1:47 pm

Welcome to antiX-forum.

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, join our community today!

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: bash scripting explained
PostPosted: Sat Aug 12, 2017 12:32 am 
User avatar

Joined: Thu Jun 29, 2017 2:24 am
Posts: 102
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:

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:

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:

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.)

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 0 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: