Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a script exists and source it if it does

Tags:

gdb

I do not see native GDB commands can do this: check if a gdb script file exists, if so, source the file.

Maybe I need to resort to python, but still want to ask here.

The use case is, I am working on several different computing environments, each one has some different GDB setup. I like to add the above to ~/.gdbinit so that even in each environment I have a different GDB script, it is automatically sourced in my ~/.gdbinit.

like image 243
my_question Avatar asked Sep 01 '25 10:09

my_question


1 Answers

There's no built-in way to do this. It can be done via scripting in a couple of different ways.

The classic way is to use shell to test the file existence and have it write out a file that then decides what to do. Something like:

(gdb) shell if test -f blah; then echo source blah; fi > /tmp/F
(gdb) source /tmp/F

This is pretty ugly, but it would work.

Alternatively you can write a Python command to do this quite easily. Or you can dig up the Python ignore-errors command, and just use ignore-errors source whatever.

like image 147
Tom Tromey Avatar answered Sep 06 '25 16:09

Tom Tromey