2

When I run this on the command line it works:

ls | grep -v "#$"

But when I do ls | scriptname and inside the script I have:

#fileformat=unix
#!/bin/bash
grep -iv '#$'

It's not working. Why?

[EDIT]

the reason for the first line is explained here.

besides that even if i remove the first two lines it SHOULD work. i tried the exact same on a remote Solaris account and it did work. so is it my Fedora installation?

1
  • yes agreed, removing first 2 lines should still work, because your script is being interpreted by the default shell, almost certainly bash. BUT the #fileformat=unix in your file is, strictly a comment. Note that the link you include, they are refering to editing a file with the vi editor AND that the exact usage is : fileformat=unix (note the ':' char). Text like :var=value is vi speak for 'set a vi option', in this case, fileformat=unix means 'use only \n (LF) char at end of line NOT the DOS version of \r\n (CR,LF). Other posters are correct, you don't want #f.. at the top! Commented Apr 28, 2011 at 18:36

3 Answers 3

2

The hash-bang line needs to be the first line in the script. Get rid of the #fileformat=unix. Also make sure your script is executable (chmod +x scriptname). This works:

#!/bin/bash
grep -iv '#$'
Sign up to request clarification or add additional context in comments.

1 Comment

i don't know what happened but it's working now...thanks for your help
0

Change it to ls < scriptname so that the output is passed to ls.

3 Comments

I didn't downvote you, but I thought about it. ;-) ... You should test your solutions before posting. Keep on posting!
@shellter - I did test it on my Mac before posting and I got the same output as running ls | grep -v "#$", maybe I misunderstood the question? I do think your solution is much more flexible being able to pass in the search targets
@shellter - Ok, I take it back ;) I just looked back at where I was testing and I was totally off, my bad!
0

1st off you need #! /bin/bash as the first line in your script.

Then '#$' has no meaning in the shell pantheon of parameters. Are you searching for a '#' at the end of the line? (That is OK). But if you meant '$#' but then $# is the parameter that means the 'number of arguments on the command-line'

Generally, piping a list of files to a script to acted on would have to be accomplished with further wrapper. So a bare-bones, general solution to the problem you pose might be :

$cat scriptname
#!/bin/bash

while read fileTargs ; do
   grep -iv "${@}" ${fileTargs}   # (search targets).
done

called as

ls | scriptname srchTargets

I hope this helps.

1 Comment

"Are you searching for a '#' at the end of the line? " Yes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.