Solved

check for a string in a file sh script with cygwin

Posted on 2014-03-20
7
461 Views
Last Modified: 2014-03-21
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.
0
Comment
Question by:GeologyETH
  • 4
  • 2
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39942939
CYGWIN has bash, so your script should run there.

The if..then part needs some modification, but that's not due to bash, it is wrong anyway. Or are "do" and "doelse" just placeholders?

...
if [ $testit -eq 0 ]; then
    echo "String not found"
    #  run commands
else
    echo " String found"
    # run other commands
fi

We could make it a bit more straightforward, though:

if ! grep -qf ~/$mydir/$file_with_string ~/$myotherdir/file_to_check ; then
    echo "String not found"
    # run commands
else
    echo " String found"
    # run other commands
fi
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39944088
Do you have your variables/paths set correctly for cygwin environment?
0
 

Author Comment

by:GeologyETH
ID: 39944779
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
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:GeologyETH
ID: 39944799
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
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39944816
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?
0
 

Author Comment

by:GeologyETH
ID: 39944821
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.
0
 

Author Comment

by:GeologyETH
ID: 39944828
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.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question