1

I've written this shell script to get the name of the process spawned from the exec line.

My issue is when I try it on Arduino IDE I get an error. I investigated and its exec line is another shell script.

I'm not sure if this is my issue but I've been trying to play around with it to make it load and I just cant seem to.

My script

#!/bin/bash

exe=$(grep '^Exec' "$1" | tail -1 | sed 's/^Exec=//' | sed 's/%[a-zA-Z]*//')
type=$(file $exe | grep "Bourne-Again")

if [ -z "$type" ]; then
  echo Debug - its a shell script
  bash "$exe" &
else
  echo Debug - its not a shell script
  $exe &
fi
PID=$(echo $!)
process=$(ps --no-header -p $PID -o comm)
kill -SIGTERM $PID

echo $exe 
echo $process

The error

bash PycharmProjects/touch_mouser/TouchMouser/get_exe_and_process_name.sh "/usr/share/applications/arduino-arduinoide.desktop"

Debug - its a shell script
bash: "/home/lewis/builds/arduino/arduino-1.8.12/arduino": No such file or directory
PycharmProjects/touch_mouser/TouchMouser/get_exe_and_process_name.sh: line 15: kill: (27840) - No such process
"/home/lewis/builds/arduino/arduino-1.8.12/arduino" ====

But if I run this is the terminal it works fine.

bash "/home/lewis/builds/arduino/arduino-1.8.12/arduino"

Any one know why or shed any light on it?

2
  • 2
    What is the exact output of your grep '^Exec'... command? I suspect it is returning "script.sh" and not script.sh.
    – terdon
    Jun 12, 2020 at 13:39
  • 1
    Yes, I agree with terdon. You could add echo bash "$exe" before the actual bash line bash "$exe" & to see what's happening there.
    – Quasímodo
    Jun 12, 2020 at 13:40

1 Answer 1

3

It looks like your exe variable has the name of the script quoted. So, if the script is foo.sh, then $exe is actually "foo.sh" and not foo.sh. Therefore, you are telling bash to find a file whose name includes those quotes. To illustrate, here's an artificial example:

$ cat foo.sh
#!/bin/sh
echo "It ran!"

Now, set a variable to the script's name quoted:

$ exe='"foo.sh"'
$ echo "$exe"
"foo.sh"

And try to run it:

$ bash "$exe"
bash: "foo.sh": No such file or directory

And the same thing but without putting the name of the scripts already quoted in the variable:

$ exe="foo.sh"
$ echo "$exe"
foo.sh
$ bash "$exe"
It ran!

So, just remove the quotes and you should be set:

exe=$(grep '^Exec' "$1" | tail -1 | sed 's/^Exec=//; s/%[a-zA-Z]*//; s/"//g')
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .