Solved

check for a string in a file sh script with cygwin

Posted on 2014-03-20
7
466 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Fine Tune your automatic Updates for Ubuntu / Debian
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

830 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