Visit The New, Modern Unix Linux Community

For loop in bash shell

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers For loop in bash shell
# 1  
For loop in bash shell


I am using a for loop to manipulate files data_1.txt through data_100.txt. The for-loop is set up like this:

for i in {1..100}; do cut -f1 data_$i.txt > output$i.txt

I get the following error message when I run the code:

cannot open `data.txt' for reading: No such file or directory

The code seems to run fine though if I take out the "_" sign in the filenames and rename them data1.txt through data1000.txt. Do you know why I'm getting the error and how do I go about fixing it?
# 2  
Just enclose the file name variable in double quotes as below:

for i in {1..100}; do cut -f1 "data_$i.txt" > "output$i.txt"

This User Gave Thanks to hiten.r.chauhan For This Post:
# 3  
This does not seem to fix the issue.
# 4  
Missing double quotes is definitely not the reason for that error.

Verify if you are running this script inside the directory where you indeed have these files.

Also perform following changes:
for i in {1..100}
        [ -f "data_$i.txt" ] && cut -d' ' -f1 "data_$i.txt" > "output$i.txt"

# 5  
Adding quotes won't make any difference for this code snippet. There is no way that data_$i.txt will ever expand to data.txt, although it might appear that way if $i contained a backspace character. (But, of course i won't contain a backspace in the loop you listed.)

Please show us the rest of the script or at least add set -xv to the start of your script to figure out what command in your script is actually failing.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #1
Difficulty: Medium
The UNIX and Linux Forums ( was founded in 1985.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Shell loop - Help !

Dear all Linux lover, I am a new learner to Bash Shell script and I would like to writing a script to to repeat my script. This mean I would like to have multiple same of result after running the .sh. ####### TIMES_NO=0 echo -n "Please enter the number for times to repeat ?" read... (10 Replies)
Discussion started by: Rocky888
10 Replies

2. Shell Programming and Scripting

"Command not found" doing a while loop in bash/shell

i=0 numberofproducts=${#urls} #gets number of entries in array called "urls" numberofproductsminusone=`expr $numberofproducts - 1` #-subtract by one while do wget ${urls} i=$(( $i + 1 )) sleep 10 done I'm getting an error ./scrape: line 22: [0: command not found that... (3 Replies)
Discussion started by: phpchick
3 Replies

3. Shell Programming and Scripting

Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.

for (( i=1; i<=3; i++ )); do for (( j=1; j<=3; j++ )); do for (( k=1; k<=3; k++ )); do echo $i$j$k done done done Will the above code work on a BOURNE shell? As far as my understanding is, if I am writing the above code in a file..say and then running it through the terminal using... (7 Replies)
Discussion started by: navienavnav
7 Replies

4. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

7. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

8. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies

9. Shell Programming and Scripting

problem with while loop in BASH shell

I have file named script1 as follows: #!/bin/bash count="0" echo "hello" echo "$count" while do echo "$count" count=`expr $count + 1` done ----------- when I run it, I get ./script1: line 9: syntax error near unexpected token `done' ./script1: line 9: `done' I... (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

Featured Tech Videos