ksh vs perl strange interaction.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh vs perl strange interaction.
# 1  
Old 01-09-2011
ksh vs perl strange interaction.

Hello everybody,

I isolated an issue: please try the following script (under AIX)

Code:
#!/usr/bin/ksh93


echo "Before Perl: \c"
read line
echo "|$line|"

perl -e 'print "Perl invocation";'
echo

echo "After Perl: \c"
read line
echo "|$line|"

On the first input, type Ctrl-D. It works
On the second input, type Ctrl-D. You'll need to type it twice.

Now, change the script to use ksh instead of ksh93. It works fine.

Smilie

What's wrong? Any workaround?

Jacques.

Last edited by jlovi; 01-09-2011 at 10:58 PM..
# 2  
Old 01-09-2011
Does'nt appear to be related to perl it does it to me without any command in-between:

Code:
#!/usr/bin/ksh93
 
echo "1st: "
read line
echo "|$line|"
 
echo "2nd: "
read line
echo "|$line|"

##Update##, seems to be ok if you use /bin/read instead of the ksh93 internal read function.
# 3  
Old 01-09-2011
You're right (quite late here to have a completely clear mind Smilie).

This has the problem too:

Code:
#!/usr/bin/ksh93


echo "a"| read a

echo "After Perl: \c"
read line
echo "|$line|"

---------- Post updated at 04:43 AM ---------- Previous update was at 04:13 AM ----------

Quote:
Originally Posted by Chubler_XL
##Update##, seems to be ok if you use /bin/read instead of the ksh93 internal read function.
Thanks for this idea... it could have worked. Unfortunately, in AIX, here the /usr/sbin/read content:

Code:
#
# This shell script gets installed in /usr/bin to provide an exec()-able
# version of certain shell builtins:
#       alias           command         getopts         umask
#       bg              fc              jobs            unalias
#       cd              fg              read            wait
#       hash            type
#
# These are also builtins, but already have historical version in /usr/bin:
#       false           kill            newgrp          true
#
command=`/usr/bin/basename $0`          # Invoke the ksh builtin
if [ "$command" = "type" ]
then
        whence -v "$@"
elif [ "$command" = "hash" ]
then
        alias -t - "$@"
else
        $command "$@"
fi

Smilie
# 4  
Old 01-09-2011
Sounds like you need a workaround, this will work for reading 1 variable if that's all you need.

Code:
#!/usr/bin/ksh93

line=$(head -1)
echo "|$line|"
line=$(head -1)
echo "|$line|"

# 5  
Old 01-09-2011
Thanks for your great ideas!

Unfortunately (I think this is the last trick), I need to emulate "read -r line" Smilie



### UPDATE ###

This seems to work. I'll try it in my program.

Code:
#!/usr/bin/ksh93


echo "a"| read a

echo "1: \c"
line=$(head -1|(read -r line;echo "$line"))
echo "|$line|"
echo "2: \c"
line=$(head -1|(read -r line;echo "$line"))
echo "|$line|"


Last edited by jlovi; 01-10-2011 at 04:25 AM..
# 6  
Old 01-10-2011
How about this:

Code:
line=$(echo $(head -1) )

# 7  
Old 01-10-2011
Well, well, I was wrong telling that this was the last trick I needed.

In fact, I need to capture Ctrl-D and Ctrl-C, differenciating them. If I type something like "\n$1", I should exactly get these characters.

Got something like this:

Code:
#!/usr/bin/ksh93
line=$(trap 'line=""' INT;head -1|(set -f;read -r line;print -r "$?:$line") 2> /dev/null)
[ "X$line" != "X" ] && read_res=$(print -r "$line"|cut -f1 -d:)
line=$(print -r "$line"|cut -f2- -d:)

Tested successfully on Linux (Fedora) and AIX

Works in ksh. Core dump in ksh93! Smilie

SmilieSmilieSmilie

Last edited by jlovi; 01-10-2011 at 02:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Perl] Strange problem with array

Hi, I have a strange problem with arrays in Perl. That is to say, for me it is strange and perhaps there is a special reason for it that I do not know of. Not a real Perl Ace. This is the program, as an example: #!/usr/bin/perl -w #-d use strict; my $pu; my $pu_list_cmd; my... (2 Replies)
Discussion started by: ejdv
2 Replies

2. Shell Programming and Scripting

Strange behaviour with perl i/o?

Hi All, I got a strange problem here. I have a perl script which is fetching data from a database table and writing a file with that data. If i run that script from linux command line, the file it creates is a normal ascii text file without any binary character in it.But... (9 Replies)
Discussion started by: DILEEP410
9 Replies

3. Shell Programming and Scripting

strange ksh arithmetic

Hello, I would like to understand this... I'm using ksh and doing (( z = y - 1 )) if y=34, then the result for z is 33, but if y=034 the result is z=27. Why?? Thanks (15 Replies)
Discussion started by: fbg
15 Replies

4. UNIX for Advanced & Expert Users

Strange KSH behaviour - any comments?

As you are probably aware, $# indicates the number of parameters passed into a korn shell script. But this appears to hang around for sunsequent runs...???? A simple script:- #!/usr/bin/ksh echo "#parameters $#" echo "\$1 $1" echo "\$2 $2" I run the script with 0 parameters (all fine) #... (7 Replies)
Discussion started by: gsw_aix
7 Replies

5. UNIX for Dummies Questions & Answers

Longer commands and strange behaviour on ksh

Hi, I was trying to customize this archaic HP-UX box. only shell available is ksh and that too seems to be pretty old and doesn't completely conform to what I read on the web about ksh. Anyway here are my issues: - I wanted to have a dynamic title on xterm or dtterm. I put the following lines... (2 Replies)
Discussion started by: anurags
2 Replies

6. Shell Programming and Scripting

Strange parameter passing problems (KSH)

Hi all, I'm having a rather peculiar problem involving parameter passing with declared functions in my shell script. Hope to get some advice here. A brief description of my code is as follows: However, I'm not getting the results I wanted. If I pass in $rdir, I'm going to end up... (4 Replies)
Discussion started by: rockysfr
4 Replies

7. Shell Programming and Scripting

ksh and awk interaction

in a ksh script, i want to process some string variables using awk, and then i want to go on using this variables in the same ksh (out of awk lines) can anybody send me a very simple example about this? (0 Replies)
Discussion started by: gfhgfnhhn
0 Replies

8. Shell Programming and Scripting

Strange behavior from 'read' statements. (ksh - but could be same on other shells)

I'm getting rather frustrated with an interactive script I'm writing. The script is divided up, with section for setting variable at the top, then functions (which make up most of the script) then basically a line at the end which calls the first function- the program moves between the... (5 Replies)
Discussion started by: alexop
5 Replies

9. Shell Programming and Scripting

bash and Perl interaction questions

hi. i´m working in bash and am trying to create a Perl daemon that controls bash´s behavior. this is actually in preparation for a later project i´ll be working on. basically, i´m looking for a way to have the Perl daemon tell bash what to do. i already have a small daemon that simply prints... (2 Replies)
Discussion started by: deryk
2 Replies
Login or Register to Ask a Question