如何通过在 shell 脚本中调用的窗口对话框获取目录名称?

gwa*_*rah 2 command-line nautilus scripts

我有一个 shell 脚本,想要获取由 nautilus 对话框(或其他工具)选择的目录名称。前任:

#/usr/bin/bash
# choosing a path name
export my_directory=`pwd`

# open a nautilus dialog (1)
# which command may I do here to get the dirercory?
my_directory=`nautilus ${my_directory}`

# my directory now has the directory chosen by nautilus dialog
echo "my directory is ${my_directory}"
Run Code Online (Sandbox Code Playgroud)

ubuntu 原生组件可以吗?

ste*_*ver 5

一种相当直接的方法是使用zenity例如

# basic syntax
zenity --file-selection --directory --filename="$PWD/"
Run Code Online (Sandbox Code Playgroud)

一个简单的代码(来源:gnome zenity help

#!/bin/bash

DIR=`zenity --file-selection --directory --title="Select a File"`
case $? in
         0)
                echo "\"$DIR\" selected.";;
         1)
                echo "No file selected.";;
        -1)
                echo "An unexpected error has occurred.";;
esac
Run Code Online (Sandbox Code Playgroud)

有关zenity --help-file-selection其他选项,请参阅。