This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: problem under cygwin with sh, bash, ksh


VÃclav Haisman schrieb:
Hubert Samm wrote, On 6.3.2009 14:26:
Hi All... I've searched and found nothing.... this script runs just fine on AIX, Solaris, and Linux, but under cygwin, the array VALUE prints only blanks.. has anyone else run into this... I've tried a bunch of different things around typecast, etc, but it looks like VALUE is local in the table_it function... any thoughts???
[...]
Have you tried debugging it using the -x switch to see what is going on?
Simple test case of setting VALUE[0]=$1 and then echoing "${VALUE[0]}" does
print what I give it, using bash.

--
VH

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



This is a problem with portable script programming between different shells. Some explanations:

Shell Versions used:
AIX uses as the default sh shell a ksh Version 88.
HP-UX uses as the default sh shell a ksh Version 88.
Solaris uses as the default sh shell a non posix compatible shell.
The ksh on Solaris is mostly a Version 88, opensolaris uses a ksh Version 93.
Linux uses as the default sh shell a bash.
Older Linux versions uses a pdksh as a replacement for the ksh shell.
Newer Linux versions uses a ksh Version 93.
Cygwin uses as the default sh shell a bash
Cygwin uses the a pdksh as a replacement fir the ksh shell.


Setting variables and sub shells.
If you set a variable in a sub shell, this variable usable in the calling shell. You can execute commands in a sub shell with the following syntax (list).


The different shell executed the commands in a pipe in a different manner. The shells uses internally sub shells to execute the different commands of a pipe line.


command1 | command2 | command3 is executed in this order in the different shells. bash : command1 | (command2) | (command3) pdksh : command1 | (command2) | (command3) ksh : (command1) | (command2) | command3

So only in a ksh it is possible to set a variable in the last command of a pipeline and uses this variable later in the shell script.

A short example script for bash, pdksh and ksh is attached.
It includes a in all 3 shell working solution at the end.

So the solution for your problem is to use a ksh version 93 on cygwin or rewrite your code more portable.
You can download a version of ksh version 93 from http://www.kornshell.com/


Regards
Dirk




















#!/bin/ksh
#!/bin/bash
#!/bin/pdksh

TESTVAR="VALUE befor setting a differend value in a sub shell"
echo "OUTSIDE of sub shell. Value of \${TESTVAR} before setting a differend value in a sub shell: ${TESTVAR}"
( 
  echo "INSIDE of sub shell. Value of \${TESTVAR} before setting a differend value in a sub shell: ${TESTVAR}"
  TESTVAR="NEW VALUE set inside a sub shell"
  echo "INSIDE of sub shell. Value of \${TESTVAR} after setting a differend value in a sub shell: ${TESTVAR}"
)
echo "OUTSIDE of sub shell. Value of \${TESTVAR} before setting a differend value in a sub shell: ${TESTVAR}"

echo
echo

TESTVAR="Value before READ in PIPE"
echo "Value of \${TESTVAR} before READING in a pipe: ${TESTVAR}"
echo "New value to READ in PIPE" | { read TESTVAR; echo "This is the value of \${TESTVAR} during execution of the read part of the pipe : ${TESTVAR}"; }
echo "Value of \${TESTVAR} after READING in a pipe: ${TESTVAR}"

echo
echo

TESTVAR="Value before portable while read loop"
echo "Value of \${TESTVAR} before READING portable in a while read loop: ${TESTVAR}"a
i=0
while read TESTVAR; do
    echo "Value of \${TESTVAR} after READING portable in a while read loop: ${TESTVAR}"
    i=$(( i + 1 ))
	READVALUE[${i}]=${TESTVAR}
done <<END
First line to read in the while read loop
Second line to read in the while read loop
END
echo "Value of \${TESTVAR} after the end of the while read loop: ${TESTVAR}"
echo "Value of \${READVALUE[1]} after the end of the while read loop: ${READVALUE[1]}"
echo "Value of \${READVALUE[2]} after the end of the while read loop: ${READVALUE[2]}"
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]