2
Please see the following function —
function touchm(){
date=$(date +'%Y-%m-%d %H:%M:%S')
read Y M D h m s <<< ${date//[-: ]/ }
file_name=$1
if [[ $file_name =~ ^[0-9a-zA-Z+_-]+.[py|pl|rb|sh] ]]; then
echo "#
# $file_name
#
# Created by $MASTER on $M/$D/$Y at $h:$m:$s
# Copyright (c) $Y $MASTER. All rights reserved.
#
" > $1
elif [[ $file_name =~ ^[0-9a-zA-Z+_-]+.[m|h|c|cpp|] ]]; then
echo "//
// $file_name
//
// Created by $MASTER on $M/$D/$Y at $h:$m:$s
// Copyright (c) $Y $MASTER. All rights reserved.
//
" > $1
elif [[ $file_name =~ ^[0-9a-zA-Z+_-]+.[ml] ]]; then
echo "(* *)
(* $file_name *)
(* *)
(* Created by $MASTER on $M/$D/$Y at $h:$m:$s *)
(* Copyright (c) $Y $MASTER. All rights reserved. *)
(* *)
" > $1
else
echo "What bullshit. Give me a good file name"
fi
}
so whenever I execute:
touchm 2-r_f.ml
this should generate an ml file with ml type comments. But it doesn't do that. It generates c type comments.
Moreover, I need to accommodate for + and _ (underscore) as well but currently it doesn't seem to work. Is that a bad regex? If so, can I get pointers to correct it?
Thank you
I've update the code with the current regex. This doesn't seem to work. It only generates pl/py based code even for c or cpp or ml. There appears to be an issue there. – p0lAris – 2013-02-12T23:41:01.690
Sorry, I'm not too sure what your script does/is trying to do; just that you can use a here doc with cat to send multi line comments in to a file, and that I've done more regex than I care to remember :) Also worth noting: [m|h|c|cpp|] should be (m|h|c|cpp) or possibly (?:m|h|c|cpp) See my answer for fixed code. – Justin – 2013-02-13T01:46:19.953
Oh this works brilliantly. The only issue was the way I check for cpp or c or m type files. This is great. Thanks very much Justin. – p0lAris – 2013-02-13T02:33:24.190
No problem. Regex is a tricky beast, but I had a part time job while I studied at university that required writing multi-paragraph regexes for various purposes. :) – Justin – 2013-02-13T02:56:50.600