the reluctant tester

Perpetual learner of the craft of Software Testing,Servant Leadership and creating better Teams


Beginner Linux Shell scripting exercise for Testers

I have beginner level competency in using shell scripting for day to day Testing/Automation jobs, as I tend to use Python for it.

A recent task forced(sometimes that is a method that works 🙂 ) me to start learning Shell scripting a bit, so sharing it here.

Hope it helps other Testers to start exploring the powerful and configurable world of Linux shell scripting for automating some of their day to day Test environment/pipeline maintenance tasks (e.g. managing Docker containers which is the subject of this post)

Problem statement

  1. You are using a custom Docker image in a DevOps pipeline to run & debug automated functional tests for a web app
  2. During your test you have a need to dynamically run/stop/manipulate one of the various containers that you use to set up the environment for your tests
  3. This need is recurring (so a candidate for scripting)

Options considered

  • Use the API interface to the Docker engine through your preferred language/SDK
  • Use the Shell to the container (that you already happen to have during the tests) to script & control the container as required

Option tried

Controlling the shell that I already have during the tests to read the live container info and manipulate it as required made the most sense and was the quickest as well ( versus writing new code/class to use the Docker engine API)

Steps towards a solution –

  1. How do I get info about my container from the Shell ?

List all the containers running in the pipeline and filter the one that I am after ( my filter criteria was that I knew the name of the container that I was after. Debt… this filter criteria should be scripted and input as a variable in the future )

docker container ls

Grep comes in handy here as I can grep by container name to filter that one container out from the list. (Side note – Grep is a must learn Linux command for any Testers who are wanting to get more competent with basic DevOp-sy day to day tasks while debugging their Test pipelines)

docker container ls | grep 'myjenkins'

Above command gives me the below information –

Ok, good start, the first column Container Id (with value – 24faddb05aa3) looks useful as I can use this to uniquely control the container from here on in this test pipeline

2. How do i extract the Container Id from the last response ?

Now, I need a way to pipe this output into a variable, parse it and extract the container id . Oh and I would need this code to be reusable so that I can call it multiple times across my tests.

That is where Shell scripting comes in handy

3. Assign a variable to hold the output of the shell command

docker_info=$(docker container ls | grep 'myjenkins')

Do a wee debug test to see if I am getting the same output from the variable as in step 1 above

echo "$docker_info"

4. Great, now I just need to split the variable’s value to get the container id value

Split string/convert string to an iterate-able data structure, iterate through it and get the first index , hah ….easy to write in Python but wait oh, I am writing this in a shell script . More googling needed !

Obviously there are multiple ways to do this in shell scripting, but the neatest way that I have stumbled upon so far,is to use this intriguing Linux creature called – “IFS” shell environment variable.

IFS or Internal Field Separator is a shell variable that influences (permanently or temporarily in the shell session) how strings are delimited. (Note – apart from IFS, there were other option I considered e.g. how can grep output be directly passed to an array ? The “awk” command to manipulate and print strings , I have not covered those options in this post here)

Using IFS to split what is got from $docker_info to get the container id was a breeze…

#split the string using a delimiter , space using IFS comment and get the first element in the array

IFS=" "

docker_id=($docker_info)

echo "${docker_id[0]}"

5. Now use the extracted docker container id to make merry

e.g. Stop the container –> docker stop ${docker_id[0]}

Last step, create a .sh file to make these steps repeatable …

Summarising the shell script



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

About Me

I’m Sunjeet Khokhar

An experienced People Leader,Practice Lead  and Test Manager .

I am driven by the success of people around me, am a keen student of organisational behaviour and firmly believe that we can be better craftspeople by being better humans first.

CoNNECT with Me

%d bloggers like this: