5

I already read How can I create a custom terminal command (to run a script)? . But this always executes the same command.

What I want is, for example, instead of having to type

gcc -m32 -g -zexecstack -fno-stack-protector -mpreferred-stack-boundary=2 -no-pie -fno-pic -o program program.c

I'll just have to type a custom command like

custom-gcc program

or at least

custom-gcc -o program program.c

It would also be good if I could add options to the custom command that would also be added to gcc.

How can I do this? Thanks

2
  • 1
    Your link is broken. – Asteroids With Wings Nov 18 '20 at 2:21
  • 3
    My whole life is broken. Thanks for the notice anyway, I fixed it now. – Chris Rahmé Nov 18 '20 at 11:05
5

General answer:
Open your web browser on www.tldp.org and look for the "Bash guides"

Simple answer:
Open a terminal (Shell) and type (in the process you will see text not shown here)

cd
mkdir bin
echo >>.bash_aliases 'PATH=$PATH:$HOME/bin'
cat <<EOF >bin/custom-gcc
#!/bin/bash
gcc -m32 -g -zexecstack -fno-stack-protector \
    -mpreferred-stack-boundary=2 -no-pie -fno-pic \
    -o "$1" "$1.c"
EOF
chmod 755 bin/custom-gcc
exit

Now

  • open an new Terminal (shell),
  • cd into a folder where you have e.g. "program.c" and type
  • custom-gcc program

... this should from now on execute your custom-gcc with the effect you wish.

NOTE: The bash guides behind the link above will provide all the information you need to improve the simple script created by the above instructions.

Explanation:
cd ensures you're in $HOME/

The next two lines creates a bin/ subfolder in your home directory and makes it be a place to look into, to find "commands".

The lines from cat to EOF is a simple way of creating a text file, here the text file content will be a bash script that does what you request (hint: read the Bash guides to understand the content).

The chmod sets "mode-flags" on the just created file, such that it will be consider "executable".

The very last line exits the shell.

As you from now on open a new shell (terminal) the $PATH variable will have your personal "$HOME/bin/" folder, leading to any files there being considered as possible commands to execute - as you type the name of one of them at the shell prompt.

1
  • Exactly what I asked for. Thanks! – Chris Rahmé Nov 18 '20 at 11:31
10

For this specific task, I'd use make

Create a Makefile in the source directory with the following contents:

$ cat Makefile
CC := gcc

CFLAGS := \
        -m32 -g \
        -zexecstack -fno-stack-protector -mpreferred-stack-boundary=2 \
        -fno-pic

LDFLAGS := -no-pie

Then you can simply run make program to compile and link your code with the given options ex. given

==> hello.c <==
#include <stdio.h>

int main(void) { printf("Hello, world!\n") ; return 0; }

==> goodbye.c <==
#include <stdio.h>

int main(void) { printf("Goodbye, world!\n") ; return 0; }

then

$ make hello
gcc -m32 -g -zexecstack -fno-stack-protector -mpreferred-stack-boundary=2 -fno-pic  -no-pie  hello.c   -o hello

and

$ make goodbye
gcc -m32 -g -zexecstack -fno-stack-protector -mpreferred-stack-boundary=2 -fno-pic  -no-pie  goodbye.c   -o goodbye

For reference, see GNU make: Variables Used by Implicit Rules

1
  • Great answer, but I just wanted to learn how to make a custom command in general, not for this solution in particular. Thanks anyway! – Chris Rahmé Nov 18 '20 at 11:33
4

Quick and dirty

just make an alias, give it a cool name.

echo "alias gargamel='gcc -m32 -g -zexecstack -fno-stack-protector -mpreferred-stack-boundary=2 -no-pie -fno-pic -o'" >> /home/yourusername/.bashrc

source /home/yourusername/.bashrc

Execute it:

gargamel program program.c

This only works for the user you made the alias for. So you'd have to make an alias for other users(including root) if you want to run it as those users.

2
  • 1
    Don't give it a "cool" name; give it a descriptive name. – Asteroids With Wings Nov 18 '20 at 2:21
  • 2
    @AsteroidsWithWings The point was, it can be anything... informal, easy, understandable, relatable... etc... If I would have used "alias gcc-alias=" ...that can look like a more advanced command to inexperienced users and may possibly even cause additional questions. With the understanding that you can use a silly unrelated cartoonish word, the command looks a lot less intimidating/advanced. Then at that point, I'm sure that same inexperienced user, if need be, can then figure out if he/she would be better suited to use a descriptive name... – WU-TANG Nov 18 '20 at 2:53
2

Actually, there is a more powerful tool that can be used for that compile task -- Makefile, for some tutorial, please refer to this link.

And if you really want a easy bash script, try a script, e.g: compile.sh like this:

gcc -m32 -g -zexecstack -fno-stack-protector -mpreferred-stack-boundary=2 -no-pie -fno-pic -o "$1" "$1.c"

$ ./compile.sh program or some bash script tutorial.

2

In general, to create a custom terminal command with a variable argument, you can, in your script, make use of the "$1", "$2", etc variables. These return the first and second argument on the command line. "$0" returns the command itself.

Thus, for example, the following script foo:

#/bin/bash echo "$0" echo "$1" echo "$2"

will return, if you call it with the command line

foo one two three
foo
one
two
three
0

The Steeldriver answer is good for the specific situation in your question, as are aliases (if the variable data is at the end of the line!), but you can also write a bash function:

custom-gcc()
{
    gcc -m32 -g \
        -zexecstack -fno-stack-protector \
        -mpreferred-stack-boundary=2 -no-pie 
        -fno-pic -o ${1} ${1).c
}

Add that to the end of your ~/.bashrc, then "source" it by running . ~/.bashrc (note the leading .). Source it in every existing open terminal window. New windows will have it automatically.

Now, given file program.c, you can run:

custom-gcc program

The beauty of bash functions is that they can be as complicated as you want to make them (though scripts are better for anything longer than a few commands).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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