Linux Scripting, Automate Linux
Introduction:
We all love Linux, but sometimes its a bit to much to handle all the hassle.
Thats why I am going to handle Linux Scripting how.
There are a couple of good scripting languages out there for Linux. They are bash, perl, python, awk and sed.
Basic Scripting:
I am going to handle Bash mostly now. Bash is the Basic scripting Language for the Bounce Again Shell.
Bash can not to a much, but when you use grep and wget and curl you can make a really nice script.
Quick tips
Like: `cat /dev/urandom` will give you a Random number.
Ah, isnt that greatwell like I said before Grep is great.
Its great since you can figure-out if there is a text in the file. If you use that together with wget. You can check if a Pecific text on a webpage.
Lets see a working script:
Ok, lets have a look at this script: `history | awk '{print $2}'|sort |uniq -c |sort -n|cowsay|more`
Lets dig into that. history gives the history of old used commands in Bash. we Pipe that (|) to the awk script: {print $2} that is print the Second Argument. lets continue. sort is.. well you guess. SORTING as we are still pasing the history over we sort history. Then we come at uniq -c.
Thats a program that can put a list of Unique things in a list. Of the given argument. As we give a list of Sorted commands we used in the history we get a list with only Unique results. After that we will also notice that there is -c in there. That means Count the results. We Pass that to sort -n that means a Number sort. As there were numbers put before the names before since of the uniq -c thing we will see the thing that happened the most on top. and they go lower and lower all the time. That gets passed to cowsay.
To make it look fun.
And then we have our big friend more and that is so we can read our text. And it will not scroll and end.
This all gets printed by print in the Awk part.
And we get a output like:
Code:5 cat 5 mv 9 htop 11 man 11 rm 11 sudo 40 vim 66 ls 82 cd 104 exit
Conclusion:
The trick of using scripting in Linux is using other programs to do the tasks you want it to do.
Bash scripting often comes in handy at my work. We use it for reporting, automation of tasks and man other things.
Not bad. Thanks!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks