简体   繁体   中英

How to resolve arithmetic syntax error in printf

I am running below code in SunOS as .sh file

#!/usr/bin/bash
#lerning linux scripting 
#
printf "%-5s %-10s %-4s\n" No Name Mark 
printf "%-5s %-10s %-4.2f\n" 1 James 80.1234
printf "%-5s %-10s %-4.2f\n" 2 Sarah 99.8923

But I am getting below error when run above one in SunOS

No    Name       Mark

: arithmetic syntax error.sh[5]: printf: 80.1234
: arithmetic syntax error0.1234
linux_sc.sh[5]: printf: warning: invalid argument of type f
80.12
: arithmetic syntax error.sh[6]: printf: 99.8923
: arithmetic syntax error9.8923
linux_sc.sh[6]: printf: warning: invalid argument of type f
99.89

So yeah, pretty sure you're using a script that has Windows style CRLF line endings, while Unix systems use LF. This happens when you write the file on Windows and don't transfer it to the other computer in a way that translates line endings.

Taking your sample script and running it through unix2dos and then trying to run it gives:

No    Name       Mark

: invalid numberprintf: 80.1234
1     James      0.00
: invalid numberprintf: 99.8923
2     Sarah      0.00

Not exactly the same output, but I'm testing on Linux, not SunOS, so that's to be expected. Close enough, though, to suggest it's the issue.

If you have dos2unix installed on the SunOS box, run your script through it and see if that fixes the problem. Or perl -pi -e 's/\\r//' your.sh is another way of stripping the extraneous carriage returns. Or tr -d '\\r' < your.sh > new.sh or any number of other ways; just depends on what a typical SunOS setup has installed. I haven't used it in years; don't remember off the top of my head.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM