LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-22-2014, 08:31 PM   #1
budgie26
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Rep: Reputation: Disabled
convert for each loop from csh to bash


I have a csh script I'm trying to convert to bash but I'm having problems with getting a for each loop to work properly. The loop is within an if statement:

Code:
if [ -e $file_list ]; then
   while read -r i; do
       mkdir $dir/file_$i
   done < $file_list
else
   :
fi
If the $frame_list file exists, then a directory needs to be created for each line in the file. The file has the directory name on each line:

2654
2655
2656

I've tried changing the NFS format to newline, but can't get the for each loop to move beyond the first line (ie. 2654).

This is probably a simple fix, but I can't work it out. Any advice would be appreciated!
 
Old 06-22-2014, 08:45 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

it looks to me like your script should work. Can you confirm that the input file is a normal text file? Also, is the directory really being created? It would be best to try to reduce this to the simplest possible script that still has the problem. Eg, get rid of the "if" statment and variables. Eg something like

Code:
#!/bin/bash
while read dir ;do 
  echo "dir is $dir"
done < foo.txt
Evo2.
 
Old 06-22-2014, 09:01 PM   #3
budgie26
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
I've recreated the input file in emacs, so it should be a normal text file (I just typed in the dir names and pressed enter after each one). I've also tested the loop by its self in a test script and I still get the same problem: only the first dir gets created.
 
Old 06-22-2014, 09:11 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

you need to debug your script... try using "set -x" at the top of the script.

Evo2.
 
Old 06-23-2014, 12:29 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
The "usual" reason is that something eats up the stdin inside the loop. Actually you didn't show us the real script, therefore there can be something next to the mkdir command. The solution is something like this:
Code:
while read dir ;do 
  echo "" | ( your commands )
done < foo.txt
but in any case you can insert set -xv at the beginning of the script to see what's happening.
 
Old 06-23-2014, 01:29 AM   #6
budgie26
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Found what the problem is. The last line of the input file needs have a 'return' after the last line. Something so simple can be so frustrating!! Thanks for all your help!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
Use csh instead of bash? sgware Fedora 2 11-16-2007 05:06 AM
convert .bat / .csh file to .sh files massoo Programming 1 03-06-2006 06:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration