1

I'm new, and joined to ask a question about a script (first answer at How to disable “Extraction completed successfully” dialog in Nautilus?) and could not "comment" beneath it (no reputation) and posting it via Answer didn't seem right.

So the question is about using double-digits or, even better, specifying ALL in a script like the following:

#!/bin/bash
p1=$1
p2=$2
p3=$3
p4=$4
p5=$5
p6=$6
if [[ $p2 == *"notify"* ]]; then
        p2=""
fi
/usr/bin/file-roller_orig $p1 $p2 $p3 $p4 $p5 $p6

The script is to disable the successful extraction popup using the File Roller context-menu option in Nautilus, and it successfully does that. However it is limited to only extracting up to 4 archives at once. Since I often extract a dozen or more files at once, and seeing that p2=$2 was tied to the notification, I experimented with adding p7=$7 etc (I am no CLI guru) and it did indeed extract more than 4, but after p9=$9 (which extracts 7 archives), it stopped there - which I thought might happen as in Gedit the last digit at the end of p10=$10 etc was a different colour to the rest (I figured that wasn't good).

So how do I make entries after p9=$9 or, much better still, get the script to tell file-roller to extract all the archives that were selected? The latter would be ideal, as even if I can make it extract a dozen files at once, I'm bound to want to extract more than that at some point, and not have to do it in batches. Many thanks in advance for your answers. Cheers.

OzzyFrank
  • 171

2 Answers2

2

You can use "$@" to get all parameters and shift N to remove N arguments.

#!/bin/bash
p1="$1"
p2="$2"
shift 2
if [[ $p2 == *"notify"* ]]; then
        p2=""
fi
/usr/bin/file-roller_orig $p1 $p2 "$@"

Quoting variables with double quotes (such as "$1") is a good idea to ensure bash doesn't try and use one variable as multiple arguments.

Although using $@ is better, it's possible to use ${10} to access the tenth argument.

luk3yx
  • 401
1

IMHO this approach is flawed, for a couple of reasons.

Most importantly, it relies on --notify being given as the second positional parameter of the command. While this may be the case when invoked from a nautilus action, there's no guarantee it will remain so (and certainly cannot be guaranteed if file-roller is run in other contexts).

Secondly, as we can deduce for the fact that the approach only "works" if the parameter is not quoted, you don't want to set the replacement value to the empty string - you want to remove the argument altogether.

What I'd suggest instead is

#!/bin/bash

declare -a args

for arg do case $arg in "--notify") continue ;; *) args+=("$arg") ;; esac done

/usr/bin/file-roller "${args[@]}"

Save somewhere earlier on your PATH than the original /usr/bin/file-roller - for example /usr/local/bin/file-roller (to override the default behavior system-wide) or ~/bin/file-roller (to override for your user only).

steeldriver
  • 142,475