Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:52:34+00:00 2026-05-26T02:52:34+00:00

I am parsing command output and putting results into array. Works fine until exiting

  • 0

I am parsing command output and putting results into array.

Works fine until exiting the inner loop – output array is empty.

declare -a KEYS

#-----------------------------------------------------------------------------#
get_keys()
{

# this extracts key NAMES from log in format "timestamp keycode"
$glue_dir/get_keys $ip | while read line; do
    echo line: $line
    set -- $line # $1 timestamp $2 keycode
    echo 1: $1 2: $2
    key=(`egrep "\\s$2$" "$glue_dir/keycodes"`) # tested for matching '40' against 401, 402 etc
    set -- $key # $1 key name $2 keycode
    KEYS+=("$1")
    echo key $1
    echo KEYS inside loop: "${KEYS[@]}"
done
    echo KEYS outside loop: "${KEYS[@]}"
}

The output when run agains two output lines:

line: 1270899320451 38
1: 1270899320451 2: 38
key UP
KEYS inside loop: UP
line: 1270899320956 40
1: 1270899320956 2: 40
key DOWN
KEYS inside loop: UP DOWN
KEYS outside loop:

I spent an hour trying to figure this out. Please help. 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:52:34+00:00Added an answer on May 26, 2026 at 2:52 am

    When you use a pipe (|) to pass your command output to while, your while loop runs in a subshell. When the loop ends, your subshell terminates and your variables will not be available outside your loop.

    Use process substitution instead:

    while read line; do
       # ...
    done < <($glue_dir/get_keys $ip)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm taking array output from a command-line program and parsing it into a PHP
Currently we're parsing arp request output from the command line. string cmd = arp
My Ruby script is running a shell command and parsing the output from it.
What is a good way of parsing command line arguments in Java?
I've found lots of libraries to help with parsing command-line arguments, but none of
My team has a command parsing library for console apps. Each team around us
Anyone know of a library that handles parsing arguements passed from the command line
Currently, I'm passing a command into a user-defined function in PHP. How do I
Because the vertical output mode of the mysql command is very nice, I've setup
What is the best way of parsing command-line arguments in C++ if the program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.