Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

check for a string in a file sh script with cygwin

Posted on 2014-03-20
7
Medium Priority
?
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
Suggested Courses

704 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