Solved

check for a string in a file sh script with cygwin

Posted on 2014-03-20
7
450 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now