Link to home
Start Free TrialLog in
Avatar of GeologyETH
GeologyETH

asked on

check for a string in a file sh script with cygwin

In OS X the statement in sh shell works fine:

string="$(cat ~/$mydir/$file_with_string 2>/dev/null)"
testit="$(grep -c "$string" ~/$myotherdir/file_to_check)"

if [ $testit -eq 0 ];then
  do
else
  doelse
fi

I need the same functionality for cygwin on W7. Thank you.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
Tintin

Do you have your variables/paths set correctly for cygwin environment?
Avatar of GeologyETH

ASKER

The difficult part is the statement "if [ $testit -eq 0 ];then". Basically, I need to check whether a string which is read from a file is also in another file.
I found now that "~/" used in a string as "$(grep -c "$string" ~/$myotherdir/file_to_check)" is interpreted as a folder with the name "~". I therefore added the path of "~" to the variables "mydir" and "myotherdir".
I checked the path variables in the script and they point to the correct files which content should be checked.
Additionally, the command, e.g. grep is not found, but it is found as "$basedir/grep" (but not without the " ") where "basedir" points to the bin folder in which grep is.
But it still doesn't work.

 I tried

"if ! "$basedir/grep" -qf  $mydir/$file_with_string $myotherdir/file_to_check ; then"

and get

/Bin grep.exe: error while loading shared libraries: cygpcre-1.dll cannot open shared object file: No such file or directory

"if [ ! "$basedir/grep" -qf  $mydir/$file_with_string $myotherdir/file_to_check ] ; then"

and get

: [: too many arguments
Coda:  no wonder "cygpcre-1.dll" is not in the bin folder. To test it, I copied "cygpcre-0.dll" to  "cygpcre-1.dll" and get now

C:/Program: No such file or directory
The root cause of your issue seems to be that CYGWIN is incompletely installed.

If you have the "cygcheck" tool at hand you could run:

cygcheck "$basedir/grep"

Any result?
As

if ! "$basedir/grep" -qf  "$mydir/$file_with_string" "$myotherdir/file_to_check" ; then

it works. Seems that cygwin needs the "" because of the spaces in the folder path.

Thanks a lot.
Sorry, didn't see your comment in time. Your are right, it's of course no solution as long as the library cygpcre-1.dll is missing. I'll open a new question for this.