LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2006, 09:03 PM   #1
fatsheep
Member
 
Registered: Sep 2006
Posts: 79

Rep: Reputation: 15
Weird Nautilus Bash Script Problem


I'm making a nautilus script that force architecture installs debian packages (for Ubuntu). To do this is opens up a terminal (gnome-terminal) and executes the proper dpkg command. Unfortunately it opens up two terminals: one for the package I want to install and one that does not install a package (I get the no package inputted blah blah blah error message). Here's my code:

Code:
#!/bin/bash
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" > ~/Desktop/log
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read file ; do
	exec_file=$(echo $file | sed 's@ @\\ @g')
	gnome-terminal -e "bash -c 'echo "$exec_file" ; sudo dpkg --force-architecture -i "$exec_file" ; read'"
done
Just so you know, in this case, $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS is equal to "/home/ubuntu/Desktop/picasa_2.2.2820-5_i386.deb" without quotes. ($NAUTILUS_SCRIPT... is equal to the path of the files currently selected in nautilus).

What's really strange is I've tried wrapping the gnome-terminal line in an if statement to make sure the $exec_file is non zero (if [ -n $exec_file ] ; then .......) but it still opens up an extra terminal with no package input.
 
Old 11-19-2006, 04:52 AM   #2
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
It might be because $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS contains an additional newline and the while read splits by IFS-whitespace (space, tab, newline).

If $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS is a newline terminated list, you can leave out the whitespace-escaping with sed because the default delimiter of read is a newline.

The test 'if [ -n $exec_file ]' doesn't work, because you need to quote $exec_file to skip the newline.

Also the quoting of $exec_file within the bash command needs to be escaped (\").

So the script should be something like this:

Code:
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read file ; do
    if [ -n "$file" ]; then
        gnome-terminal -e "\
            bash -c 'echo \"$file\";\
            sudo dpkg --force-architecture -i \"$file\";\
            read'\
        "
    fi
done
or a bit shorter:

Code:
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read file ; do
    [ -n "$file" ] && gnome-terminal -e \
        "bash -c 'echo \"$file\";\
        sudo dpkg --force-architecture -i \"$file\";\
        read'"
done
Hope this helps,

Freddy Vulto
http://www.fvue.nl/wiki/Bash

Last edited by fvu; 11-24-2006 at 02:35 AM.
 
Old 11-19-2006, 05:51 PM   #3
fatsheep
Member
 
Registered: Sep 2006
Posts: 79

Original Poster
Rep: Reputation: 15
Thanks, this method works great. The only time it fails is when multiple files are involved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in script bash with if nadavvin Linux - General 2 08-05-2006 02:51 PM
bash script problem noir911 Programming 1 01-21-2006 06:02 PM
Weird Nautilus Problem cthulueldergod Linux - Software 1 03-29-2005 06:00 PM
bash script problem sorry99 Programming 2 09-20-2004 10:02 AM
bash script connect from one host to another and to another weird aim nakkaya Linux - Networking 1 06-30-2003 02:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration