Levent Kaya
Levent Kaya

Reputation: 47

Passing 'If statement' problem in Shell Scripting

I'm developing simple terminal based application that helps to compile your C/C++ and Python source files in a one command. But when I execute the function like 'erun test.py' It's always gives the output: ERun: file unknown file extension.

For my opinion the problem at the if statement. I try to edit these statements but nothing is changed. Here is my source code:

#/bin/bash
# function ERun for C/C++ and python
# version 1.0

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

function erun {
    if [ -z "$1" ]; then
        #display usage if no paramters given
        echo "Usage: erun file.c/.cpp/.py"
        echo "Run: ./file"
    else
        for n in "$@"
        do
            if [ -f "$n" ] ; then
                case "$n{n%,}" in
                    *.py)
                    chmod +x "$n"       ;;
                    *.c|*.cpp)
                    gcc "$n" -o "$n"    ;;
                    *)
                    echo "ERun: '$n' unknown file extension"

                    return 1
                    ;;        
            esac
        else
            echo "'$n' - file does not exist."
            return 1
        fi
    done
 fi
}

IFS=$SAVEIFS

My expected output is getting a executable file. I'll be happy if you can help me. By the way if you want to contribute my this tiny project here is the project link: https://github.com/lvntky/ERun/ :)

Upvotes: 0

Views: 44

Answers (1)

choroba
choroba

Reputation: 241868

This is weird

"$n{n%,}"

For ab/program.py, it returns ab/program.py{%n,}.

You probably wanted something like

"${n,,}"

instead which turns all the uppercase letters to lowercase.

Upvotes: 1

Related Questions