foreach loop in unix script

Hi all,

I have a script which searches for all sql files in the current directory and replaces all sql files with an underscore with a dash. The next part I need to do is record the number of changes made (underscore to dash) and display this value (e.g.2). This is what I have so far;

find / -name "*.sql" | sed -e  's/_/-/g'

foreach f ('*-.sql')
    set n = 0
    n = `expr $n + 1`
    echo "Number of conversions is $n"
end

Now this returns '0'. I need it to increment the value of n depending on the number of conversions. Any ideas?

Thanks

Hello friend ,
You need to modify your looping part as below

for i in $(ls *-.sql)
do
        ((n++))
done
echo "Number of conversions is $n"

Thank you for the quick reply, appreciated.

I get a 'Badly placed ()'s.' error when running the code above. Can I ask why it is 'n++' as opposed to 'n = n +1'?

Theoritically

n=`expr $n + 1`
((n++))
let "n=n+1"

all are same .

Couple of things you need to check .
Are you using bash as your shell while running the script .Because some of the above mentioned syntaxes are native to bash only .
If you are using some older shells you may try using something like below .

for i in `ls *-.sql`
do
        n=`expr $n + 1`
done
echo "Number of conversions is $n"

This is running with csh, should have mentioned that before:$

I've also tried to set n to 0 followed by @n = n + 1, but this doesn't work either

can you add an #!/bin/bash at the very beginning of your shell script and run .
That would run the code in bash only .

Changed to bash, same Badly placed ()'s error appears

This is the code running;

#!/usr/bin/bash

echo WARNING : THIS DATA HAS BEEN SQLED

#finds all sql files in the home directory and replace sql files containing an underscore with a dash

find / -name "*.sql" | sed -e  's/_/-/g'

#records the number of changes made and outputs them to the user

for i in $ (ls *-.sql)
do
        set n = 0
        n++
done
    echo "Number of conversions is $n"

In C shell when you are incrementing a variable the syntax is

@n = $n + 1

istead of

@n = n + 1

as you put in your code .

---------- Post updated at 06:31 PM ---------- Previous update was at 06:29 PM ----------

Below is the modified code

#!/usr/bin/bash

echo WARNING : THIS DATA HAS BEEN SQLED

#finds all sql files in the home directory and replace sql files containing an underscore with a dash

find / -name "*.sql" | sed -e 's/_/-/g'

#records the number of changes made and outputs them to the user

for i in `ls *-.sql`
do
((n++))
done
echo "Number of conversions is $n"

Thanks for that info.

Ran the above code and the following messages appeared;

ls: No match.
for: Command not found.
do: Command not found.
n++: Command not found.
done: Command not found.
n: Undefined variable.

i changed to 'ls *-.sql' and the error for ls didn't come up

not sure why the rest of the errors appear

Hello,
wouldn't it be shorter?

n=`ls *-.sql|wc -l`

bash wont throw these type of errors .
Are you sure you are running the script with appropriate bash .
chechk

which bash

which bash returns;

/bin/bash

is this correct?

Then why are you using #!/usr/bin/bash at the beginning of the script . :frowning:
please modify the code .
add #!/bin/bash at the beginning .

I didn't know that would make a difference.

Ran it again, same errors appear

One last try , can you run exactly the below from a shell script .

#!/bin/bash

bash
echo WARNING : THIS DATA HAS BEEN SQLED

#finds all sql files in the home directory and replace sql files containing an underscore with a dash

find / -name "*.sql" | sed -e 's/_/-/g'

#records the number of changes made and outputs them to the user

for i in `ls *-.sql`
do
((n++))
done
echo "Number of conversions is $n"

Is something specific supposed to be entered after the bash? (above the warning)

I managed to increment the count of f using the below code. However, the count that it is displaying isn't correct; it is echoing 1. There has been more than one sql file replaced with a dash. Any ideas?

#records the number of changes made and outputs them to the user

set sum  = 0
foreach f ("-.sql")
     @ sum++
end
    echo "Number of conversions is " $sum

If you have any doubt in your coursework on question 1b , you can always consult your lecturer or post the question to our university portal (moodle).