Search code examples
linuxshellnautilusmeld

Shell scripts for Meld Nautilus context menu


Beyond Compare provides "Select for compare" and "Compare to Selected" by using two nautilus scripts (stored in /home/user/.gnome2/nautilus-scripts).

Script 1: Select for compare

#!/bin/sh
quoted=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' | sed -e s#\"\"##)
echo "$quoted" > $HOME/.beyondcompare/nautilus

Script 2: Compare to Selected

#!/bin/sh
arg2=$(cat $HOME/.beyondcompare/nautilus)
arg1=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' | sed -e s#\"\"##)
bcompare $arg1 $arg2

I am trying to do similar scripts for Meld, but it is not working.

I am not familiar with shell scripts. Can anyone help me understand this:

quoted=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' | sed -e s#\"\"##)

so that I can adapt to meld.


Solution

  • The quoted=$( ...) assigns whatever output there is to the variable named quoted, and can be used later in the script as $quoted OR ${quoted} OR "${quoted}" OR "$quoted"

    The '|' char is called a 'pipe' in unix/linux and it connects the output of the preceding command to feed into the following command.

    So you just take the script apart 1 piece at a time and see what it does,

    quoted=$(
    # I would execute below by itself first
    echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
    # then add on this piped program to see how data gets transformed
    | awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' 
    # then add this
    | sed -e s#\"\"##
    # the capturing of the output to the var 'quoted' is the final step of code
    )
    
    # you **cannot** copy paste this whole block of code and expect it to work ;-)
    

    I don't know what is supposed to be in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS, so it is hard to show you here. AND, that variable is not defined in any of the code you specify here, so you may only get a blank line when you echo its value. Be prepared to do some research on how that value get set AND what are the correct values.

    Also I notice that your code is 'prefixed' as #!/bin/sh. If it is truly /bin/sh then command substitution like quoted=$(....) will not work and should generate an error message. Persumably your system is really using bash for /bin/sh. You can eliminate any possible confusion in the future (when changing to a system where /bin/sh = bourne shell), by changing the 'shebang' to #! /bin/bash.

    I hope this helps.