Problem with character by character reading

Hi friend,
i have the following problem:
when i am writting the below command on the command prompt , its working.

while read -n 1 ch; do echo "$ch" ; echo "$ch" ; done<file_name.out.

but when i am executing it after saving it in a ksh file, its not working.
Please helppppppppp .. thankss in advance

What is not working? What output do you get? What is your input?

read -n 1 is a bash-ism. ksh88 does not support it.

Care to tell us what 'does not work' means?

@ scrutinizer : i am getting the error as dirty read . input is a simple text file.
@jim mcnamara : i am getting error as dirty read . can you please tel me the korn shell equivalent of the above command?

Thanks a lot guys in advance

What is the exact error?

the exact error is : read: bad option(s)
can you please tel me the korn shell equivalent of the above command?

thanks in advance :slight_smile:

This can be used in a recent ksh93, but not in older version, nor in ksh88. What OS and version are you using and what is your ksh version? It would appear to be ksh88.

SunOS spdma544 5.10 Generic_142909-17 sun4u sparc SUNW,SPARC-Enterprise

how to get the ksh version??

kah --version and echo $KSH_VERSION
are not working .. please help

you can check with fold command

 
fold -w1 input.txt
1 Like

That is ksh88. That is going to be a bit complicated with dd and terminal settings and such. Does it need to be ksh, or could you use bash?
*edit* never mind ... input is a text file..

try awk with -F"", it should give you every single character approachable.

Try something like:

while read line
do 
  while [ -n "$line" ]
  do
    newln=${line#?}
    ch=${line%$newln}
    line=$newln
    echo "$ch"
  done
done < infile

or feed itkamaraj's command into a while loop...

1 Like

thanks

here what does -n 1 exactly do, mean to ask what will be the difference in o/p while not using it.

Thanks
Aditya

@aditya i guess then it will start reading line by line . here (i.e in this case) it is reading character by character. :slight_smile:

Two ways:

  1. display the content of the (system)-variable "${.sh.version}". This is from my system:
# print ${.sh.version}
Version JM 93t+ 2010-03-05
  1. switch to "vi"-command line editing mode, then press <ESC> and then <CTRL>-<V>, which does basically the same as in method 1. The second line is after pressing the mentioned keys.
# set -o vi
# Version JM 93t+ 2010-03-05

I hope this helps.

bakunin

2 Likes

@bakunin. Thanks. The second method also works in ksh88.

set -o vi
Version M-11/16/88i
..
is it ksh88 ?? :frowning:

Yes..

1 Like