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?
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"
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"
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"
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