Counting the lines that begin with #.
grep -c '^#' script.sh
Just using grep '#' will also match these lines. That line is not a comment is it?
Code:
[root@beastlinux ~]# grep '#' script
echo "product #5"
[root@beastlinux ~]# grep '^#' script
[root@beastlinux ~]#
I have the script I helped another user with on the forum.
Code:
[root@beastlinux ~]# cat recycle
#!/bin/bash
recycleBin=/tmp/recycle_bin
function recycle_files {
mv -v "$1" $recycleBin
}
case "$1" in
-r) recycle_files $2 ;;
*) echo "-r wasn't used" ;;
esac
[root@beastlinux ~]#
This greps only the code. Any lines that are blank, contain only white space, or begin with a comment are removed from the results leaving only the lines of code.
Code:
[root@beastlinux ~]# egrep -v '^$|^#|^[ ]$' recycle
recycleBin=/tmp/recycle_bin
function recycle_files {
mv -v "$1" $recycleBin
}
case "$1" in
-r) recycle_files $2 ;;
*) echo "-r wasn't used" ;;
esac
[root@beastlinux ~]#
The [ ] contains a space and a tab character. To insert the tab, push control + v, then tab.
So if you want to count those different things, just add -c to grep.