problem with read command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with read command
# 1  
Old 08-06-2009
problem with read command

Hi Guys,

I am just trying to read data from a file.
The command i tried worked well with AIX server. But in SunOS, it is not reading.

Code:
cat log.out |sed -n '4,4p'| read value;
+ sed -n 4,4p
+ cat log.out
+ read value
echo $value;
+ echo

This is the log of running the script.

Can you people help me in figuring it out.. and also i want to know how the same command which worked on AIX is not working in SunOS.

Thanks & Regards,
Magesh
# 2  
Old 08-06-2009
I guess you are just going to get the 4th line.
The cat is not needed since sed can read files on it's own.

In bash you can do this
Code:
read VAR < <(sed '4!d' infile)
echo $VAR
test-9876

This should work in most shells and would also need no read:
Code:
VAR=`sed '4!d' infile`
echo $VAR

# 3  
Old 08-06-2009
zaxxon do we need to have two "<" in the code?
# 4  
Old 08-06-2009
We have Posix-standard and then we have real world Smilie - people who has done the shells.

Only in ksh you can use pipe:
Code:
cmd | read var1 var2 ...


Previous zaxxon example include howto read generic one variable:
Code:
var=$( cmd )

Previous zaxxon example include howto read more than one variable using bash.
Code:
read var1 var2 <  <(cmd)

Generic version for more than one variable read:
Code:
read var1 var2 <<EOF
$(cmd)
EOF


Last edited by kshji; 08-06-2009 at 04:33 AM..
# 5  
Old 08-06-2009
KShJi, i am using a ksh only..

But still why i am not able to pass the value to the read command ?

Can you please explain me?

Thanks in advance
# 6  
Old 08-06-2009
Code:
cat log.out | sed -n '4,4p'| read value
echo "$value"

Works fine with ksh88 and ksh93. Just tested - echo something, if log.out has more than 3 lines.
# 7  
Old 08-06-2009
this is the content of my file
Code:
tcmsd003:/usr/local/dsadm/dsprod/src>cat log.out

  COUNT(*)
----------
         1

Actually from the unix prompt if i run the same sed command, i am getting the desired output, in this case 1. But i have problem in the script only

---------- Post updated at 01:24 PM ---------- Previous update was at 01:23 PM ----------

This is the output of my sed command.
Code:
tcmsd003:/usr/local/dsadm/dsprod/src>sed -n '4,4p' log.out
         1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read -p problem

Hi, :) I tried to make of "line returns" with "\n" : read -p $'Do you really want append this line in '$CONFIG_FILE''\n'IP CLIENT: '$IP_INPUT' - PATH: '$PATH_INPUT''\n'y/n ?:' CONFIRM_INPUT But it didn't work, please anyone have an idea? :b: (2 Replies)
Discussion started by: Arnaudh78
2 Replies

2. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

3. Shell Programming and Scripting

Problem with read line

Hi everybody, I am new to shell script. Please help me with this problem. I have a file test.txt with the content like this: I have a shell script test.sh like this #!/bin/sh while read line do echo $line >> out.txt done < test.txt out.txt is expected to have the same content... (10 Replies)
Discussion started by: Dark2Bright
10 Replies

4. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

5. Shell Programming and Scripting

Problem to read archive

Dear all, I have this archive: cat file.txt archive test 02 sequence 03 02length 52 archive test 02 sequence 04 02length 52 archive test 02 sequence 05 02length 52 teste arquivo 06 sequencia 08 06 length 54 teste arquivo 06 sequencia 09 ... (8 Replies)
Discussion started by: cewa67
8 Replies

6. Shell Programming and Scripting

While Read problem

Hello, I have this simple script while read name do ssh $name "hostname > /tmp/$name.txt" ssh $name "/opt/ignite/bin/print_manifest | grep Main >> /tmp/$name.txt" ssh $name "getconf MACHINE_SERIAL >> /tmp/$name.txt" ssh $name "ioscan -kfnC processor | grep processor | wc -l >>... (3 Replies)
Discussion started by: Andyp2704
3 Replies

7. Programming

Problem in read() from a pipe

Hi, Can any one please help me with this. Am struggling hard to get a solution. I am doing telnet through a C program and getting the stdout file descriptor of the remote machine to pipe. read() function is getting data, But whenl it receives SOH character ie. ^A ( Start of heading = Console... (2 Replies)
Discussion started by: JDS
2 Replies

8. Programming

Problem with read & write

Hello mates: I met problem with using read() & write(). I m trying to use read twice on client first time is the size of buffer, 2nd time is the buffer. I think I have to, coz I dnot know file size. So, I write twice on server as well -- 1st, filesize; 2nd, buffer. The problem is, sometimes,... (11 Replies)
Discussion started by: EltonSky
11 Replies

9. Shell Programming and Scripting

Problem with read in sh

I've discovered a very annoying problem in sh: echo -en "one\ntwo\nthree" | while read VALUE do echo "${VALUE}" done This will print one and two, but not three. The last line is IGNORED because it lacks a newline. This makes it hard to use sh for things like CGI scripting; you have... (6 Replies)
Discussion started by: Corona688
6 Replies

10. Shell Programming and Scripting

SH script problem with read

Hi, I'm doing a script that reads lines from a file and then copy them to another file, if the user wants it. But I'having problems to get the user selection because when I do the read to ask the user, the script reads the next line of the file. The script looks like this: #!/bin/sh # ... (2 Replies)
Discussion started by: pmpx
2 Replies
Login or Register to Ask a Question