0

I'm trying to execute this simple script in solaris. I want to sort(numeric) the filenames of the files in source directory and copy the file one by one to another directory. And, I want to print a message after copying every 100 files.

#!/bin/bash

count=0

for i in `ls | sort -n`
do 
 cp $i ../target
 count = $((count+1))
 if[ $count%100 -eq 0 ] 
 then
    echo $count files copied
    sleep 1
 fi

done

this is not working. I tried different things after searching in net.
I get errors like these -

syntax error at line 8: '(' unexpected.
syntax error at line 10: 'then' unexpected.
syntax error at line 13: 'fi' unexpected etc.

What is the problem with this script?

bash version - GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)

3 Answers 3

2

The basic problem with the script is spacing. You have spaces where you shouldn't have them:

(wrong)  count = $((count+1))
(right)  count=$((count+1))
(better) ((count++))

and you're missing spaces where you need them:

(wrong)  if[ $count%100 -eq 0 ]
(right)  if [ $((count % 100)) -eq 0 ]
(better) if ((count % 100 == 0))

count = $((count+1)) tries to run the command count passing it two arguments, = and the value of count+1. if[ ... tries to run the command if[ because [ is a valid word character; it doesn't automatically start a new token.

Having said all that, I'm puzzled by the unexpected ( error message. Could your bash be too old to recognize $(( syntax? Possibly. It's a very old bash.

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

2 Comments

Thanks. Got these errors - count++: not found, syntax error at line 8: `count=$' unexpected etc.. For some reason these things are not working. maybe this is very old bash?
@KarthiPk: It's quite an old bash. I suspect it doesn't have arithmetic expansion. Does let count++ work?
0

count=$((count+1))

if [ `echo $count % 100 | bc` -eq 0 ]

Make these corrections.

Edit: Please try

count=`expr $count + 1`

2 Comments

Thanks. count=$((count+1)) throws this error syntax error at line 8: count=$' unexpected. if worked.
count=`expr $count + 1` and count=`echo "$count + 1" | bc` posted by idfah both worked.
0

I see a few errors here. First, you need double quotes around $i in case they have special characters.

Second, you shouldn't ever use

for i in `ls | sort -n`

Instead, try the following

ls -1 | sort -n | while read i

Third, change your if statement to

if ((count%5 == 0))

The (( syntax is bash is made just for integer math.

Fourth, change your counter increment to

((count++))

This is more concise. Also, the space in your version may break things. Remember, spaces matter.

4 Comments

Thanks. while worked. if ((count%5 == 0)) and ((count++)) gives these errors - count%5: not found and count++: not found
Hmm, probably a really old version of bash. You could try count=`echo "$count + 1" | bc` and if [ `echo "$count % 100 | bc` -eq 0]
Strange, I think arithmetic should work in bash 3. Do you have #!/bin/bash or #!/bin/sh at the top of your script? Do you have posix mode on? Check with set -o
I used to use solaris 10 and don't recall having problems with ((

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.