Added by on 2011-10-07

In this bash basics HakTip we’re turning exhaustively long commands into shell scripts with minimal effort.

Download HD Download MP4

Now a shell script is basically a set of commands written for your computers 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 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 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 ! dollar sign. 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 files 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.

Episode Keywords (Comma separated): shell, script, bash, terminal, tcsh, Unix, Linux, ping, permissions, chmod,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

17 Comments

  • dean 1 year ago

    note to editor:
    the background music is too loud; i had a difficult time hearing Darren talking over it.

    nice tip over all though :)

  • Nice I like the script command instead of typing the whole thing..

    But instead of just do +x .. why don’t you give it access like :
    chmod 777 file.sh

    And by the way.. I was wondering when are you going to talk a little about Teensy 2.0 :) ! – I have been programming on it for a little while, but got issues with spawn a meterpreter-shell … I would love to see a “How to” use Teensy as a meterpreter session enstablishment.

    Yours faithfully,

    GhostMX

  • DigiP 1 year ago

    When a file you want to run is not executable while in your path, you can also type bash and the file name. ie: “bash dateping.sh” or “bash ./dateping.sh” and it should execute it, without having to chmod it to executable. Have to be root though.

  • Clyde 1 year ago

    Darren, you ignorant… No, no, that’s too aggressive.

    Darren dear, maybe what you meant to say was… No, no, that’s WAY too wimpy.

    Darren, it’s not “Born again shell”, it’s “Bourne-again shell”. As in Stepen Bourne who wrote the default unix shell under unix system 7. (Wikipedia is your friends fathers former room-mate) Kids these days, I tell ya! Bank, zoom, to the moon, Alice!

    Clyde R. Visser, P.E.
    Resident Curmudgeon

  • David 1 year ago

    I use both Linux and Windows, so where possible, I like to use Perl scripts for this. If the commands or programs I am using are available on both platforms, using Perl means I can re-use the same script without using Cygwin or rewriting it in something ugly like Batch. Standard UNIX tools like dos2unix/unix2dos, wget, etc. are available on Windows through projects like GNUWin32, so you can just add them to your PATH variable and run the script like normal. Other scripting languages like Python could also be used.

    I find scripting languages like Perl to be easier to expand later on, by adding command line switches, for example.

  • Nice tips, have made an alias from your script, it looks like this:
    function dping {
    ping $@ | while read addr
    do echo “$(date +%F_%T) — $addr”
    done
    }

    it accept all ping parameters(notice $@) :)

    • Kehinde 1 year ago

      Nice one!
      Wanted to suggest that also.
      One can now ping any IP address instead of the default “8.8.8.8″ address demonstrated by Darren.

    • Kehinde 1 year ago

      Nice one ENQ!
      Wanted to suggest that also.
      One can now ping any IP address instead of the default “8.8.8.8″ address demonstrated by Darren.

  • adan rivas 1 year ago

    nicely done, but what if you are not running X. I mean for someone new, i would suggest opening up the editor and typing in the commands, save it to your home directory. Never be logged in as root, or save any executable as root. Then give it executable permissions, example: `chmod +x dude.sh`. after that if u really know what your doing and have permissions to run the shell script, just run: sudo ./dude.sh

  • Kehinde 1 year ago

    Nice one ENG!
    Also wanted to suggest this.
    One can now ping any IP address rather than with the default “8.8.8.8″ address demonstrated by Darren.
    But a few modification to your code below:

    function dping {
    ping $@ | while read addr
    do echo “$(date +%F_%T) — $addr”
    done
    }
    dping

    You din’t call the “dping” function below.

    • This function meant to be an alias, you put it into ~/.bashrc file and call it from terminal ex. dping -c 3 8.8.8.8 :) it works for me tho ;)

  • hi,
    first i thought it’s nothing new just scripts…
    but the editor call is awsome!
    i like the midnight commander and it’s working, too – lucky me :D
    export EDITOR=mcedit

  • Jake 1 year ago

    Darren,

    Liked the video, a lot of useful information for everyday usage. I think you should add a scripting video where you incorporate piping into other programs and tailing log files. :)

  • Flirlj 1 year ago

    Is there a way to execute your script like a regular command, without the ./ part? So you can issue simply dateping from anywhere, instead of /full/path/name/dateping or ./dateping?

  • Great job guys. You should definitely read the comments above and include those tips in the next bash basics. I’ll watch every episode of bash basics :)

  • vhazon 1 year ago

    On a related note, you can also setup bash to use this as an alias. Pop open nano, vi, or what have you, type alias commandiwanttouse=’actualreallylongcommandthatidontwanttotypeeachtime’

    Then, just save the file in your home directory as .bash_aliases and the alias will load when you restart bash or open a new terminal. Incidentally, you can put these aliases directly into your .bashrc file, but I find it easier to find my aliases if they’re all in their own file. You can also do a session-based alias that will be wiped out when you reboot. Just do the “alias command=’actualcommand’” directly in the terminal and you can use that alias for the duration of your session.

    To see what aliases you currently have, type “alias” and a list of them will pop up. Hope this is helpful to someone.