foreach loop in unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foreach loop in unix script
# 1  
Old 03-04-2012
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;
Code:
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

Last edited by Franklin52; 03-05-2012 at 03:16 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-04-2012
Hello friend ,
You need to modify your looping part as below

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

This User Gave Thanks to codemaniac For This Post:
# 3  
Old 03-04-2012
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'?
# 4  
Old 03-04-2012
Theoritically
Code:
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 .

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


Last edited by codemaniac; 03-04-2012 at 08:43 AM.. Reason: removing additional spaces
# 5  
Old 03-04-2012
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
# 6  
Old 03-04-2012
can you add an #!/bin/bash at the very beginning of your shell script and run .
That would run the code in bash only .
# 7  
Old 03-04-2012
Changed to bash, same Badly placed ()'s error appears

This is the code running;
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
        set n = 0
        n++
done
    echo "Number of conversions is $n"


Last edited by Franklin52; 03-05-2012 at 03:16 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach loop with two variables

I need to put together a script that will take the contents of two different files (database name and database owner) and put them in two variables within a line: foreach x (`cat /local/hd3/dba/tools/build_db_scripts/dbs`) foreach z (`cat /local/hd3/dba/tools/build_db_scripts/dbas`)... (6 Replies)
Discussion started by: deneuve01
6 Replies

2. UNIX for Dummies Questions & Answers

foreach loop in csh

Hi everyone I'm new to unix and encountered a small problem i couldnt find out a reason why it doesn't work..please help.. in my csh script when i tried to use the foreach loop like this: foreach x ( ls ) echo $x end when i tried to run it, it printed out 'ls' to the std out instead of... (3 Replies)
Discussion started by: ymc1g11
3 Replies

3. Shell Programming and Scripting

foreach loop problem

Dear all, I wrote a script to download files and move files in directories according to their name. Now here is the problem: Both p101 and p360 data download successfully, but when I move them according to the year and month, only p101 data can be placed at the right location, p360,... (1 Reply)
Discussion started by: handsonzhao
1 Replies

4. UNIX for Dummies Questions & Answers

Using the Foreach loop, Needing help

I am trying to make a script for my Counter-Strike: Source servers. What i am wanting it to do is for it to restart each server, the only way i can think of doing this in through for each. Years what i have at the moment. server_start() { START=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f... (5 Replies)
Discussion started by: grahamn95
5 Replies

5. Shell Programming and Scripting

foreach loop working in terminal but not in the script

Hi, I am new here I have used the forums a long time to search for things they are very helpful. I have unfortunately used up all my resources (professors, grad students) and need some help. I have this very simple piece of code (using to isolate the problem) in a csh script: #!/bin/csh... (12 Replies)
Discussion started by: bflinchum
12 Replies

6. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

7. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

8. Shell Programming and Scripting

foreach loop + 2 variables

In a foreach loop, is it possible for the loop to go through 2 arguments instead of one i.e. instead of foreach i (do stuff for i), we have foreach i j(do stuff for i; do stuff for j) I am working under BASH and TCSH shell environments cheers (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

10. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies
Login or Register to Ask a Question