1. Create Nautilus script's file and make it executable:
touch "$HOME/.local/share/nautilus/scripts/MyC++Run"
chmod +x "$HOME/.local/share/nautilus/scripts/MyC++Run"
2. Here is the content of the script. It creates an auxiliary (auto deleted) script, which is executed in a new gnome-terminal, thus you can see the error messages within the terminal window:
#!/bin/bash -e
# Get the list of the selected in Nautilus items as an array $ITEM_LIST
IFS_BAK=$IFS
IFS=$'\t\n'
ITEM_LIST=($NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)
IFS=$IFS_BAK
# Create aux script, that compile and execute the program. Run the script in gnome-terminal
compile_and_exec_program() {
OUT="${DIR}/${NAME}.out" # Define the name of the output file
AUX="${DIR}/${NAME}.bash" # Define the name of the aux script
printf '#!/bin/bash -e\n' > "${AUX}" # Create the auxiliary script
printf '%s "%s" "%s" && "%s"\n' "${1}" "${OUT}" "${item}" "${OUT}" >> "${AUX}"
printf 'rm -f "%s"\nexec bash' "${AUX}" >> "${AUX}"
chmod +x "${AUX}" # Make the aux script exec and run it
nohup gnome-terminal -x sh -c "$(echo \'"${AUX}"\')" >/dev/null 2>&1 &
}
# For each selected item: get its name, location, etc. and proceed...
for item in "${ITEM_LIST[@]}"; do
ITEM="$(basename "${item}")" # Get the item name (exclude the path)
DIR="$(dirname "${item}")" # Get the path to the item (exclude the name)
NAME="${ITEM%.*}" # Get the name (exclude the extension)
EXT="${ITEM##*.}" # Get the extension (exclude the name)
# If the item is a file and its extension is `c` or `cpp`, then compile and execute
if [ -f "$item" ]; then
if [ "$EXT" == "c" ]; then compile_and_exec_program "gcc -o"
elif [ "$EXT" == "cpp" ]; then compile_and_exec_program "g++ -o"
else notify-send "Wrong extension of the selected file: $ITEM"
fi
else
notify-send "The selected item is a directory: $ITEM"
fi
done
Additional explanations: Using of an auxiliary script is the most robust way to run more than one commands within a new gnome-terminal, that I found while I made one of my answers.
Depending of the input parameters of the function compile_and_exec_program
, the content of the generated, by the printf
section, auxiliary script will be similar as:
#!/bin/bash -e
g++ -o /work/dir/project.cpp /work/dir/output.out && /work/dir/project.out
rm -f /work/dir/project.bash
exec bash
Where &&
means (as usual) if the command that is on the left side is successfully executed, then execute the command that is on the right side. The line rm -f /work/dir/project.bash
will remove the auxiliary script itself. The last line exec bash
intends to keep open the new gnome-terminal window.
This part $(echo \'"${AUX}"\')
intends to print single quote marks around the name of the aux script. It is important when the script name contains some special characters. I couldn't found other way to do that. Another way to qote only the spaces is using of: ${AUX/\ /\\ }
.
Here is an example script, that creates a log files, where you can see the error messages from the process.
3. Here is a demonstration (from the previous version) of the script's features:
