LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-04-2008, 11:21 AM   #1
bengoavs
LQ Newbie
 
Registered: Jun 2006
Posts: 20

Rep: Reputation: 0
env loop


Yo!

I need a loop that would go through each line of the env.
Should be simple.

TNX!!!
 
Old 01-04-2008, 01:02 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
for i in `env`; do something; done



Cheers,
Tink
 
Old 01-04-2008, 02:22 PM   #3
bengoavs
LQ Newbie
 
Registered: Jun 2006
Posts: 20

Original Poster
Rep: Reputation: 0
That's actually what I've done but get some strange lines.
Here's the output of diff between manual env and script env echoing:
What are those 2435, 22 ?? I want it to be identical to manual env
Code:
benw@aoilinux1/~/bash$ diff /tmp/auto /tmp/manu
2d1
< TERM=xterm
3a3
> TERM=xterm
5,7c5,6
< SSH_CLIENT=::ffff:172.19.187.27
< 2435
< 22
---
> SSH_CLIENT=::ffff:172.19.187.27 2435 22
> OLDPWD=/home/benw
12d10
< MAIL=/var/spool/mail/benw
14,15c12
< _=/bin/env
< INPUTRC=/etc/inputrc
---
> MAIL=/var/spool/mail/benw
16a14
> INPUTRC=/etc/inputrc
17a16
> PS1=\[\e[32m\]\u@\h/\[\e[1;31m\]\w\[\e[1;34m\]\$\[\e[0m\]
19d17
< SHLVL=3
20a19
> SHLVL=2
22,27c21,22
< SSH_CONNECTION=::ffff:172.19.187.27
< 2435
< ::ffff:172.29.37.9
< 22
< LESSOPEN=|/usr/bin/lesspipe.sh
< %s
---
> SSH_CONNECTION=::ffff:172.19.187.27 2435 ::ffff:172.29.37.9 22
> LESSOPEN=|/usr/bin/lesspipe.sh %s
28a24
> _=/bin/env

Last edited by bengoavs; 01-04-2008 at 02:30 PM.
 
Old 01-04-2008, 03:19 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
< SSH_CLIENT=::ffff:172.19.187.27
< 2435
< 22

> SSH_CLIENT=::ffff:172.19.187.27 2435 22
Your script does not manage blank spaces in the correct way. If you post the content of the script it would be easier to explain.
 
Old 01-05-2008, 01:24 AM   #5
bengoavs
LQ Newbie
 
Registered: Jun 2006
Posts: 20

Original Poster
Rep: Reputation: 0
Code:
for i in `env`
do
echo "$i"
done
 
Old 01-05-2008, 04:55 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well... before the shell executes the for loop, it substitutes the output of the env command so it truly executes something like the following
Code:
for i in LESS=-M -I LESSOPEN=lessopen.sh %s LESSCLOSE=lessclose.sh %s %s and so on ...
do
  echo $i
done
that is it considers a long list of strings (fields) separated by blank spaces and execute the command inside the loop one by one. To prevent this behavior (splitting a long string by blank spaces) you have to modify the value of the Input Field Separator, which is stored in the shell variable IFS. The correct way is:
Code:
# Store the original IFS
OLD_IFS=${IFS}

# Set IFS to 'newline'
IFS=$(echo)

# Now execute the loop splitting the string by newlines instead of blank spaces
for i in $(env)
do
   echo $i
done

# Restore the original IFS
IFS=${OLD_IFS}
Note that the syntax $(command) is equivalent to `command`. See man bash, section "Command substitution" for details.

Last edited by colucix; 01-05-2008 at 04:56 AM.
 
Old 01-05-2008, 06:22 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If you don't need to operate on elements you could also echo complete lines: 'env|while read i; do echo "$i"; done'
 
  


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 Off
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 loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
execute the .env bxr1981 Programming 5 08-22-2005 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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