LinuxQuestions.org
Review your favorite Linux distribution.
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 09-02-2006, 06:48 AM   #1
Locura
Member
 
Registered: May 2003
Distribution: Ubuntu 6.10 SE, Mac OS 10.4.8
Posts: 370

Rep: Reputation: 30
bash scripting question


Hello all, I'm just getting into scripting and I'm tring to write a script that will operate on each file in a directory, but I'm a little lost on the programming scructure necessary to do so. Basically I want to take each file, check the extension using an if/then statement, then run a command on the file conditionally based on the results of the if/then. Can someone point me in the right direction? I'm clear on how to do an if/then, it's just the part about operating on each file where I'm fuzzy. I tried accepting it as an argument, and then doing a for file in $1, but then I realized that filenames with spaces in them would be affected, so I obviously need a better way of doing this. Any help would be much appreciated.
 
Old 09-02-2006, 07:02 AM   #2
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

Rep: Reputation: 57
Code:
#!/bin/sh
# this is the line you're interested in
IFS='
'

for $f in `ls`; do
  echo $f
done

exit 0
Not sure if that is the "right" way or not but it's worked for me
 
Old 09-02-2006, 07:13 AM   #3
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Quote:
Originally Posted by Locura
I tried accepting it as an argument, and then doing a for file in $1, but then I realized that filenames with spaces in them would be affected.
The problem with spaces in filenames can be avoided simply by putting double quotes around the variable "$1".
It sounds to me like you need to investigate the command find. These commands checks whether my website validates by running a perl script called validate on every .html file under a particular folder
Code:
for page in $(find /home/andy/save/src/htdocs -name "*.html")
do validate "$page" &>/dev/null || echo "$page is not valid html"
done
This find command changes the permissions on only the files (not the folders) under a particular folder
Code:
find /usr/share/AbiSuite-2.4 -type f -exec chmod 644 {} \;
 
Old 09-02-2006, 08:17 AM   #4
Locura
Member
 
Registered: May 2003
Distribution: Ubuntu 6.10 SE, Mac OS 10.4.8
Posts: 370

Original Poster
Rep: Reputation: 30
Thank you both. Andrew, I think I'm on the right track. I'm using the following for statement to look for .txt files in the current working directory:

for file in $(find ~+ -name "*.txt")

However, I'm still running into problems because files that have spaces in them are being split up.
 
Old 09-02-2006, 09:03 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
That's where the following comes in handy, as proposed by cs-cam:
Code:
IFS=$'\n'
 
Old 09-02-2006, 09:13 AM   #6
Locura
Member
 
Registered: May 2003
Distribution: Ubuntu 6.10 SE, Mac OS 10.4.8
Posts: 370

Original Poster
Rep: Reputation: 30
D'oh! I had commented out that line. My bad!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash scripting question nitroid Programming 3 04-13-2006 07:08 AM
A Bash Scripting Question steve_f60 Linux - Newbie 4 05-26-2005 06:57 PM
bash scripting question Andy@DP Programming 4 04-13-2004 05:06 PM
bash scripting question brian0918 Linux - Newbie 7 06-26-2003 03:48 PM
Bash Scripting Question RefriedBean Programming 6 09-11-2002 09:06 AM

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

All times are GMT -5. The time now is 07:05 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