Skip to content

Commit

Permalink
fourth version
Browse files Browse the repository at this point in the history
  • Loading branch information
gilcot committed May 30, 2017
1 parent 8b6c263 commit 7689005
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This a collection of scripts that could be a Ghist each.
## `get_comments`

During my _Unix_ course, this is one of the exercice I ask my students in
in order to start playing with learned concepts and commands:
order to start playing with learned concepts and commands:
1. "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, or doesn't contain shebang. Try to use only
Expand All @@ -20,4 +20,6 @@ non-shell scripts too, and test with an unstrurtured BASIC file for example
Be aware that delimiter lenght may change... (`'[0-9]* REM '` vs `'# '` ...)
Finally, completely relax the check, so that it can accept something like
`#!/bin/env somename` and others."
4. "Finally, let user indicate, optionally, the beginning and the ending of
comments. So the script may extract bloc comments from PHP and others."

12 changes: 9 additions & 3 deletions get_comments
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ cat "$1" 1>/dev/null || exit 1
# check first line
head -n 1 "$1" | grep -q '^#!/' || { echo "script without shebang"; exit 2; }

# get length of regexp
l=$( grep -o "${2:-#}" "$1" | sort -gu | tail -n 1 )
# separator
echo "${2:-#}" | grep -qv '/' && _s='/' \
|| echo "$2" | grep -qv '#' && _s='#' \
|| echo "$2" | grep -qv '+' && _s='+' \
|| echo "$2" | grep -qv '%' && _s='%' \
|| echo "$2" | grep -qv '!' && _s='!' \
|| echo "$2" | grep -qv '~' && _s='~'

# extract comments
grep -o "${2:-#}.*$" "$1" | cut -c $(( ${#l} + 1 ))-
##grep -o "${2:-#}.*${3:-$}" "$1" | sed "s${_s}^${2:-#}\\|${3:- }\$${_s}${_s}g"
grep -o "${2:-#}.*${3:-$}" "$1" | sed "s$_s^${2:-#}$_s$_s" | sed "s$_s${3:- }\$$_s$_s"

24 changes: 24 additions & 0 deletions test2_php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/php
<? /* PHP script test
------------------------- */

/*
1. Ask for name
*/
echo "Who are you? ";
$handle = fopen('php://stdin','r');

/*
2. Warm salutions
*/
echo "\nHello ". fgets($handle)) . "\n" ; /* 2.1. say hello */
fclose($handle);
echo "\nI am " . exec('uname -n') . "\n" ; /* 2.2 introduce yourself */
$today = getdate();
echo "\nToday is $today[mon]/$today[mday]/$today[year]\n" ; /* 2.3 a memorable day */

/*
3 So shy
*/
sleep(15); /* 3.1 nothing to say... */
echo "\nSee you\n" ; /* 3.2 say goodbye */

0 comments on commit 7689005

Please sign in to comment.