LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 03-25-2013, 11:45 AM   #1
trainee
Member
 
Registered: Dec 2004
Distribution: Slackware
Posts: 142

Rep: Reputation: 16
bash question - syntax error near unexpected token


Hi,
I wrote a script like this
Code:
for i in 2 3 4 5 6 7 8 9 10 do 
/usr/bin/pdfunite test.pdf chap[i].pdf test.pdf 
done
and put it in a file testrun.sh

When I run
#./testrun.sh
I got the error

./testrun.sh: line 3: syntax error near unexpected token `/usr/bin/pdfunite'
./testrun.sh: line 3: ` /usr/bin/pdfunite test.pdf chap$i.pdf test.pdf'

Do you know how to fix the error?

Thanks in advance.
 
Old 03-25-2013, 11:55 AM   #2
Ahau
Member
 
Registered: Jun 2011
Location: USA
Distribution: Porteus, Slackware
Posts: 58

Rep: Reputation: 19
try a semicolon after your last list item (before do, else bash thinks 'do' is one of the items to perform the for loop on), and another semicolon on the line before done. You also need to let bash know [i] is your variable, with $i.

Try this:
Code:
for i in 2 3 4 5 6 7 8 9 10; do 
/usr/bin/pdfunite test.pdf chap[$i].pdf test.pdf;
done
 
1 members found this post helpful.
Old 03-25-2013, 11:56 AM   #3
Mike_M
Member
 
Registered: Mar 2011
Location: California
Distribution: Slackware
Posts: 116

Rep: Reputation: 50
On line 1 you either need a semicolon before " do", or "do" needs to be on its own line.

Examples:

Code:
#!/bin/sh
for i in 2 3 4 5 6 7 8 9 10; do 
   /usr/bin/pdfunite test.pdf chap[$i].pdf test.pdf 
done

Code:
#!/bin/sh
for i in 2 3 4 5 6 7 8 9 10
do 
   /usr/bin/pdfunite test.pdf chap[$i].pdf test.pdf 
done

Last edited by Mike_M; 03-25-2013 at 11:58 AM. Reason: I missed the $, too. Thanks colucix.
 
1 members found this post helpful.
Old 03-25-2013, 11:56 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You missed a semicolon before the do keyword (plus a $ sign to evaluate the i variable inside the loop)
Code:
for i in 2 3 4 5 6 7 8 9 10;  do 
  /usr/bin/pdfunite test.pdf chap[$i].pdf test.pdf
done
or in alternative:
Code:
for i in 2 3 4 5 6 7 8 9 10
do
  /usr/bin/pdfunite test.pdf chap[$i].pdf test.pdf
done
Edit: too late!
 
Old 03-25-2013, 12:13 PM   #5
trainee
Member
 
Registered: Dec 2004
Distribution: Slackware
Posts: 142

Original Poster
Rep: Reputation: 16
Thanks for the tips. I fixed the error. Now something else comes up.

I run the script and got this message:
Code:
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
Syntax Error: Catalog object is wrong type (null)
and this is the output of ls-al
Code:
drwxr-xr-x  2 dat root   4096 Mar 25 12:11 .
drwx--x--x 27 dat root   4096 Mar 25 12:09 ..
-rw-r--r--  1 dat root 394200 Mar 25 10:40 back.pdf
-rw-r--r--  1 dat root 456464 Mar 25 10:40 chap1.pdf
-rw-r--r--  1 dat root 792229 Mar 25 10:40 chap10.pdf
-rw-r--r--  1 dat root 645207 Mar 25 10:40 chap2.pdf
-rw-r--r--  1 dat root 566813 Mar 25 10:40 chap3.pdf
-rw-r--r--  1 dat root 887743 Mar 25 10:40 chap4.pdf
-rw-r--r--  1 dat root 463453 Mar 25 10:40 chap5.pdf
-rw-r--r--  1 dat root 422404 Mar 25 10:40 chap6.pdf
-rw-r--r--  1 dat root 833556 Mar 25 10:40 chap7.pdf
-rw-r--r--  1 dat root 752984 Mar 25 10:40 chap8.pdf
-rw-r--r--  1 dat root 503119 Mar 25 10:40 chap9.pdf
-rw-r--r--  1 dat root 199399 Mar 25 10:40 front.pdf
-rw-r--r--  1 dat root 646077 Mar 25 12:11 test.pdf
-rwxr-xr-x  1 dat root    100 Mar 25 12:09 testrun.sh
and this is the code of the shell
Code:
#!/bin/bash
for i in 2  3 4 5 6 7 8 9 10
do
        /usr/bin/pdfunite test.pdf chap[$i].pdf test.pdf
done
I tried for both chap$i.pdf and chap[$i].pdf, same error.
 
Old 03-25-2013, 12:26 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
According to the pdfunite man page you can try to do it without a loop and by means of extended brace expansion:
Code:
/usr/bin/pdfunite chap{1..10}.pdf test.pdf
Anyway the "Catalog object is wrong type (null)" error is not from bash, but maybe from pdfunite itself. Therefore maybe something is wrong with the PDF files?

