• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Splitting a Qshell Variable

    August 7, 2002 Timothy Prickett Morgan

    Hey, Ted:

    I have two values, separated by a space, in one Qshell variable. I want to split them into two variables. Is there a way to do that?

    — Hank

    I know of one way. I’ll show it to you. It will make you appreciate that good old, antiquated RPG.

    Let’s say the two values are a first name and last name, stored in a variable called name, and that you want to put them into variables called first and last.

    First, let me give you the commands to split one variable into two. Then I’ll explain how it works.

    set $(echo $name | ( read fn ln ; echo $fn ; echo $ln ))
    first=$1
    last=$2
    

    The first echo copies the value of the name variable to the standard output file (stdout).

    The vertical bar tells Qshell that the output of the echo command is to be the input to the command that follows the vertical bar.

    The command that follows the vertical bar is a compound command, made up of three simple commands: one read and two echos. Qshell knows it’s a compound command because of the parentheses that surround it and the semicolons that separate the three simple commands.

    The read reads the piped output of the first echo command and divides it into two variables. Everything before the first space gets stored in variable fn. Everything after the first group of one or more spaces gets stored into variable ln. Variables fn and ln are only defined within the compound command.

    The last two echo commands send the values of fn and ln to standard output.

    Notice that most of the line is enclosed in a command substitution expression. A command substitution expression begins with a dollar sign ($) and open parenthesis and ends with a close parenthesis. (The use of an older syntax, which uses backquotes as delimiters, is discouraged in Unix circles.) The output of a command substitution expression is fed back into the currently executing Qshell command, which in this case is a set command.

    The set command, then, has two arguments–the first and last names that were extracted by the read. It stores them into positional parameters 1 and 2.

    Not exactly the epitome of intuitiveness, is it?

    The last two lines store the two positional parameters into variables called first and last.

    Maybe an example will help.

    Suppose variable name contains the value Joe Smith.

    • The first echo runs, sending the string Joe Smith into the pipeline.
    • The read command reads one record from the pipe, containing the value Joe Smith. Read stores Joe into variable fn and Smith into variable ln.
    • The second echo command sends Joe to stdout.
    • The third echo command sends Smith to stdout.
    • The set command is interpreted as follows: set Joe Smith. This action stores Joe in the first positional parameter and Smith in the second positional parameter.
    • The two assignment statements copy the first two positional parameters into variables first and last.

    — Ted

    Sponsored By
    WORKSRIGHT SOFTWARE

    On June 30, 2002,
    $$$$$$$$    Postal Rates went UP!    $$$$$$$$

    On July 1, 2002,
    $$$$$    you wanted your postage bill to go down.    $$$$$

    We have the solution! CASS certify your mailing names and addresses and presort your outgoing mail and save. Our CASS certification software ensures that your address files have valid ZIP Code and address information. Our presort software ensures that you can properly prepare you mail for delivery to your Post Office.

    WorksRight Software, Inc. is the number-one source for iSeries and AS/400 CASS, presort, ZIP Code, and area code software and data.

    Visit our Web site – www.worksright.com – to learn more about our CASS and presorting software, or contact WorksRight Software, Inc., phone 601-856-8337,
    e-mail software@worksright. com .

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: mgo_rc, Volume 2, Number 59 -- August 7, 2002

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Confirm File Deletions in Qshell TCP/IP Printing Problem

    Leave a Reply Cancel reply

MGO Volume: 2 Issue: 59

This Issue Sponsored By

    Table of Contents

    • Valuable Newsletters
    • Splitting a Qshell Variable
    • Tell PC5250 to Stop Bugging Me about My Password

    Content archive

    • The Four Hundred
    • Four Hundred Stuff
    • Four Hundred Guru

    Recent Posts

    • Meet The Next Gen Of IBMers Helping To Build IBM i
    • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
    • Will Independent IBM i Clouds Survive PowerVS?
    • Now, IBM Is Jacking Up Hardware Maintenance Prices
    • IBM i PTF Guide, Volume 27, Number 24
    • Big Blue Raises IBM i License Transfer Fees, Other Prices
    • Keep The IBM i Youth Movement Going With More Training, Better Tools
    • Remain Begins Migrating DevOps Tools To VS Code
    • IBM Readies LTO-10 Tape Drives And Libraries
    • IBM i PTF Guide, Volume 27, Number 23

    Subscribe

    To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

    Pages

    • About Us
    • Contact
    • Contributors
    • Four Hundred Monitor
    • IBM i PTF Guide
    • Media Kit
    • Subscribe

    Search

    Copyright © 2025 IT Jungle