-1

I am wanting to build and run a program called dbgen using the external tools. I know that this variable holds the filename, ‍‍‍‍‍‍‍‍‍dbgen.c‍‍ :

$GEDIT_CURRENT_DOCUMENT_NAME

How do I remove the .c part so I can use it here for any file name:

#!/bin/sh
make dbgen
./dbgen

In addition, I would like to know how I can check if a makefile exists, if not, use some default flags on gcc instead, like CFLAGS=-Wall -g

Tool Window:

enter image description here

  • It is not clear where you are trying to program/achive something. Are you using a shell or are you doing something in a settings window of gedit? Or something else? What are the external tools? How is the file name and .c suffix question related to the second code block? – Lucas May 3 '15 at 22:41
  • @Lucas please see update. – sprocket12 May 3 '15 at 22:47
0

Remove extension:

Try to use the ${varname%.ext} expansion of sh/bash/zsh. For example like this:

#!/bin/sh
# ...
file_name_with_out_ext="${GEDIT_CURRENT_DOCUMENT_NAME%.c}"

(but beware that this will only remove .c extionsions and leave .h or .cpp, or any other extensions alone)

Test if file exists:

You can test if a file exists (for example a makefile) in the shell (sh/bash/zsh) with the test or [ command or the [[ builtin:

test -e some_file_name && echo exists
[ -e some_file_name ] && echo exists
[[ -e some_file_name ]] && echo exists

Be sure to check the man page for test(1), bash(1) or zshbuiltins(1).

Make file flags:

Do you mean something like this:

if [[ -e makefile ]]; then 
  make CFLAGS='-Wall -g' my_target
else
  make my_target
fi
0

To remove the extension use something like this-

echo $GEDIT_CURRENT_DOCUMENT_NAME | sed 's/\..*$//'

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.