hi,a question .. why does my script work as sh script.sh and not as ./script.sh ??

also,i was trying to write a script that would filter out a pattern in a .txt file and display the count of the same. Say my pattern is king, and there are 4 "king"s in the txt file .. also there are two "taking"s .. shouldnt the count be 6? m getting 4.
command -- grep -i "king" | wc -l

looking for healthy replies ..

~cheers

Add #!/bin/sh to the top of the script and make sure it's executable. If you are not sure about the path of sh then run which sh.

shouldnt the count be 6?

yes, it works fine for me. Maybe there are special characters?

hi,

why does my script work as sh script.sh and not as ./script.sh ?

has your script been made executable ?

my pattern is king, and there are 4 "king"s in the txt file .. also there are two "taking"s .. shouldnt the count be 6? m getting 4

please show input file. there are probably two or more on one or more lines.

Show your whole script too.

hi fellas .. yeah sh thingy works !!

inp file ..
""
king king
king
king
taking
taking
""

script :

while read line
do
grep -wi "king" | wc -l
done<abc.txt

grep -i "king" abc.txt | wc -l returns 5 (lines) -w returns 6 (words)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.