0

I am trying to make a shortcut code with gcc. The purpose is to compile and then automatically execute the compiled file.The code I am using looks like this,

gcc [email protected] -o $@ ./$@ 

But it won't execute as purposed. It is saying something like Segmentation Fault. How should I fix this?

2
  • 1
    Why are you using $@ instead of $1? The way you've written the script, it won't work with multiple arguments. Commented Mar 16, 2017 at 21:14
  • 1
    @Barmar $@ could make sense in the context of a Makefile. Still, not enough information given here to figure out what's going on... Commented Mar 17, 2017 at 6:40

1 Answer 1

1

You probly want

#!/bin/sh -e if ! test $# -gt 0 -a -f "$1"; then echo >&2 "Usage: $(basename $0) FILE [ARG]..." exit 1 fi F="$1" shift X=$(echo "$F" | sed 's/\.[^.]\+//') gcc "$F" -o "$X" "$@" ./"$X" 
Sign up to request clarification or add additional context in comments.

2 Comments

Put $@ in double quotes
@Barmar Done. I didn't bother in the first place, given that original code had much more serious problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.