Visit Our UNIX and Linux User Community


problem with read command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with read command
# 8  
Old 08-06-2009
Are you sure that your script has done using ksh ? What is your SHELL variable value ?
1st line:
Code:
#!/bin/ksh

and execute flag is on ?

or run it using ksh
Code:
ksh script



---------- Post updated at 11:17 AM ---------- Previous update was at 11:14 AM ----------

Script without firstline #!programpath and execute flag is on and you give scriptname, then
script has done using variable SHELL value. Always programpath with correct options in the first line in scriptfile. Awk, sh, sed, perl, python, ...

Safety testing always:
interpreter scriptfile
# 9  
Old 08-06-2009
As said, the < is just when using bash in this example.
Also as said, leave out the useless cat. The most tools can read files on their own and don't need it to be cat'ed & piped.

Use for ksh:
Code:
VAR=`awk 'NR == 4 {print $1}' infile`
echo ${VAR}

I changed to awk so you get rid of leading spaces this way.
Try it in your script and tell if it does not work.
# 10  
Old 08-06-2009
Works fine on my AIX box with ksh.
Code:
$ cat read.ksh
#!/usr/bin/ksh

sed -n '4{;p;q;}' log.out | read value
echo "Value is '$value'"

$ cat log.out

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

$ read.ksh
Value is '1'
$

If the value that you want to read is the last one in the file, you can replace your sed command by this awk command :
Code:
awk 'NF {v=$1} END {print v}' log.out

Jean-Pierre.
# 11  
Old 08-06-2009
Thanks for your replies guys

@kshji, i have the first line as
Code:
#!/usr/bin/ksh

Earlier i was running it as
Code:
sh -vx script_name

.

At that time, it was not able to read the value. But after i used ksh, i was able to read the value.
But still it throws an error. the error is
Code:
test1.ksh[20]: 4,4p: is not an identifier

@Jean-Pierrre, the script runs fine in AIX. But it is throwing a problem in SunOS. I have two servers.

---------- Post updated at 02:45 PM ---------- Previous update was at 02:39 PM ----------

Actually i was using << before read command, then i replaced it with <. But now it throws an another error. Please see the log below.

Code:
read value < sed -n '4,4p' log.out;
+ read value -n 4,4p log.out
+ test1.ksh[20]: sed: cannot open

Please let me know where i am going wrong..
# 12  
Old 08-06-2009
If you like to test script with ksh then call
Code:
ksh script

not ex. sh.

This works with ksh, bash, zsh and all other posix-sh
Code:
value=$(sed -n "4,4p" log.out)
echo "$value"
EOF

# 13  
Old 08-06-2009
cool that worked like charm.. i think i was just trying to understand why we are not able to use "|" in the SunOS. Anyways thanks for your help.. Thanks all
# 14  
Old 08-06-2009
SunOS include same command many times, different version (many other *nix has same status).
There is own old binary = compatible, xpg4 spec version, u95 version, gnu, posix, ...

I think, if you try find, you'll find more than one ksh.
Code:
find / -name ksh
find / -name sed

Try | using with all those ksh.

---------- Post updated at 04:41 PM ---------- Previous update was at 04:39 PM ----------

If you like to use latest ksh, then
download from ATT

Previous Thread | Next Thread
Test Your Knowledge in Computers #295
Difficulty: Easy
Ubuntu releases updated versions predictably every 3 months.
True or False?

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

Featured Tech Videos