-1

I want to start developing in c#, however I use Linux and VSCode for development and VSCode doesn't run the code due to some error that I can't find an answer to, so I decided with the help of this link (Run C# code on linux terminal) to make a bash script that does this part of the code:

mcs -out:hello.exe hello.cs
mono hello.exe

I made it like this so far:

#Trial
function cs_compiler(){
mcs -out:$2 $1
mono $2
}

It does work however I want to make it so that $2 (the second parameter) is automatically named after the $1(the first parameter), thus I needn't write $2's value and just write $1 create an exe with the same name and run that exe with mono. The second thing I want is to check if the exe form of the cs file already exists, if so just run it (mono) without doing mcs-out.

I'm sorry if this is of inconvenience, I'm new to stackoverflow and coding.

Apoqlite
  • 239
  • 2
  • 21
  • why not `function ... mcs -out:$1.exe $1.cs ; mono $1.exe...` ? Also for *I want is to check if the exe form...exists*, `if [ ! -f $1.exe ] ; then msc .... else $1.exe ; fi` . If you get syntax errors on your new code, use https://shellheck.net to point you in the right direction to fix them. The error messages are a little obscure for a new user, but think about where in the code the error is flagged and the error message provided and take your best guess to try and fix it. Good luck.... – shellter Nov 11 '18 at 15:22
  • And I agree with @dlatikay, testing for an existing file and running it will likely mean you keep running old, broken code. The `make` program is designed for these situations, but you'll need more help than we can provide on StackOverflow to get up to speed (probably). See if you can find someone local that can help you with your "Hello World" make file. Good luck. – shellter Nov 11 '18 at 15:28

1 Answers1

0

From man basename:

Strip directory and suffix from filenames

Sadly you have to know suffix in advance to use it. So:

cs_compiler() {
    local infile outfile
    infile="$1"
    outfile="$(dirname "$1")/$(basename "$1" .cs)"
    mcs -out:"$outfile" "$infile"
    mono "$oufile"
}
  1. Quote your variables.
  2. Indent your code

But maybe better would be to remove the part after dot:

infilename=$(basename -- "$1") # some temp variable
outfile="$(dirname "$infile")/${infilename##*.}"

See this post on how to extract filename from files.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111