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
  • 161
  • 7
  • 1
    Since files in /usr/bin may be overwritten by package updates, it would be better imho to create the wrapper acript as `/usr/local/bin/file-roller`, and have it call the original file-roller as `/usr/bin/file-roller` – steeldriver Dec 10 '20 at 02:47
  • Many thanks for that bit of info. I figured it would get replaced the next time file-roller gets an update, but that's easily rectified. But out of curiosity: will your solution still work when using the "Extract Here" context-menu option in Nautilus? I'm guessing Nautilus will look for /usr/bin/file-roller, so the wrapper would be ignored. If that is the case, is there a way to get Nautilus to use /usr/local/bin/file-roller instead? – OzzyFrank Dec 10 '20 at 03:27
  • TBH I'm not sure how nautilus actions work - it may be necessary to modify the corresponding .desktop file (which you should do on a local copy, rather than the "systemwide" one in /usr/share - for the same reasons) – steeldriver Dec 10 '20 at 13:03

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
  • 6
  • 18
  • Many thanks for your answer, but with your revised script I get the following error popup: Could not open "myusername" Command not found Any ideas? – OzzyFrank Dec 10 '20 at 03:13
  • It's OK, I figured it out: I removed the quotes, and it worked fine. Then I tested it on extracting 18 archives at once, and it worked a treat. Many thanks for your help!!! I think I might add it as an Answer so other people can clearly see it. Cheers – OzzyFrank Dec 10 '20 at 03:21
  • I've edited my answer with the fixed code, sorry for not catching that issue. – luk3yx Dec 10 '20 at 03:22
  • No worries, and thanks heaps for your help! But... I took out all the quotes, and it still works fine? Does that seem right? – OzzyFrank Dec 10 '20 at 03:29
  • It does, I don't think they're required. – luk3yx Dec 10 '20 at 03:32
  • Many thanks once again - you're a champion. I'll have to come back later once I have more "reputation" so I can upvote your answer. Now I have to see if I can find out how to mark this as [SOLVED]... – OzzyFrank Dec 10 '20 at 03:40
  • @OzzyFrank if you need to remove quoting to make it work, then something else is wrong - possibly `$2` should have been removed rather than set to hte empty string `""`? (The only place quoting is not required in the above is on the RHS of the assignments - see for example [When is double-quoting necessary?](https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary) ) – steeldriver Dec 10 '20 at 11:59
  • @steeldriver - I should have clarified I removed the quotes from the top and bottom; the if/then section I left as-is. The following works fine: #!/bin/bash p1=$1 p2=$2 shift 2 if [[ $p2 == *"notify"* ]]; then p2="" fi /usr/bin/file-roller_orig $p1 $p2 $@ – OzzyFrank Dec 10 '20 at 23:37
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
  • 131,985
  • 21
  • 239
  • 326
  • Thanks for the info. You're the expert here - I didn't even understand half of what you said, haha! (sorry, you need to speak in layman's terms to people like me) - but as "flawed" as this approach is, it works flawlessly. I mean, all I want is to be able to extract multiple archives at once, and without a single completion notification, and it does this without issue. But I'll definitely add this code to my document containing all the bits and pieces related to this, in case I need it in the future. – OzzyFrank Dec 11 '20 at 05:23
  • As for the path where to put this wrapper, as you said: you do not know how the actions in the Nautilus context-menu work, and this is **exactly** how I am accessing "Extract Here". Sure, I can look into which .desktop file to edit, and how, but this works right now, no fuss. The only downside I can see is it will be overwritten during the next file-roller update, which is no big deal, as I have a backup I can easily copy back over. If there is something important I am missing here, please feel free to enlighten me. – OzzyFrank Dec 11 '20 at 05:23
  • Re "certainly cannot be guaranteed if file-roller is run in other contexts": this really isn't an issue - pretty much the only way I use file-roller is through the Nautilus context-menu. But thanks once again for all this info. I definitely will be saving it, and it is enlightening (the bits I can understand - I am no code guru by any stretch of the imagination!). Actually, I may even replace my code with yours; while what I have works fine, if yours does the same, and *could* prevent issues in the future, then why not, I suppose. – OzzyFrank Dec 11 '20 at 05:30
  • Unfortunately, not only does your code **not work**, to say it turns out yours is the "problematic" one would be a huge understatement! Due to character limit here, I'll have to split it up, but basically not only didn't it work in the slightest, but after 12 mins of a frozen system and furious HD activity, I was forced to do a hard reboot. Tested it again after that, and yeah - there is something extremely wrong with your code. First off, double-clicking an archive briefly shows Archive Manager appearing in the taskbar, then just exists. Tried it twice, no difference. (Cont) – OzzyFrank Dec 13 '20 at 00:11
  • Right-clicking an archive and choosing "Extract Here" does nothing. Tried 3 times. Then strange flickering of screen I've never seen before started. Mouse froze. Once flickering stopped, got mouse back but could not click on anything. Another 10 mins of this and the HD light blinking continually I had to hit the reboot button. Upon reboot, tried all that again, but this time when things started slowing down, like jerky scrolling of web pages, mouse intermittently stalling, I have terminal open and did a 'killall file-roller' and everything was OK. (Cont:) – OzzyFrank Dec 13 '20 at 00:16
  • After I replaced your code with the previous one, then saved file-roller, suddenly multiple Archive Manager windows appeared (one for each time I had double-clicked each of 2 archives) + the 2 files I had right-clicked suddenly got extracted. I'm guessing it has to do with "Save somewhere earlier on your PATH than the original /usr/bin/file-roller" - sorry, but I don't even really know what that means. I know you're adamant I should be saving this elsewhere, but overwriting file-roller works for me. I now see your code calls for file-roller with the old name, which was the problem (Cont:) – OzzyFrank Dec 13 '20 at 00:29
  • Lastly, while I thank you for your attempt to help, you should have specified it could cause issues if I didn't change the path (and how to do that) or tell me to edit your last line to point to the renamed original if I wasn't going to do that. As I'd said before, I was quite happy overwriting file-roller after renaming the original, so being clueless re scripts I couldn't see that using your code as-is was going to cause massive problems. Anyway, I'll edit that code, and try again, but feel free to enlighten on the PATH thing - happy to try that if I know HOW. Thanks again. – OzzyFrank Dec 13 '20 at 00:40
  • I edited the code to point to file-roller orig (can't put underscores here) and it works great. The reason I tried it was because the code I used had one quirk: while right-click extract worked great, double-clicking an archive would open 2 instances of it. Not a major annoyance, as I rarely do that, but worth trying your code (which only opens 1 window). But I am happy to do your suggestion (I understand what you're saying just not how to achieve it) BUT only if this still works for "Extract Here" in Nautilus context-menu. That's vital. Thanks once again. – OzzyFrank Dec 13 '20 at 00:54
  • Oh, by "earlier on your PATH" do you mean ALPHABETICALLY? No, wait: /usr/local/bin comes after /usr/bin. OK, I'm guessing that the wrapper file-roller being elsewhere means it gets used instead of the actual file-roller, but I really need clarification on how this works, if it's not alphanumerical. (But I guess you're saying /usr/local/bin/ gets used before /usr/bin/ but I'd still like to understand that). And I'm happy with applying this system-wide. Many thanks once again. – OzzyFrank Dec 13 '20 at 01:03
  • @OzzyFrank apologies if that wasn't clear. Your `PATH` is a colon-separated list of locations that is searched **in order** for executable programs. By convention, personal files (in `~/bin`) come before local systemwide files (`/usr/local/bin`) which come before global systemwide files (`/usr/bin` or `/bin`). The idea was to place the script in `~/bin` or `/usr/local/bin` so that when `file-roller` is invoked from nautilus it finds the script first, which modifies the given arguments and then calls the global `/usr/bin/file-roller`... – steeldriver Dec 13 '20 at 01:27
  • ... That way, nothing breaks if `/usr/bin/file-roller` gets overwritten by a subsequent package update. Unfortunately, by saving the **script** as `/usr/bin/file-roller` you created an infinite loop - I didn't anticipate that you would do that. – steeldriver Dec 13 '20 at 01:28
  • Thanks - didn't know that about the bin folders, and always wondered about the different locations. I'll make a note of that. And it was only after posting here that I saw "/usr/bin/file-roller" in your code, and realised it had caused a loop. If I had caught that before using it, I would have changed it to include orig. But I will do as you suggest and rename the original & put the wrapper elsewhere. Saves me having to restore the wrapper backup next time file-roller gets updated. Thanks for all the info. – OzzyFrank Dec 13 '20 at 04:05