1

I am trying to execute a shell script that gets for an argument a file name.

I can do it if I go to the terminal and type the full PATH.

But I am trying to make this more user friendly so I what I want to do is to send the argument (complete path) from the file manager or nautilus.

Is it possible?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Agustin
  • 31
  • 6

2 Answers2

3

It is possible. Here's a way to do it:

Copy your script to the folder ~/.local/share/nautilus/scripts/ -- please note that this path will expand to include the current user information, such as: /home/john/.local/share/nautilus/scripts/.

Now, on your script, capture the argument using $1. For example, let's say that your script is intended to delete the file, then your script could be like this:

#!/bin/bash
rm -f "$1"
exit

Now, restart Nautilus and navigate to the file you want to execute the action on. Right-click the file, and on the menu that appears, choose "scripts", and on the sumenu that appears, choose your script name -- that will execute your script with the file name as argument.

Note: don't forget to make your script executable first, for example: chmod +x /home/john/.local/share/nautilus/scripts/myScript

Jamil Said
  • 2,033
  • 3
  • 15
  • 18
  • Hey Thankyou thats an amazing answer with everything so clear. I am going to try that right now. I also found another way to do it. It is not exactly executing an script from nautilus but it works for me. I am sharing as someone might find this useful. When executing your script, use zenity file selection tool. In my example ./script.sh $(zenity --file-selection) That will allow you to browse through your files and when you select a file. The path is passed as an argument to your script. Again thanks everyone for the help and good luck everybody. – Agustin Jan 07 '17 at 04:07
  • You're welcome. When an answer helped you on stack Overflow, consider upvoting that answer so that it will signal to others that it is a good answer. Another thing: if your script requires root privileges to work, you can still achieve that by using `gksudo` on the invocation. – Jamil Said Jan 07 '17 at 05:52
2

Ok I am going to share a way I found that works.

All I was trying to do was to pass a path as an argument, without typing it on the command line. to make it user friendly.

So I use a zenity tool, file selection, to browse through the file manager and select the file.

./script.sh $(zenity --file-selection)

See, now I am passing the path of the file I selected.

If you want to try this out, just type in the command line.

zenity --file-selection
Agustin
  • 31
  • 6