standard input to read

standard input to read

Post by Uwe Koh » Fri, 29 Jan 1999 04:00:00



HI,

I'm trying to execute the following command on hp - ux:

ls /tmp | read a

the output from "ls /tmp" should be read to the variable a.

but "echo $a" shows a return. Nothing else (and there are files in /tmp!).

Someone knows wy it does not work?

Thanks

---------------------------------------------------------------------------

Dipl.-Ing. Uwe Kohl                              DDDD          K   K
FG Datenverarbeitung in der Konstruktion (DiK)   D   D   ***   K  K
TU Darmstadt                                     D    D  ***   K K
Petersenstr.30                                   D     D       KK
D-64287 Darmstadt                                D     D  I    KK
Tel: +49 6151 16 5484                            D    D   I    K K
Fax: +49 6151 16 6854                            D   D    I    K  K

WWW: http://www.dik.maschinenbau.tu-darmstadt.de
---------------------------------------------------------------------------

 
 
 

standard input to read

Post by Andreas Geissle » Fri, 29 Jan 1999 04:00:00


Uwe Kohl schrieb:

Quote:> HI,

> I'm trying to execute the following command on hp - ux:

> ls /tmp | read a

> the output from "ls /tmp" should be read to the variable a.

> but "echo $a" shows a return. Nothing else (and there are files in /tmp!).

> Someone knows wy it does not work?

what about

a=`ls /tmp`

andi.

  andi.vcf
< 1K Download

 
 
 

standard input to read

Post by Avi Abram » Fri, 29 Jan 1999 04:00:00



> I'm trying to execute the following command on hp - ux:

> ls /tmp | read a

> the output from "ls /tmp" should be read to the variable a.

> but "echo $a" shows a return. Nothing else (and there are files in /tmp!).

> Someone knows wy it does not work?

Let me assume that what you are trying to do is store the list
of files in the /tmp directory in a shell variable.

The way it is usually done (in the Bourne shell) is thus ...

    a=`ls /tmp`

Note the use of back-quote characters (`) as opposed to
regular single quotes (') or double quotes ("). This is all
well documented in (you guessed it) TFM (as in - RTFM ;-)
I'm referring to man sh(1) - for the use of back quotes and
variable assignment.

Of-course my next question is - what are you really trying to
do? What do you want to do with the list of files in /tmp?
Just as there are different ways to assign values to shell
variables, there are different ways to completing a given
task.

If you're just playing around with the "read" command, I'm
afraid I can't enlighten you as to why your construct
doesn't work.

HTH,
Avi.

************************************************************************
  /\ \    /| Avi Abrami, Analyst/Programmer, Telegate Ltd.
 /__\ \  / | 7 Haplada Street, Or-Yehuda, ISRAEL

 
 
 

standard input to read

Post by Cal Duniga » Fri, 29 Jan 1999 04:00:00



> I'm trying to execute the following command on hp - ux:

> ls /tmp | read a

> the output from "ls /tmp" should be read to the variable a.
> but "echo $a" shows a return. Nothing else (and there are files in /tmp!).

> Someone knows wy it does not work?

Other posters have offered alternative that will work, but the reason
it doesn't is instructive.  The ls command sends the list of files in
single-column format when the output isn't going to a terminal, and
the read command reads in only one line.

What you _should_ see is the first filename in /tmp.  Possibly you
have a filename in /tmp consisting of only whitespace or non-printable
characters.  It would pop up first.  Or possibly you are using a
version of ls that produces an empty line first.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

      Consulting                   wrong with a world in which Ken
      Modeling                     Thompson lives in obscurity and
      Training                     Bill Gates is a famous billionaire.
//////////////////////////////////////////////////////////////////////

 
 
 

standard input to read

Post by Dan Merc » Fri, 29 Jan 1999 04:00:00





>> I'm trying to execute the following command on hp - ux:

>> ls /tmp | read a

>> the output from "ls /tmp" should be read to the variable a.
>> but "echo $a" shows a return. Nothing else (and there are files in /tmp!).

>> Someone knows wy it does not work?

> Other posters have offered alternative that will work, but the reason
> it doesn't is instructive.  The ls command sends the list of files in
> single-column format when the output isn't going to a terminal, and
> the read command reads in only one line.

> What you _should_ see is the first filename in /tmp.  Possibly you
> have a filename in /tmp consisting of only whitespace or non-printable
> characters.  It would pop up first.  Or possibly you are using a
> version of ls that produces an empty line first.

No,  I will hazard a guess that he is using /bin/sh under HP-UX 9.x,
where /bin/sh is the Bourne sh.  /bin/sh at HP-UX 10 and above is the
posix shell,  and he would get at least the first filename.  Posix
shells execute the tail end of a pipeline (read a) in the current
shell and all other segments in subshells.  The Bourne shell executes
all segments in subshells (forked,  but not exec'd processes).  While
the forked segments have a copy of the parent's memory,  the parent
of course has no access to the child's,  so when the subshell for
"read a" exits,  the value stored in "a" is lost.

Dan Mercer

> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

>       Consulting                   wrong with a world in which Ken
>       Modeling                     Thompson lives in obscurity and
>       Training                     Bill Gates is a famous billionaire.
> //////////////////////////////////////////////////////////////////////

Opinions expressed herein are my own and may not represent those of my employer.
 
 
 

1. Reading a line from the standard input

I known none with a C++ interface.  With a C interface, there is GNU
readline (which provides command line editing, history,
completion,...)  and at least one public domain variant which provides
command line editing (I don't think it provides history).

This should be a terminal configuration problem.  Or the application
already plays with the terminal driver and I don't known how a
readline like library will interact with that.  Badly probably.

Crossposted to comp.unix.programmer, no follow-up set but one should
if continuing the discussion on the configuration of terminals under
Unix.

Yours,

--
Jean-Marc


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. .ram files in Mozilla

3. Hiding "read" commands standard input

4. pppd 2.3.9 won't compile

5. How do you read standard input or a file list

6. Help setting up PPP server

7. use php to read from standard input

8. libc.so.5 => not found

9. How to make passwd to read from standard input?

10. standard input instead of input file

11. LCSDNYR 2001 -> standards, standards, standards

12. standard input to rmdir?

13. DOSEMU: dosemu needs a standard input