Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unix script error

Posted on 2013-05-21
5
Medium Priority
?
673 Views
Last Modified: 2013-05-23
I have a unix script (validint.bash) that continues to give the following error after I type the file name and following input...

   sh-3.2# bash validint.bash 123.45

Then I get this error message...

   : command not found 3:
   'alidint.bash: line 4: syntax error near unexpected token `{
   'alidint.bash: line 4: `function validint() {

I attached the script below in a text (.rtf) file. Please help. Thanks.
unix-script.rtf
0
Comment
Question by:Tech_20
  • 2
  • 2
5 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 1332 total points
ID: 39184487
Don't use spaces around "=" in variable assignments!

number="$1"; min="$2"; max="$3"´
...
testvalue="${number#?}"
...
testvalue="$number"
...
nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"

The space following "#" in the shebang line is also wrong:

#!/bin/sh
0
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 668 total points
ID: 39186047
woolmilkporc is correct in identifying spaces that should be removed, but that is not your immediate problem.
You immediate problem is that your file has DOS-style line endings - did you prepare it using Notepad?
DOS (and Windows) text files end lines with Carriage-return Line-feed whereas under Linux or any Unix system, text files need to end to end lines with Newline (actually the same character as Line-feed but we call it Newline because of its different behavior).
I can reproduce your output by saving your file DOS-style, but on my system bash highlights that the problem is with Carriage-return(Cr)
08:02:04$ bash ee101.sh 123.45
ee101.sh: line 3: $'\r': command not found
ee101.sh: line 4: syntax error near unexpected token `$'{\r''
'e101.sh: line 4: `function validint() {
08:02:06$ 

Open in new window

Your system treated Cr as a regular character, hence the references to alidint.bash.
To fix your problem, convert your file with dos2unix or re-code it under Linux with vim or some other native editor.
0
 

Author Comment

by:Tech_20
ID: 39189224
Both of you have great points. I made the changes woolmilkporc suggested. I also transferred the code from Sublime2 to MacVim after experiencing similar errors and it worked. I had another script "validFloat2.bash" referencing the previous script named "validint2.bash" and it worked but I also received this error message.

input
sh-3.2# bash validFloat.bash 1234.56

output
validint2.bash: line 12: [: missing `]'
validint2.bash: line 19: [: missing `]'
validFloat.bash: line 32: [: fractionalPart: unary operator expected
1234.56 is a valid floating-point value

Any other suggestions.
validint2.rtf
validFloat2.rtf
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 1332 total points
ID: 39189239
You're missing a  space in front of the closing square bracket here (first script):

if [ -z $number ] ; then
..
if [ "${number%${number#?}}" = "-" ] ; then
...

and here (second script):

if [ "fractionalPart" != "" ] ; then
...

and it seems that you still have carriage return characters at the line ends!
0
 

Author Closing Comment

by:Tech_20
ID: 39192587
Thanks. The comments on variable assignments, carriage returns and choice of code editor helped!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question