• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unix Shell - printf unknown option error  RSS feed

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I just want to confirm whether OS X 10.9.3 really is a bit of a pigs breakfast.
I have the following code (Korn shell)

typeset -i j=0
#
while (( j <= count ))
do
printf "-- j = %d\n" $j
if [ ${arrayA[j]} == $search_value ]
then
answer=$(( $j + 1 ))
# answer=$j
printf "answer = %d\n" $answer
# exit 0
fi
(( j = j + 1 ))
done
#

I'm getting the following error: (the loop has 28 entries).

answer = 28
/Users/simoningram/unix/bin/linear_search_old.ksh[63]: printf: -- j: unknown option
Usage: printf [ options ] format [string ...]
/Users/simoningram/unix/bin/linear_search_old.ksh[64]: [: argument expected
29
answer = 28

I am concerned about the "printf unknown option" error, since IMHO there is nothing wrong with the syntax (the second printf is ok)

This just shows details about the installation:
/Users/simoningram/unix/bin => uname -a
Darwin Simons-iMac.local 13.2.0 Darwin Kernel Version 13.2.0: Thu Apr 17 23:03:13 PDT 2014; root:xnu-2422.100.13~1/RELEASE_X86_64 x86_64

I have seen posts about printf not working with find, but this is just the standard shell command.
I'm going a bit blind on this one. Am I missing something?
 
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the ksh docs I found raise an unanswered question as to command-line syntax on printf.

Apparently what the shell is doing is pre-processing the arguments, then invoking printf as though you had coded it thus:


Assuming a value of 7 for j. Note that in this form, the "--" look like a command switch indicator.

You might try coding it this way:



The ksh docs describe the "-f" flag as meaning "format follows", then immediately provide tons of examples where they don't use it.

 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for trying Tim,

Alas I'm just getting an additional error message:

/Users/simoningram/unix/bin/linear_search_old.ksh[64]: printf: -f: unknown option
/Users/simoningram/unix/bin/linear_search_old.ksh[64]: printf: -- j: unknown option
Usage: printf [ options ] format [string ...]

The odd thing is I have used the printf in the way described. Sometimes it works sometimes it doesn't.
I'm beginning to think the answer to my first question is simply - yes!

Best Regards
Simon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!