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 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 — firstname.lastname@example.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,