bash/sh: global or local variable with for (loop) isssue
Hello,
I wanna prepare for growisofs, with taking the files, removing space, and to put them not in column but in line. my code is: Code:
ls -1 | sedremovespace | while read GL ; do FINAL="$FINAL $GL " ; done ; echo "$FINAL" with /usr/bin/sedremovespace: Code:
sed 's/\ /\\\ /g' |
Removed the post because of an embarrassing mistake.
|
Moin,
every pipe creates a subshell and that's why nothing you assign to a variable after a pipe is visible outside. It's a common mistake: A variable can't be exported from a subshell to the parent shell. You can do it like this (look here for explanations: http://www.jan-trippler.de/en/script...variables.html): Code:
FINAL="`ls -1 | sedremovespace | while read GL; do echo -n \"$GL \"; done`" EDIT: But there's an even more simplier way: You don't need the read loop. Code:
FINAL="`ls -1 | sedremovespace`" |
Code:
#!/bin/bash cuz echo $counter returns nothing |
Like Jan61 said
Quote:
Code:
counter=0 |
Quote:
if cat /tmp/files.tmp Code:
/home/french/the file with spaces.doc Code:
counter=0 then your script doesnt work because for / "$file" are messed up with spaces and not LF/chr(10)chr(13) :( :( :( Help please |
In that case, set the IFS to a newline only at the start.
http://tldp.org/LDP/abs/html/internalvariables.html |
Quote:
In my opinion, bash developers took an unfortunate decision when choosing which part of a pipeline would be run in a subshell. |
Quote:
As for this particular script, I don't see the use for piping. I have seen a few of this similar problems over the past few months which all concerned piping into a loop. Piping the result of one program into another (like Code:
cat myfile | grep foo | sed s/foo/bar/g Code:
ls -1 | while read .... done The example chrism01 provided is much cleaner, provides hooks for debugging and makes the code obvious and understandable. Maybe programmers should be discouraged of using pipes in this manner anyway. jlinkels |
Quote:
Either you explicitely specify the shell you use (bash, ksh, zsh, whatever) and you are free to take advantage of their specific enhancements. Either you want to write portable scripts and you need to stick to POSIX shell statements and syntax. What is unfortunate in that specific pipe subshell case is POSIX didn't choose to follow ksh (which was the model for most features) and let that choice implementation dependent. Scripts that rely on variable sets in pipeline components are thus all non portable. Quote:
Quote:
Quote:
Code:
sed -n 's/foo/bar/gp' myfile |
All times are GMT -5. The time now is 02:44 PM. |