0

I'm new at scripting but giving it a try and encountering some problem I can't seem to fix.

What I'm trying to do is to cat different pdf files using pdftk , the files I'd like to cat are being fed from a different program which is able to trigger a script file, but unable to send parameters with it. Therefore I create two .txt files one with a single setting and one with a file name. The setting file has to tell my script if it should create a new file or not and the filename should tell my script what file to cat into the big pdf file.

Both the setting and filename files are read into variables and by using echo I can see that the values of these variables are correct, I can't however use them for some reason I can't figure out.

This is my script:

#!/bin/bash

#on Date       Who   Call Remarks
# ------- ---------- --- ------ -------------------------------------------- 
# 1.0     28-07-2014 AAS ------ New script to combine pdf files.
#
# ==========================================================================
#
#Changelog:
#
#
#
# Locvar:

file=$(cat "/home/scanning/finloon/concatfile.txt")
setting=$(cat "/home/scanning/finloon/concatsetting.txt")

echo $file
echo $setting

removefile()
{
    echo "remove combine.pdf"
    rm -f -r /home/scanning/finloon/combine.pdf
    rm -f -r /home/scanning/finloon/workfile.pdf
}

makefirst()
{
    echo "make first combine.pdf"
    pdftk /home/scanning/janssen/linked/44/$file cat output combine.pdf
}

copyfile()
{
    echo "copy to workfile.pdf"
    cp /home/scanning/finloon/combine.pdf /home/scanning/finloon/workfile.pdf
}

combinefile()
{
    echo "combine files"
    pdftk /home/scanning/finloon/workfile.pdf /home/scanning/janssen/linked/44/$file     cat output combine.pdf
}

if [ "$setting" == "yes" ];
        then
    removefile
    makefirst $file
else
    copyfile
    combinefile $file
fi

exit

the content of concatsetting.txt = yes (unix file, contains no spaces)

the content of concatfile.txt = 1.pdf (unix file, contains no spaces)

Now when I run My script I's expect it to do the first commands in the if statement due to the value of $setting being yes but it won't. I suspect it has something to do with the string I use as a variable because when I change the if-statement to use a variable $1 and I start the script by concatcmr.sh yes it works but then I'm also unable to use the filename stored in the file variable. here I seem to have the same problem because if I change that variable to $2 and start my script as concatcmr.sh yes 1.pdf everything works.

Now if I run the script this is the output I get:

$ concatcmr.sh

1.pdf

yes

copy to workfile.pdf

cp: cannot stat `/home/scanning/finloon/combine.pdf': No such file or directory

combine files

Error: Unable to find file.

Error: Failed to open PDF file:

/home/scanning/finloon/workfile.pdf

Error: Unable to find file.

Error: Failed to open PDF file:

Errors encountered. No output created.

Done. Input errors, so no output created.

$

sllthough the content of $setting is yes the if statement doesn't recognise this and skips to the else statement. I can't figure out why, must be something stupid but I just can't find It.

Thanks in advance for your help!

cheers.

mechuccio
  • 1
  • 1
  • If what you are trying to do is merge pdf files, have a look at http://stackoverflow.com/questions/2507766/merge-convert-multiple-pdf-files-into-one-pdf – Kokkie Jul 31 '14 at 08:41
  • Thanks @Kokkie for your comment but merging is not my problem, I'm not able to work with variables that are getting a value from a different file. – mechuccio Jul 31 '14 at 08:49
  • You should be using proper quoting (double quotes) around any string which is not specifically required to be split on whitespace and have its wildcards expanded. Anyway, does `echo "'$setting'"` reveal any whitespace or other aberrances in the value you read from the file? It apparently isn't literally the three bytes `y`, `e`, `s` (and possibly a final newline). – tripleee Jul 31 '14 at 08:59
  • Your functions should examine the local parameter `"$1"` and not the global variable `"$file"`. In this case, it doesn't matter, but you should avoid global variables as a matter of basic programming hygiene. – tripleee Jul 31 '14 at 09:01
  • `rm` can remove multiple files. When the target is not a directory, the `-r` option is useless. So `rm -f /home/scanning/finloon/combine.pdf /home/scanning/finloon/workfile.pdf` (possibly with a shell expression to avoid repeating the path; `/home/scanning/finloon/{workfil,combin}e.pdf`). – tripleee Jul 31 '14 at 09:03
  • @triplee thanks! when I do echo "'$setting'" I can see there's a ` in front of the text. I'll try to remove that. also I'll try to edit the script so it uses local parameters. As I'm just starting to learn scripting in Linux I'd better learn it right ;-) – mechuccio Jul 31 '14 at 09:07
  • That's part of the `echo` just to help you see if there is any leading whitespace. – tripleee Jul 31 '14 at 09:08
  • Does a hex dump of the settings file reveal anything unexpected? – tripleee Jul 31 '14 at 09:08
  • Ah ok, well then it that case there's nothing more than yes in the file. I thoufht maybe some hidden character but nothing to find in the file but the word yes. – mechuccio Jul 31 '14 at 09:14
  • If I open the setting.txt file with a hex editor the value seems to be 79 65 73 0d 0a, or in readable text yes.. – mechuccio Jul 31 '14 at 09:16
  • I used hex editor to remove the last two characters, the filesize is now 3 byte but still the same problem. My if statement won't recocnise the variables value to be yes. – mechuccio Jul 31 '14 at 09:30
  • In addition, I also tried using wildcards but even that doesn't help. I really can't figure out whats wrong with the variable or my if statement. – mechuccio Jul 31 '14 at 10:11
  • Ok, so using a case statement solved it, I don't know why an if statement won't work in this case but for my purposes a case statement is as good. – mechuccio Jul 31 '14 at 10:34
  • The DOS carriage return (0x0d) would have caused the original problem. Maybe you never did manage to remove it entirely. – tripleee Jul 31 '14 at 12:37
  • Ok, thanks for all the help so far, I've managed to finish the script in another way, I read my file with filenames into an array and use that array to feed pdftk filenames, this works perfectly, in this way I don't need to work with settings etc. I just needed to rethink the process. – mechuccio Aug 08 '14 at 12:48

0 Answers0