HakTip

Hosted by Darren Kitchen and Shannon Morse.

Whether you're a beginner or a pro, HakTip is essential viewing for current and aspiring hackers, computer enthusiasts, and IT professionals. With a how-to approach to all things Information Technology, HakTip breaks down the core concepts, tools, and techniques of Linux, Wireless Networks, Systems... Read More

Bash Basics: Turn Long Commands into Scripts

Friday, October 7th, 2011 – running time 05:58

In this Bash basics HakTip, we're turning exhaustively long commands into shell scripts with minimal effort.

Now a shell script is basically a set of commands written for your computer's shell, or command line interface. If you're running Windows this might be the PowerShell or the Microsoft-DOS Command.com.

These aren't compiled programs. For those look into more powerful languages like C or Perl. But for getting the job done quick, sometimes a shell script is all you need.

In our example, I'm using the BASH, or the Bourne-Again shell. It's one of the more common shells you'll find out there for Unix-like operating systems. Though to stave off angy emails, I'll give a shoutout here to the C shell.

Now last week, we built a command using ping and date to give us nice timestamps for our ping replies. What I'd like to do now is turn that long string of commands into a nice looking script. To do so, I'll bring up the command in my shell and pop it into an editor with the nifty keyboard shortcut CTRL+x, CTRL+e. This copies the current line into our editor of choice. Now this can also be done with the fc command, but we're going to come back to that powerful command another time.

We'll need to prepend our command with a shebang. A shebang (or sometimes called a hashbang) is a sequence that specifies which interpreter to use when executing the script. The "she" -- supposedly short for "sharp" or "hash" is a # pound sign while the "bang" is a ! exclamation mark. This will be followed by the path to our interpreter. Since we're using bash that'll be /bin/sh

So our first line is now #!/bin/sh

From here we can pretty up our script, if we want, by replacing the semicolons ; with carriage returns. Here in nano we'll write this to a file with CTRL+o and give it a name of dateping.sh in our /root/ directory.

Now that our file has been saved, we can quit the nano editor with CTRL+x. Our script will begin executing, which is nice, but for this example we want to run it from our newly created file. So stop the ping with CTRL+c.

Navigating to my home directory with cd and looking for the .sh file with ls -l *.sh I find that our file isn't executable. I can tell because I don't see an "x" in the files permissions in the directory listing. It's also quite obvious with this shell because it wasn't displayed in green.

To make the file executable we'll use the chmod command to change the file mode. We could spend all day on the different numeric representation of file modes in regards to the user, group and everything else. But since we simply want to make it executable for ourselves, we'll use the + plus operator along with the x execute mode.

If we issue chmod +x file.sh and reissue ls -l *.sh we can verify that the file is now executable.

And finally with our file all ready to go, it's just a matter of invoking it with the complete path and filename. Since the file is located in our current working directory we can use the path shortcut . dot, followed by a slash / and the filename. Hit enter, Bob's your uncle, Robert's your mother's brother and there you have it.

So that leads me to ask, what programs, commands or scripts are rocking your world? Hit us up -- tips@hak5.org, or simply leave a comment below.

And be sure to check out our sister show, Hak5 for more great stuff just like this.


Netflix

With more than 23 million members, Netflix is the world's largest subscription service instantly streaming TV episodes and movies over the Internet. Members can instantly watch thousands of titles on a vast array of devices streaming TV episodes and movies like Microsoft's Xbox 360, Sony's PS3 game console and the Nintendo Wii console. As a Netflix unlimited member, you can instantly watch as many movies as you want, anytime you want for one low monthly price. There are no late fees or due dates. For a limited time, as a new member and a Hak5 viewer, you can get a FREE 30 day trial membership. Go to netflix.com/Hak5 and sign up NOW!

Connect with HakTip

  • 19 days ago

    Hackers!! Our new episode of Hak5 will be posting soon! It's jam packed with epic interviews this week, so stay tuned!

  • 19 days ago

    @michael_a_nass Are you enjoying the awesomeness that is Hack Across America?

  • 19 days ago

    @jardinesoftware Thanks for the bump! The Throwing Star LAN Tap Pro is one of my favorite tools (though, I prefer soldering mine! :) -@snubs

  • about a month ago

    Employers want social media passwords, US gets a #CPO, and #TheOnion! All that and more this time on #ThreatWire! http://t.co/SrZpicvnt6

  • about a month ago

    #Installing #Solar panels, #Google #Chrome #extensions, and more on @Hak5! http://t.co/QppYLgZpi5

  • about a month ago

    Legalizing #Internet eavesdropping, #LivingSocial is #hacked, and more on this weeks #ThreatWire! http://t.co/xyIxzy8kes

About the Show

Details
Show Title:
HakTip
Description:
Whether you're a beginner or a pro, HakTip is essential viewing for current and aspiring hackers, computer enthusiasts, and IT professionals. With a how-to approach to all things Information Technology, HakTip breaks down the core concepts, tools, and techniques of Linux, Wireless Networks, Systems Administration, and more
Categories:
How-To & DIY, Technology & Gadgets
Launch Date:
May 23, 2011
Episodes: