0

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/ :)

2
  • 4
    Fix your shebang. Commented May 15, 2019 at 18:10
  • 1
    A good first troubleshooting step is to run ShellCheck on a bad script. In this case it automatically flags both the bad shebang and the fact that the case patterns will never match Commented May 15, 2019 at 18:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the first fix, but not sufficient because the if statement surrounding the case block is not properly closed by a corresponding fi either.
How is it not properly closed? fi for it is before the done for the for loop. Spacing in the code is a bit funky though.
The indentation is wrong starting with the esac line, so it looks wrong, but the various close keywords actually match up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.