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!
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.
$? exit status variable. The $? variable holds the exit status of a command, a function, or of the script itself. $$
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”.
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!"
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With