LinuxQuestions.org
Help answer threads with 0 replies.
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 11-04-2008, 03:27 PM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 57
Always the same "space" problem with for !!


Code:
 for each in $(amixer | grep "control" | awk  'BEGIN {FS="control " } { print $2 }' | awk '{ gsub(" ","\\ ") ; print $0 }  ' ) ; do echo "$each" ; done
and one get this:

Code:
Boost',0
'Capture',0
'Capture',1
'Capture',2
'Caller\
ID',0
'Digital',0
'Input\
Source',0
'Input\
Source',1
'Input\
Source',2
'Off-hook',0


it is the same when u have a file :
Code:
for each in $( cat /tmp/listoffiles) ; do ...
then you are always never anything working due to spaces in filenames

no solutions??
 
Old 11-04-2008, 03:30 PM   #2
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
and with | then followed by for
why isnt it working?? I saw it once ... that it is kind of possible?

Code:
 amixer | grep "control" | awk  'BEGIN {FS="control " } { print $2 }' | for each  in  ; do echo "$each" ; done
 
Old 11-04-2008, 03:34 PM   #3
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
not working either


Code:
 amixer | grep "control" | awk  'BEGIN {FS="control " } { print $2 }' | while read each  ; do echo "$each" ; amixer set "$each" 100 ;    done
 
Old 11-04-2008, 04:07 PM   #4
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
I found those information :

Quote:
s the error message translated? I would expect something more like "argument list too long".

First thing (although it won't help you with this exact problem - just a general tip) - you don't need to do this:
Code:

for rpt in `ls myfiles*.txt`

This will often work, but it's not the best way. It works by the backtick syntax running the command "ls myfiles*.txt", and taking the result of that an substituting it as the for loop list. The problem is that if you have a file with a space in the name, it will be split when the output of ls is read, and you will probably get two or more iterations of the loop on non-existing, or (worse) incorrect files.

The good news is, the shell will expand glob patterns for you, so just do this instead:
Code:

for rpt in myfiles*.txt

OK, back to the real problem. The shell has a limit to the number (and total length) of items in an argument list. This is most often encountered when you expend a glob pattern which matches a very large number of files. There are a few solutions. The simplest one when you have multiple commands to use with arguments (as you do) is to use find to identify the files, and then read the output of find into a while loop:
Code:

find . -name "myfiles*.txt" -maxdepth 1 -type f |while read rpt
do
instruction 1 $rpt
instruction 2 $rpt
...
instruction n $rpt
done

The disadvantage here is that the code block of the while loop is executed in a sub-shell. This means that you cannot modify the value variables in your main script inside the loop, and then read the modified values once the while loop is complete.

[/code]#!/bin/bash

counter=0

seq 1 5 | while read n; do
let counter+=1
echo "random number $counter is $RANDOM"
done

echo "after loop, value of counter is: $counter"[/code]

I hope that helps.
that help a lot
 
Old 11-05-2008, 04:17 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

to use a for loop with values containing spaces, you can redefine the $IFS variable (Input Field Separator) to only newline:
Code:
OLD_IFS="$IFS"
IFS='
'
for var in `command_which_returns_values_with_spaces`; do
  ...
done
# or
for var in `cat file_with_lines_containing_spaces`; do
  ...
done
IFS="$OLD_IFS"
Jan
 
Old 11-05-2008, 05:17 PM   #6
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by jan61 View Post
Moin,

to use a for loop with values containing spaces, you can redefine the $IFS variable (Input Field Separator) to only newline:
Code:
OLD_IFS="$IFS"
IFS='
'
for var in `command_which_returns_values_with_spaces`; do
  ...
done
# or
for var in `cat file_with_lines_containing_spaces`; do
  ...
done
IFS="$OLD_IFS"
Jan
Not bad IFS is in let / set exported things?

It is bit complex but can make it. maybe there is already in debian packagtes that do such things easier? odnt know

thanks

that s the first positive and working reply !!
 
Old 11-06-2008, 02:32 AM   #7
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
I don't know why your efforts failed, but this does, in fact, work:

Code:
echo "a filename with spaces" | while read line; do touch "$line"; done
Furthermore, there is no need to use both grep and awk, since awk can do everything grep can (and more, of course).
 
Old 11-06-2008, 08:59 AM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by burschik View Post
I don't know why your efforts failed, but this does, in fact, work:

Code:
echo "a filename with spaces" | while read line; do touch "$line"; done
Try to get the value of $line, when the loop is done. Working with $line within the loop is o.k., but there you are in a subshell (everything following the pipe) - so you are not able to access $line outside.

Code:
F=1
echo -e "2\n3\n4\n" | while read l; do
  F=$l
done
echo $F # will display 1
Jan
 
Old 11-06-2008, 09:37 AM   #9
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
I know that, but the original question wasn't about variable scope, or was it?
 
Old 11-06-2008, 11:15 AM   #10
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by burschik View Post
I know that, but the original question wasn't about variable scope, or was it?
no it wasn't, I was a little bit confused because of the article discussing the subshell effect, which frenchn00b posted 04-11-08 22:07. Of cource the while loop works too to solve the poster's problem.

Jan
 
Old 11-20-2008, 04:01 PM   #11
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
I thought that x0a could make it but still ... not working ...

Code:
!/bin/sh

FS2=$'\x0a'

FILE=""

FILE="$FILE`ls -1 *.smc`$FS2 "
zo=1
for i in "$FILE" ; do

echo "$zo $i"

let zo+=1
echo "$zo"
done
 
  


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
Cedega "Disk space required for the installation exceeds available disk space" Solved Spewdemon LinuxQuestions.org Member Success Stories 1 10-18-2007 07:19 PM
"No space" when creating LFS in USB, What can I solve that problem valpa Linux From Scratch 3 06-20-2007 02:18 PM
Proposed solution for "status" (aka "problem solved") indicator demerson3 LQ Suggestions & Feedback 12 04-08-2006 03:15 PM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 05:51 AM
"no space left on device" - But df shows free space! monita Linux - General 7 03-30-2004 02:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration