Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash loop skip commented lines

Tags:

bash

shell

I'm looping over lines in a file. I just need to skip lines that start with "#". How do I do that?

 #!/bin/sh 

 while read line; do
    if ["$line doesn't start with #"];then
     echo "line";
    fi
 done < /tmp/myfile

Thanks for any help!

like image 828
exvance Avatar asked Sep 19 '12 04:09

exvance


People also ask

How do I ignore a line in bash?

Using head to get the first lines of a stream, and tail to get the last lines in a stream is intuitive. But if you need to skip the first few lines of a stream, then you use tail “-n +k” syntax. And to skip the last lines of a stream head “-n -k” syntax.

What does$? mean in shell script?

$? exit status variable. The $? variable holds the exit status of a command, a function, or of the script itself. $$

What is\'in Linux?

That is, '\'' (i.e., single quote, backslash, single quote, single quote) acts like a single quote within a quoted string. Why? The first ' in '\'' ends the quoted string we started with ( ' Hatter ), the \' inserts a literal single quote, and the next ' starts another quoted string that ends with the word “party”.

How to loop a command in bash?

To demonstrate, add the following code to a Bash script: #!/bin/bash # Infinite for loop with break i=0 for (( ; ; )) do echo "Iteration: ${i}" (( i++ )) if [[ i -gt 10 ]] then break; fi done echo "Done!"


1 Answers

while read line; do
  case "$line" in \#*) continue ;; esac
  ...
done < /tmp/my/input

Frankly, however, it is often clearer to turn to grep:

grep -v '^#' < /tmp/myfile | { while read line; ...; done; }
like image 104
pilcrow Avatar answered Sep 28 '22 09:09

pilcrow