Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can try using cat to do this instead, i.e.

cat << EOF > output-filename
# $file_name
#
# Created by $MASTER on $M/$D/$Y at $h:$m:$s
# Copyright (c) $Y $MASTER. All rights reserved.
#
EOF

If you want to indent the code above, change << for <<- which will remove leading tabs.

Also, yes, it is bad regex. character classes (square brackets) don't need pipes, that is for having alternative matches inside of round brackets. you want something like:
Replace: $file_name =~ ^[0-9|a-z|A-Z|-]*.[ml]
With: $file_name =~ ^[0-9a-zA-Z+_-]*.[ml]

You could also replace the * with a + to indicate 1 or more, as that regex would also match '.ml'. Make sure a hyphen is the last character in a character class, otherwise it will assume you are specifying a range.

EDIT: try this, just tested with a .sh, .cpp and .ml file. Out of curiosity, is the 3rd if statement supposed to be for .ml, or .m OR .l as you seemed to be matching? the second if statement also matched .m, so one of them is probably wrong, below code assumes the 3rd if should be .ml

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
}
share|improve this answer
    
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. –  flippex17 Feb 12 '13 at 23:41
    
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 Feb 13 '13 at 1:46
    
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. –  flippex17 Feb 13 '13 at 2:33
    
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 Feb 13 '13 at 2:56
    
That's pretty cool! Thanks a lot Justin. :) –  flippex17 Feb 13 '13 at 3:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.