简体   繁体   中英

Adding an graphic Interface for a Gnu/linux bash Script

I try to execute a bash script which works totally fine when executed form the shell. However if I try to execute it using my grahpic menu script the execution fails. There must be a problem with the parameters, but I can't find it. For example if I try to execute option one with my gui, the correct strings are saved in the variable, but cd in option 1 won't work.

Code of my main-program which is called getList.sh

#! /bin/bash
SourcePath=$1

DestinationPath=$2

DestinationFileName=$3

Option=$4

#Option 1
#Creates a recursive list of all files in a directory and subdirectories, if the destination file already exists, the content will be overwritten 
if [ $Option = 1 ]
then
                cd $DestinationPath        
                ls -1Rasl $SourcePath > $PathDate
                

############################################################################
#Option 2
#Creates a recursive list of files in a directory and subdirectory including files in archives and prints it  
elif [ $Option = 2 ]
then
                MakeFileListAndPrint

############################################################################
#Option 3
#Recursive search for a file name in all files including archives, prints all found macthes with path
else 
        MakeFileList 
        grep -H $SearchFile $PathDate  

fi

GUI-CODE:

#! /bin/bash


SourcePath=$(zenity --entry --text "Enter your source path" --title "sourcepath" --entry-text=""); 
DestinationPath=$(zenity --entry --text "Enter your destination path" --title "destination path" --entry-text="");
DestinationFileName=$(zenity --entry --text "Enter your destination file name" --title "destination file name" --$

ask=`zenity --list --title="Choose your Option" --column="0" "1" "2" "3" --width=100 --height=300 --hide-header`

if [ "$ask" == "1" ]; then
    bash ./getList.sh $SourcePath $DestinationPath $DestinationFileName $ask
    
 fi

if [ "$ask" == "2" ]; then
    bash ./getList.sh $SourcePath $DestinationPath $DestinationFileName $ask
fi

if [ "$ask" == "3" ]; then
    SearchFile=$(zenity --entry --text "Enter your search term" --title "search term" --entry-text="");
    bash ./getList.sh $SourcePath $DestinationPath $DestinationFileName $ask $SearchFile
fi

Output with echo statements with these given parameters: bash menu $1=~/test $2=~/Destination $3=testfile $4=1

user:~/debug$ bash menu.sh 
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
Sourcepath in menu is: ~/test 
DestinationPath in menu is ~/Destination 
Destination Filename in Menu is: testfile 
option 1 is executed
DestinationPath in getList is: ~/Destination
PWD bevor change dir user/debug
./getList.sh: line 85: cd: ~/Destination: No such file or directory
PWD after change dir user/debug
./getList.sh: line 87: ~/Destination/testfile: No such file or directory
PWD after Rasl user/debug
program executed
user:~/debug$ 

Your problem is that ~ is not expanded. This works:

cd ~

But puting a tilde ( ~ ) to zenity would be returned in double-quotes, just try this, entering just the tilde:

cd $(zenity --entry)

It is the same as

$ cd "~"
bash: cd: ~: No such file or directory

This worked for me to re-trigger the bash expansion:

x=$(zenity --entry)
cd $(eval echo $x)

However this is not safe! If the user would put some dangerous code to x, it would be evaluated! This is somewhat better:

cd $(echo $x | sed "s:~:$HOME:")

This exploits this simple difference:

$ echo ~ $HOME
/home/roman /home/roman
$ echo "~ $HOME"
~ /home/roman
$ echo '~ $HOME'
~ $HOME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM