Shell Scripting Tips

January 24, 2018

tips
The shellscript.sh website has a new section:
https://www.shellscript.sh/tips/

This is a collection of hints and tips that I have written up over time; it covers individual tasks, problem areas, gotchas, and tips.

Go and browse, I’m sure you’ll find something of interest!

Steve


http://www.shellscript.sh/

August 31, 2016

My main Shell Scripting tutorial has moved to its own domain, at http://www.shellscript.sh/

This is easier to navigate, more tablet and mobile friendly, and generally more usable than before.
It also includes all the links to allow you to buy the content for yourself from Amazon (in eBook and Paperback), as well as for my full “Shell Scripting” book, as published by Wiley of New York.

And of course every page ends with a form to contact me with your feedback and comments.


Create an RPM

March 6, 2016

Creating an RPM package is incredibly easy, but somehow seems to be quite tricky to describe to people. I’ve written a short eBook (also available in Paperback) which comes with a free downloadable VirtualBox virtual machine image for working through the examples.

How to Create an RPM

I would love your feedback on this book, as it’s not a subject where there is necessarily a single “right” or “wrong” approach. What I’ve tried to cover here, is a minimal-effort guide to creating an RPM. There are other, more thorough, more source-tarball-based, ways of creating RPMs. And there are also a few methods similar to that which the book describes, which are arguably easier to manage.

The approach presented in this book, however, is the most minimalist way to go about making an RPM.


Creating RPMs – Easily!

August 25, 2015

Here is a “minimal” guide to creating RPMs at http://steve-parker.org/rpm/ – I do plan to expand it further, but want to put something out there for now, for feedback at least, and to help people to make RPMs without necessarily having to dig deep into all the extra stuff that RPM can do for them.

I plan to make this into a more detailed and complete document, but for now, I’d love you to read this and tell me what you think.


Shell Shock!

September 26, 2014

One of the best ways to exploit “Shellshock” is via a CGI script executing Bash. Not many people write CGI scripts in Bash, but I do, and I published one in a book, too. So I’ve written about it: http://steve-parker.org/articles/shellshock/


Efficient Shell Scripting

January 22, 2014

In an article on ITworld.com, Sandra Henry-Stocker gives advice about writing efficient shell scripts.

Whilst a lot of the principles provided would appear to make sense, most of them actually do not make any significant difference, and some are entirely wrongly measured.

First, Henry-Stocker suggests replacing this script:

for day in Mon Tue Wed Thu Fri
do
    echo $day
    touch $day.log
done

… with this one:

for day in Mon Tue Wed Thu Fri
do
    if [ $verbose ]; then echo $day; fi
    touch $day.log
done

I ran both scripts 5,000 times, like this:

for x in `seq 1 5000`
do
  for day in Mon Tue Wed Thu Fri
  do
      echo $day
      touch $day.log
  done
done

… and similarly for the second script.

The “slow” script ran in 21.425 seconds on my PC, the “fast” script, which although it does not echo anything, instead parses and executes the test, which means that it took longer – 25.178 seconds, or 17% slower than simply running “echo” every time.

I would also note that the syntax if [ $verbose ] is asking for trouble, in real scripts I’m sure she would agree that you should use something like: “if [ "$verbose" -eq "y" ]“.

If the code is running on an old Sun framebuffer console, which will update the screen at around one second per line, all this needless echoing would make a difference, but in any real-world situation in 2014, the overhead of the test is far slower than writing the output.

Over on page two (because it’s all about selling advertising space 🙂 ), order of comparison is taken on. Whilst in principle, it could make a significant difference, the example given involves a single if statement, no fork()ing, and some simple variable comparisons:

echo -n "enter foo> "; read foo;
echo -n "enter bar> "; read bar;
echo -n "enter tmp> "; read tmp;


if [[ $foo -eq 1 && $bar -eq 2 && $tmp -eq 3 ]]; then
    echo ok
fi

Taking out the read from the tests, we find that it takes 0.083 seconds to do 5,000 runs of the full test, with all variables matching (so all three conditions have to be tested each time), and 0.033 seconds when the first condition does not match, so it takes just over twice as long to run three tests as it does to run one test.

This is a significant difference, but it’s not the 1.195 seconds per iteration suggested by the article, it’s 0.00001 second per iteration. Taking Sandra Henry-Stocker’s results at face value, my tests which each took well under 1 second, would have taken 4 hours 5 minutes, or 2 hours 26 minutes respectively.

If one comparison was particularly time-consuming, it would be a more effective example. Here, if the find command takes 10 seconds to run, but foo is usually 1, then this will take 11 seconds:
if find /var -name foo.txt && [ "$foo" -eq "93" ]; then ...
whilst this will take 1 second, 1000% faster:
if [ "$foo" -eq "93" ] && find /var -name foo.txt; then ...
The example provided just doesn’t match the claimed results.

Avoiding unnecessary cat, echo and similar statements is good advice; not as significant as it was 10 years ago, and much less significant on Linux, where fork()ing is much faster than on Unix.


VMWare Balloon Size

October 29, 2013

This is just a quick note as it took me a while to find. I assumed that vmware_balloon.c would write stats into /proc, like most Linux kernel modules do.

That’s not how vmware roll, however. To find out how much RAM has been claimed by VMWare’s “Balloon” driver from within the guest OS itself, use the vmware-tools command:

# vmware-toolbox-cmd stat balloon
4460 MB
#

Shell Scripting Tutorial on Kindle

March 29, 2013

Unix & Linux Shell Scripting Tutorial on Kindle

Unix & Linux Shell Scripting Tutorial on Kindle

The Shell Scripting tutorial at http://steve-parker.org is now available natively on the Kindle!

USA (amazon.com)

UK (amazon.co.uk)

Similarly, you can search for “B00C2EGNSA” on any Amazon site, or just go to http://www.amazon.COUNTRY/dp/B00C2EGNSA (where “COUNTRY” is .fr, .de, etc) for your local equivalent.


Persian translation of Shell Scripting Tutorial

December 15, 2012

Mahmood Pahlevani has translated my Shell Scripting tutorial at http://steve-parker.org/sh/sh.shtml into Persian, at http://bashlinux.persiangig.com/stev/sh/index.shtml

Any feedback on this work is welcome here, I will pass on all praise to Mahmood,

Steve


Track memory usage on Solaris

November 15, 2012

When a Solaris server is overloaded, this is one way to check what actual memory each process is using. Here I am restricting the checks to one user (“steve”) but by omitting the “-u steve” flag to ps, the whole system will be checked.

$ps -ea -o pid,rss -u steve | sort -n -k 2 | while read pid rss
> do
> echo -en "RSS : ${rss} Kb: "
> pmap -x $pid | tail -1
> done
RSS: 50104 Kb: total Kb 1384400 712744 494312 -
RSS: 669296 Kb: total Kb 1414648 819496 657584 -

This uses the pmap tool to inspect the actual memory usage, which as the ps(1) man page says, is more accurate than just the RSS field of ps.

I suspect that a method using pmap alone might be possible, but this is just a quick note as I go…