1

I have made (with my little knowledge of Bash and an extensive use of a search engine) a Bash script to reorder the pages of a big PDF file:

#!/bin/bash
file=originalfile.pdf;
newfile=$(basename $file .pdf)-2.pdf;
tmpfile=$(mktemp --suffix=.pdf);
blankfile=$(mktemp --suffix=.pdf);
cp -f $file $newfile;
cp -f $file $tmpfile;
numberofpages=`pdftk $file dump_data | grep "NumberOfPages" | sed 's:.*\([0-9][0-9*]\).*:\1:'`;
echo "" | ps2pdf -sPAPERSIZE=a4 - $blankfile;
while (( $numberofpages % 4 != 0 ));
do 
  ((numberofpages++));
  pdftk A=$newfile B=$blankfile cat A B output $tmpfile;
  cp -f $tmpfile $newfile;
done;
neworder=`
for ((  a=1, b=3, c=4, d=2 ;
        a <=numberofpages ;
        ((a+=4)), ((b+=4)), ((c+=4)), ((d+=4))
    ));
do
echo -n "$a $b $c $d ";
done`;
pdftk $tmpfile cat $neworder output $newfile;

I wanted to make a Nautilus-Actions script out of it so it could be "installed" and used by a regular user. By regular user, I mean someone unable to type any command-line and unable to follow a few steps to copy the script at a specified place.

Unfortunately the script didn't work and I came up with this new script thanks to the help of people commenting below:

#!/bin/bash
file=originalfile.pdf;
newfile=$(basename $file .pdf)-2.pdf;
tmpfile=$(mktemp --suffix=.pdf);
blankfile=$(mktemp --suffix=.pdf);
cp -f $file $newfile;
cp -f $file $tmpfile;
numberofpages=`pdftk $file dump_data | grep "NumberOfPages" | sed 's:.*\([0-9][0-9*]\).*:\1:'`;
echo "" | ps2pdf -sPAPERSIZE=a4 - $blankfile;
while (( $numberofpages % 4 != 0 )); # NOTE: replace % by %% in Nautilus-Actions
do 
  ((numberofpages++));
  pdftk A=$newfile B=$blankfile cat A B output $tmpfile;
  cp -f $tmpfile $newfile;
done;
a=0;
neworder=$(
  while [ $a -lt $numberofpages ];
  do 
    echo -n "$(($a + 1)) $(($a + 3)) $(($a + 4)) $(($a + 2)) ";
    ((a+=4));
  done;
);
pdftk $tmpfile cat $neworder output $newfile;

I did paste everything in the Path entry of Nautilus-Actions and it finally worked. The newly created Nautilus-action could then be exported in a .desktop file (and therefore imported very easily by any user):

Screenshot of Nautilus-Actions

If I ask Nautilus-Actions to display the output, It seems that Nautilus-Actions execute the command line inside a /bin/sh -c 'myscript...' command.

Could you explain to me why I had to change so many things in order to make it work ? Especially why I had to change the for into a while ?

Note: I completely revamp the question since It was a mess.

remjg
  • 536
  • 5
  • 16
  • 2
    Why don't you save your command in a file, add a [shebang line](http://en.wikipedia.org/wiki/Shebang_(Unix)) at the top, mark it as executable, and then make the shortcut point to that? – Burhan Khalid Jun 01 '14 at 09:30
  • If I do that, I will have to explain the "regular" user where to copy the script and how to configure a Nautilus action. I would prefer them to just import a `.desktop` file in Nautilus-Actions. – remjg Jun 01 '14 at 09:34
  • 3
    `sh` is not `bash` ;). For your quotes problem, you could use `sh -c "sh -c 'msg=\"hello\"; echo \"$msg\";'"` for example. – Idriss Neumann Jun 01 '14 at 09:38
  • You can still do that by adding the file to their `~/bin` directory, which is automatically added to the path by most linux distributions. Further, you can also make this file part of all new users by adding it to [default skeleton](http://www.linfo.org/etc_skel.html) that is used when creating new accounts. – Burhan Khalid Jun 01 '14 at 09:40
  • @IdrissNeumann: Thanks for pointing out the difference to me (I looked at [question 5725296](http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash)). Your valid solution won't work for me since I can't change the first `sh -c '...'` command (it is automatically inserted by Nautilus-Actions). – remjg Jun 01 '14 at 09:46
  • @BurhanKhalid : Most people won't be able to use the command line at all. I'm preparing a training for a group of teachers that don't know anything about Linux and command lines. It is about using [this software to create and correct multiple choice tests](http://home.gna.org/auto-qcm/index.en) and they would need this script for a special kind of printing layout. The software is already difficult to use, and I don't want to add more complexity for simple tasks that would discourage the audience. – remjg Jun 01 '14 at 09:55
  • 1
    Are you sure you are not allowed to use single quotes in your command? Single quotes are a property of the shell, but Nautilus probably doesn't use a shell internally (then the whole `sh` would be redundant). It probably runs `execve("sh","-c",variable)` where it is fine for `variable` to contain single quotes. I would consider it a bug if you were not able to use any valid shell construct in this context. – tripleee Jun 01 '14 at 10:27
  • 2
    However, your script seems to contain Bash constructs; you should be invoking it with `bash -c '...'` rather than `sh -c '...'` (or refactor to not use any bashisms). (I guess this is what @IdrissNeumann was also trying to point out.) – tripleee Jun 01 '14 at 10:30
  • I see this is a bug in the original script you link to as well. – tripleee Jun 01 '14 at 10:34
  • @triplee: Indeed, I'm currently learning the difference between `sh` and `bash` and I will correct the script. You are right, the second `sh` is redundant, I have just found that I can write everything in the `Path` entry. Sorry my question is a mess ! – remjg Jun 01 '14 at 10:55
  • I finally came up with a working script. I had to change the question to make it clearer. Hope you don't mind. So basically, if you still think it was related to Bash/Sh differences, you are very welcome to make an answer. – remjg Jun 01 '14 at 14:17

0 Answers0