LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-30-2013, 03:02 PM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,604

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477

Also gnu!xargs's -0 option can be useful.
 
Old 05-07-2013, 01:08 AM   #17
hDUQUE
Member
 
Registered: Jan 2007
Posts: 76

Original Poster
Rep: Reputation: 15
thank you guys ... you are the best !!!
 
Old 05-07-2013, 02:42 PM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Quote:
Originally Posted by unSpawn View Post
There are a few common problems in scripting that are easy avoidable if you keep to simple rules, and one of them is using "for" instead of "while" loops. If you don't want to take it it from me, David The H. or other LQ members well-versed in scripting see http://mywiki.wooledge.org/DontReadLinesWithFor

To clarify, now that I've stumbled upon this thread...

A for loop operates on a series of word tokens, like this:

Code:
for word in word1 word2 word3 word4; do
   echo "$word"
done
The wordlist can be generated in whatever way you want. You can add them manually, and/or use brace expansion, or an array, or even a command substitution, if you know what you're doing.

The key is to understand that command or simple variable substitutions will always be subject to shell word-splitting before being used. That means you can have either a list of simple words (if you leave the quotes off), or one big single entry, if you quote it. There's no way to split the expansion in one place while keeping it unsplit in another, identical place (e.g. spaces within filenames cannot be distinguished from spaces between them). The link provided above shows how it works.

One of the most common is to use globbing file matching expansion. This is safe, even if the files have whitespace in them, because globbing expansion doesn't happen until after word-splitting is finished. Globs will always expand into lists of complete filename tokens.

As a final warning, however, for loops do always have a danger of running into system ARG_MAX limits.

For almost all other uses, when reading from commands and files, you should use a while+read loop, which allows you to cleanly control how the input is delimited and broken up. Of course a while loop does have a bit of extra complexity involved to it, particularly when delimiting filenames, but it's something you need to learn if you're ever going to be a competent scripter.

One final, kind of hybrid solution, is to somehow split your input into an array first. The "${array[@]}" expansion pattern, when quoted, is perfectly safe for use in a for loop. The only problem is that it usually takes a while loop to safely set the array contents in the first place. The new bash mapfile built-in can make it much easier in some cases, however, at least if the input is delimited by newlines. You can safely use globbing patterns to set an array of filenames too.

Last edited by David the H.; 05-07-2013 at 02:49 PM.
 
1 members found this post helpful.
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue in running commands in expect script from shell script yadvinder Programming 0 05-31-2012 04:07 AM
Shell Script issue elmo219 Linux - Newbie 4 03-09-2012 10:08 AM
Issue with shell script zaeem Linux - Networking 10 01-06-2011 09:56 AM
Shell script issue suvra82002 Linux - Enterprise 23 07-26-2008 02:02 PM
issue with shell script chupacabra Linux - General 3 10-18-2002 08:12 PM


All times are GMT -5. The time now is 12:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration