Coding Challenge #50 - Build Your Own Xargs
John Crickett
Helping you become a better software engineer by building real-world applications.
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:
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
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.
Solutions Architect @ iVedha | Ruby on Rails, GO | CKAD | AWS x1 | GCP x2 | FinOps Certified
7 个月Looking forward to "Build your own cryptocurrency"
Engineering Lead @ Salesforce / Founder / Builder
7 个月I've been appreciating your project build suggestions from afar. Definitely recommending them to my mentees
Helping to save lives through coding @ sureVIVE SA
7 个月I suggest: build your own bitcoin node