diff --git a/README.md b/README.md index 9b7be86..8479a33 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ This a collection of scripts that could be a Ghist each. During my _Unix_ course, this is one of the exercice I ask my students in in order to start playing with `grep`: -"Using only grep, write a script that will extract from a given shell script, -after checking the file argument, all comments that start at firsh column." +"Write a script that will extract comment from a shell script. But make it +fail when the script is called with no argument, and also when the script +file cannot be found or read. Try to use only grep and keep your script the +smallest possible." diff --git a/get_comments b/get_comments index 16736be..02a1f11 100755 --- a/get_comments +++ b/get_comments @@ -3,6 +3,9 @@ # readable file requiered grep -q "" "$1" || exit 1 +# check first line +head -n 1 "$1" | grep -q '^#!/s\?bin/[a-z]*sh$' || exit 2 + # extract comments -grep '^#' "$1" | cut -c 2- +grep -o '#.*$' "$1" | cut -c 2-