Last edited by colucix; 03-25-2013 at 12:33 PM.
 
2 members found this post helpful.
Old 03-25-2013, 12:28 PM   #7
trainee
Member
 
Registered: Dec 2004
Distribution: Slackware
Posts: 142

Original Poster
Rep: Reputation: 16
As mentioned in the earlier post, I tried that and got the same error.
 
Old 03-25-2013, 12:34 PM   #8
Mike_M
Member
 
Registered: Mar 2011
Location: California
Distribution: Slackware
Posts: 116

Rep: Reputation: 50
There may be problems with your PDF files.

Also, pdfunite may not work when your destination PDF file is the same as one of your source PDF files.
 
1 members found this post helpful.
Old 03-25-2013, 12:45 PM   #9
trainee
Member
 
Registered: Dec 2004
Distribution: Slackware
Posts: 142

Original Poster
Rep: Reputation: 16
Yes, Mike_M. You are right. Is there anyway I can type a command that listed chap1.pdf chap2.pdf til chap10.pdf without having to spell them out all. Thanks.
 
Old 03-25-2013, 01:13 PM   #10
Mike_M
Member
 
Registered: Mar 2011
Location: California
Distribution: Slackware
Posts: 116

Rep: Reputation: 50
I'm sure there are many ways to do this, but the first that pops in to mind would be using concatenation to build your final command. Something along the lines of the following:


Code:
#!/bin/sh

PDFUNITE='/usr/bin/pdfunite'
OUTFILE='test.pdf'
PDFUNITE_ARGS=""

for i in {1..10}
do
   PDFUNITE_ARGS="$PDFUNITE_ARGS chap$i.pdf"
done

$PDFUNITE $PDFUNITE_ARGS $OUTFILE
[edit]
See colucix' answer in post #6 for the obviously better solution

Last edited by Mike_M; 03-25-2013 at 02:36 PM.
 
1 members found this post helpful.
Old 03-25-2013, 01:44 PM   #11
trainee
Member
 
Registered: Dec 2004
Distribution: Slackware
Posts: 142

Original Poster
Rep: Reputation: 16
Very nice. I should have thought of that. Thank you very much.
 
Old 03-25-2013, 01:53 PM   #12
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,653
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
The code previously shown by colucix does the same in a much more elegant way, provided that you use a modern shell, like Bash (most likely) or Zsh:
Code:
/usr/bin/pdfunite chap{1..10}.pdf test.pdf
The shell will automatically expand chap{1..10}.pdf to all filenames it can find that matches the expression. No need for loops at all.
 
1 members found this post helpful.
Old 03-26-2013, 05:04 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Quote:
Originally Posted by Mike_M View Post
I'm sure there are many ways to do this, but the first that pops in to mind would be using concatenation to build your final command. Something along the lines of the following:


Code:
#!/bin/sh

PDFUNITE='/usr/bin/pdfunite'
OUTFILE='test.pdf'
PDFUNITE_ARGS=""

for i in {1..10}
do
   PDFUNITE_ARGS="$PDFUNITE_ARGS chap$i.pdf"
done

$PDFUNITE $PDFUNITE_ARGS $OUTFILE
The idea may be ok, but the execution is wrong. You should never use simple, scalar variables for lists of things. It's very difficult to safely re-split it back into the individual entries. You should always use arrays instead for this purpose.

(But note that arrays are not supported by posix-based /bin/sh scripts. Always use /bin/bash or another shell that has explicit array ability.)

Code:
#!/bin/bash

outfile='test.pdf'
pdfunite_args=""

for i in {1..10}; do
    pdfunite_args+=( "chap$i.pdf" )
done

/usr/bin/pdfunite "${pdfunite_args[@]}" "$outfile"

Since environment variables are generally all upper-case, it's recommended practice to keep your own user variables in lower-case or mixed-case to help differentiate them.

Scripting With Style



But finally, since the chapter files are already there, you can probably just use simple globbing instead.

Code:
/usr/bin/pdfunite chap[1-9].pdf chap10.pdf "$outfile"
This is basically the same as given by colucix, but generally better, since globbing only does pattern matching on existing files, while brace expansion attempts to generate a list of all possible filenames for the pattern. Any generated names that don't have corresponding files matching them would result in "file not found" errors.

Notice how I had to separate out chap10.pdf though, since globbing produces lists in shell sorting order. For this reason I highly recommend getting into the habit of fully zero-padding all numbers in your filenames, which will let the shell automatically sort them properly.

Last edited by David the H.; 03-26-2013 at 05:08 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] -bash: syntax error near unexpected token AmirJamez Ubuntu 3 05-01-2012 08:48 AM
[SOLVED] bash: syntax error near unexpected token `(' rc5gd Programming 6 07-13-2010 09:54 PM
[SOLVED] bash: syntax error near unexpected token `else' lucmove Programming 9 05-10-2010 10:03 PM
bash: syntax error near unexpected token `(' Folklore Linux - Newbie 1 05-02-2010 04:35 AM
-bash: syntax error near unexpected token ty1on Linux - Newbie 3 08-26-2009 03:19 PM


All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration