0

I'm new to bash, I'm just trying to pick in up in my free time. I'm writing a simple script that prints out the filename I supply as many times as there are characters in the filename, and then positive or negative based on the second argument. I'm getting an unexpected end of file error on this script, and I'm not sure why.

#!/bin/bash

FILENAME=$1
NUMBER=$2

COUNT=0;
STR=${#FILENAME}

while ($COUNT < $STR){
echo $FILENAME
$COUNT++
}

if ($NUMBER < 0)
echo "Negative"
if ($NUMBER > 0)
echo "Positive"

exit

I'm executing with

./script1.sh hello 2

and I'm expecting the output to be

hello
hello
hello
hello
hello
Positive

If anyone could shed some light as to what's going on with the error, that'd be great.

edit: forgot to add the second condition to add to the COUNT variable, and now I'm getting an error:

line 9: syntax error near unexpected token `{'
line 9: `while ($COUNT < $STR){'
1
  • 1
    there are few errors here. I suggest you to use shellcheck.net to get an explanation of each one of them. Commented Apr 4, 2015 at 23:22

1 Answer 1

1

Basically you are confusing Bash's syntax with some other language.

In Bash, if's syntax (if not using else) is if condition;then commands;fi. Your code does not have then and fi. Also, the condition should be (( $NUMBER > 0 )).

The syntax of while is while condition;do commands;done, and your code is also wrong.

Sign up to request clarification or add additional context in comments.

3 Comments

So it would read if ($NUMBER -lt 0) echo "Negative" fi
I'd say that "some other language" is C, or something in the C family.
Also, would be better to use a normal for loop: for ((i=1; i<=${#name}; i++)) instead of keeping track of a counter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.