foreach loop in unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foreach loop in unix script
# 8  
Old 03-04-2012
In C shell when you are incrementing a variable the syntax is
Code:
@n = $n + 1

istead of
Code:
@n = n + 1

as you put in your code .

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

Quote:
Originally Posted by shawi
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"
Below is the modified code

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"

# 9  
Old 03-04-2012
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
# 10  
Old 03-04-2012
Hello,
wouldn't it be shorter?

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

# 11  
Old 03-04-2012
Quote:
Originally Posted by shawi
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
bash wont throw these type of errors .
Are you sure you are running the script with appropriate bash .
chechk
Code:
which bash

# 12  
Old 03-04-2012
which bash returns;

/bin/bash

is this correct?
# 13  
Old 03-04-2012
Then why are you using #!/usr/bin/bash at the beginning of the script . Smilie
please modify the code .
add #!/bin/bash at the beginning .
# 14  
Old 03-04-2012
I didn't know that would make a difference.

Ran it again, same errors appear
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