Coding Challenge #50 - Build Your Own Xargs

Coding Challenge #50 - Build Your Own Xargs

This challenge is to build your own version of the Unix command line tool xargs!

The Unix command line tools are a great metaphor for good software engineering and they follow the Unix Philosophies of:

  • Writing simple parts connected by clean interfaces - each tool does just one thing and provides a simple CLI that handles text input from either files or file streams.
  • Design programs to be connected to other programs - each tool can be easily connected to other tools, via files and streams, to create incredibly powerful compositions.

Following these philosophies has made the Unix command line tools some of the most widely used software engineering tools which can be chained together to create far more complex and powerful set of tools that you’d expect.

Xargs epitomises the philosophy by providing a tool to allow us to connect together programs effectively, using the output of one to configure the behaviour of the next.

If You Enjoy Coding Challenges Here Are Four Ways You Can Help Support It

  1. Refer a friend or colleague to the newsletter. ??
  2. Sign up for a paid subscription - think of it as buying me a coffee ?? twice a month, with the bonus that you also get 20% off any of my courses.
  3. Buy one of my courses that walk you through a Coding Challenge.
  4. If you work for a company that sells to software engineers, encourage them to sponsor the newsletter. ??

The Challenge - Building You Own Xargs

This challenge is to build your own version of xargs. As always with command line tools a great way to find out what the tool does and how to use it is to use man:

NAME
     xargs – construct argument list(s) and execute utility

SYNOPSIS
     xargs [-0oprt] [-E eofstr] [-I replstr [-R replacements]
           [-S replsize]] [-J replstr] [-L number] [-n number [-x]]
           [-P maxprocs] [-s size] [utility [argument ...]]

DESCRIPTION
     The xargs utility reads space, tab, newline and end-of-file
     delimited strings from the standard input and executes utility
     with the strings as arguments.

     Any arguments specified on the command line are given to
     utility upon each invocation, followed by some number of the
     arguments read from the standard input of xargs.  This is
     repeated until standard input is exhausted.
        

You can read about how useful xargs can be in my Developing Skills newsletter article that explains how I used it to build a simple load testing tool for a RESTful API.

Step Zero

In this introductory step you’re going to set your environment up ready to begin developing and testing your solution.

I’ll leave you to setup your IDE / editor of choice and programming language of choice.

Step 1

In this step your goal is to build the command ccxargs that will take a whitespace separated set of strings from standard in and convert them into command line arguments that can be passed to a command (referred to as utility in the man page quoted above).

You can test your code using, this command below to create three text files we can use for testing:

% for i in {1..3}; do echo "This is file ${i}" > test-${i}.txt; done;
        

Then in the same directory we can use ls to create a whitespace separated list of files and pipe that into our ccxargs program which we will tell to run the command cat with each of the items in the list as the argument to cat:

% ls | ccxargs cat                                                     
This is file 1
This is file 2
This is file 3
        

This is the equivalent of having done:

% cat test-1.txt test-2.txt test-3.txt 
This is file 1
This is file 2
This is file 3
        

Bonus points if you use your own version of cat from the build your own cat Coding Challenge.

Continued...

You can find Step 2 and beyond on the Coding Challenges website as build your own Xargs.

Or if you'd rather get the whole challenge delivered to you inbox every week, you can subscribe on the Coding Challenges Substack.

Muralish Clinton

Solutions Architect @ iVedha | Ruby on Rails, GO | CKAD | AWS x1 | GCP x2 | FinOps Certified

7 个月

Looking forward to "Build your own cryptocurrency"

Brian Feister

Engineering Lead @ Salesforce / Founder / Builder

7 个月

I've been appreciating your project build suggestions from afar. Definitely recommending them to my mentees

Antonio Alvino

Helping to save lives through coding @ sureVIVE SA

7 个月

I suggest: build your own bitcoin node

要查看或添加评论,请登录

社区洞察

其他会员也浏览了