Welcome Guest | My Membership | Login

Expanding Your Toolkit: Using Saved Lists at the Linux $ Prompt


Expanding Your Toolkit is an occasional series which helps us integrate the MV and non-MV aspects of our jobs.

While I spend a majority of my working life in Multivalue — Unidata, specifically — I spend a good amount of time working in other environments as well. One of my favorite features of MultiValue that I miss in other environments is saved lists. I can't tell you how many times I've been working in a Linux shell and thought, "if only I had a saved list of items, this would be so much easier."

I recently needed to verify the compile dates on a list of items in a very cluttered program file. If only I had a saved list of the programs in Linux! With a saved list I could get my list, list the file and check the compilation dates on the items. But we can't do saved lists at the $ prompt in Linux. Or can we?

Spoiler alert: We CAN. Okay, so maybe it's not saved lists in the traditional MultiValue syntax, but we can do the same kind of thing quite easily.

Recently I needed to list the compile date of approximately 20 programs in a directory. To do this, I created a file in /tmp called list and cut-and-pasted the program names into this file, one program name per line. With this file in place, I used cd to get into the directory and then issued this command:

cat /tmp/list | xargs ls -l

The xargs command means "expand arguments." For each line in the /tmp/list file , xargs will add the file names after the ls -l up to the globbing limit , that is, the maximum length of a single shell command. If Linux runs out of space for the command before it runs out of names to add to the command, it'll reissue ls -l followed by the file names, again and again, until the list of names has been fully consumed.

This is just one way to use the xargs command. A little time with the xargs man page can reveal a number of other useful nuggets.

While this was useful, what I really needed was the compilation dates on the items, not the last update date on the source. And being just a little bit lazy, I really didn't want to have to go back and add an underscore in front of each item I had pasted into the /tmp/list_file (Unidata stores compiled code in the program file with an underscore in front of the name).

So rather than use xargs , I decided to use a for-loop instead. In my opinion, the for-loop is one of the most powerful shell scripting tools available, made even stronger by the fact that you don't need to create a shell script to use it. It works just as well from the command line.

The basic syntax of the for-loop looks like this the code:

for variable in `command` ; do
shell commands using $variable

Simply replace the variable with some letter or word and then use that variable between the do and done. That's it. For my particular dilemma — finding object code for the list of source items in /tmp/list — the command was as simple as:

for i in `cat /tmp/list` ; do
ls -l _$i

And yes, this is entered directly at the $ prompt! Here's how it works:

The backtick (`) operator runs a command. For each entry (variable i) in the result set, the scripting commands between the do and done are executed. The value of each entry in the result set (i) can be retrieved as a standard environment variable using $i. In this case, the loop expands to:

ls -l _item_1
ls -l _item_2
ls -l _item_3

ls -l item_n

The net result is that any list of items in a text record can be used as a saved list either through xargs or using a for-loop. Just because we're at the $ prompt shouldn't limit us from some of our MultiValue goodness.


# # #          # # #          # # #


Related Articles

  • From the Inside November/December 2017

    Company: International Spectrum

    There is a constant battle between business and technology, and it looks like businesses might finally realize that winning is losing. Over the last decade (and more) companies have been in a battle between the need for technology and the requirements of business' rules and practices.

  • 2017 MultiValue Industry End-of-Year Recap

    Company: SJ+ SYSTEMS ASSOCIATES, Zumasys, Inc, Rocket Software, ONgroup Intl, Ladybridge Systems Ltd, Entrinsik

    Recent years have moved technology deeply into the day-to-day of nearly every culture. We've asked some key influencers in the MultiValue world to talk about how they moved their products forward in 2017. They also talked to us about where they'll be focusing in 2018.

  • Case Study: Clarke Transport Migrates to jBASE

    Company: Zumasys, Inc Database: jBASE

    At the heart of Clarke Transport's UK business is a powerful transportation management system written in Pick BASIC. After hiring a series of new developers, Clarke Transport decided to embrace jBASE for its native Windows capabilities, which makes it easier for programmers without MultiValue knowledge to master the environment.

  • Business Tech: UI/UX Part IV: Kansei Engineering

    Company: HDWP

    When we think of programming as engineering, we can get lost in a sea of methods like Waterfall and Agile. This isn't that article. Kansei engineering is a guiding principal for mechanical engineering which adapts surprisingly well to programming. Let's take our view of UX up a notch and examine a philosophy that promotes user experience to the top of our priority list.

  • Linux Shared Memory and Your MultiValue Database

    There are a number of different ways for programs to pass data to each other. One of the most common in the MultiValue world is using disk. (Remember the old "port" files?) Sometimes, however, this can still be a bottleneck. You might want to consider putting that data into shared memory.

Return to top