16
Beginners Bash Scripting for Fun and Profit Finally, the power of Unix tools combined with an OS containing the richest soup of malware, together at last Before we go any further: Yes, I know, running Bash on Ubuntu on Windows is now a thing...

Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

Beginners Bash Scriptingfor Fun and Profit

Finally, the power of Unix tools combined with an OS

containing the richest soup of malware, together at last

Before we go any further:Yes, I know, running

Bash on Ubuntu on Windows is now

a thing...

Page 2: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

What is scripting?

● Doing the same thing over and over again?● Make it a script...

– Save time typing

– Reduce errors

– Improve efficiency

● Maybe add it to startup eg rc.local?● Maybe add it to cron?● A substantial proportion of Linux is scripts!

– find /usr/bin -print0 |xargs -0 file |grep script

– ls -al /etc/init.d

Page 3: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

How do I start?

● Anything you type at the commandline can be run as a script...– Create the file, and don't forget the sha-bang to set the interpreter:

● vi myscript.sh

– Add the commands you want to run (see next pages)

– No compilation is necessary

– Use bash -x to see explicitly what's happening during execution

– Make it executable: ● chmod +x myscript.sh

– Store it somewhere accessible and appropriate● sudo chown root:root myscript.sh● sudo mv myscript.sh /usr/local/bin

– There are literally hundreds of worthy tutorials available on the web.

Page 4: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

Example First Script

● Start with a sha-bang, then go for it...

Page 5: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

Example Second Script

Page 6: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

Host lookup – just the IP, thanks

Page 7: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

Something useful: mylame

● Lame ain't an mp3 encoder– Well actually, it is now.

● Lame can only turn one wav file into an mp3 at a time.– lame *.wav #doesn't work!!!

● We can write a script that accepts multiple arguments, invokes lame for each one, additionally ensures -h is specified for high quality, and accepts any other options we provide

● A good script is indistinguishable from programming!

Page 8: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

Mylame.minimal

Page 9: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,

mylame.full

Page 10: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,
Page 11: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,
Page 12: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,
Page 13: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,
Page 14: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,
Page 15: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,
Page 16: Beginners Bash Scripting for Fun and Profitlinuxlsga.net/bash-fun+profit.pdf · How do I start? Anything you type at the commandline can be run as a script... – Create the file,