Pass filename from Nautilus to shell script

一曲冷凌霜 提交于 2021-01-29 04:52:50

问题


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?


回答1:


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




回答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


来源:https://stackoverflow.com/questions/41516147/pass-filename-from-nautilus-to-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!