• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Unix Shell - printf unknown option error

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28133
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic