shell - word splitting - using eval


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell - word splitting - using eval
# 1  
Old 03-24-2009
shell - word splitting - using eval

In POSIX shell, we don't have arrays, but we can iterate over a list like this:

Code:
#!/bin/sh
list="Fred Barney Wilma Betty"
for i in $list; do echo $i; done

Fred
Barney
Wilma
Betty

But let's say we want "Mr. Slate" in the list.
We know we can't just stick him in there like this:
list="Fred Barney Mr. Slate"
Because word splitting would split Mr. Slate into two words.

So, we could play with IFS

Code:
#!/bin/sh
list="Fred:Barney:Mr. Slate"
IFS=:
for i in $list; do echo $i; done

Fred
Barney
Mr. Slate

But what if we don't want to fool with the IFS.
We could use eval

Code:
#!/bin/sh
list='Fred Barney "Mr. Slate"'
eval '
    for i in '$list'; do 
        echo $i
    done
'
Fred
Barney
Mr. Slate

Notice we had to put literal quotation marks around Mr. Slate in the list. That's understandable.
Had to use single quotes to mark the beginning and end of eval block.
And had to make sure $list is NOT in single quotes.

This works, but it's kind of clumsy.
Is there a better way? Without messing with IFS and keeping with POSIX sh.

The reason I say this: when I do an eval block, it doesn't get the nice syntax highlighting in my editor! (gvim) It seems whenever I do an eval block, I never get it right the first time. Or second time!

Sometimes I do the eval thing within a function, as a way of "passing by reference" (I think that's what it's called). I know Bash has references. I'm trying to get in the habit of "portable" scripting. Or at least, make it run in Dash.

-MD
# 2  
Old 03-24-2009
If the list is in a file, then you can do this:

Code:
while read $name ; do
     echo $name
done < $file

You can also do the below style; makes it eaiser to add/remove (you can also do a comment-out mechanism using # by doing grep -v "^#" in the while).

Code:
list=$(cat <<-LIST
Fred
Barney
Mr. Slate
# Mr. Skipped
LIST
)
echo "${list}" |grep -v "^#"|while read $name ; do
     echo $name
done

I don't think there is anything radical we can do with purely shell.
# 3  
Old 03-24-2009
Quote:
Originally Posted by mjd_tech
[...]
This works, but it's kind of clumsy.
Is there a better way? Without messing with IFS and keeping with POSIX sh.[...]
You can emulate arrays using positional parametrs:

Code:
$ set -- Fred Barney "Mr. Slate"
$ for e;do printf '=>%s<=\n' "$e";done
=>Fred<=
=>Barney<=
=>Mr. Slate<=

# 4  
Old 03-24-2009
Code:
$ set -- Fred Barney "Mr. Slate"

How do we set, if the list in a variable?
# 5  
Old 03-24-2009
Try...
Code:
$ list='Fred Barney "Mr. Slate"'
$ eval set -- "$list"
$ for i
> do echo $i
> done
Fred
Barney
Mr. Slate
$

# 6  
Old 03-24-2009
Thanks - had been trying this for a while.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

4. Shell Programming and Scripting

Prevent word splitting with file with spaces in name

Hello, I have a script that "validates" a ZIP file that look like this AAA_20120801.zip => x~back end~20120801.TXT y~time in~20120801.TXT z~heat_chamber~20120801.TXT AAA_20120801.ctlMy task is to compare its contents (i.e the list of files contained inside) with the control file that is... (2 Replies)
Discussion started by: alan
2 Replies

5. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

6. UNIX for Advanced & Expert Users

Variable assignments specified with eval shell built-in

According to the POSIX specifications eval is a special shell built-in, which should imply that variable assignments specified together with it should remain in effect after the built-in completes. Thus one would expect IFS to be changed after this: var=$'a\nb c' $ IFS=$'\n' eval ' for i in... (4 Replies)
Discussion started by: Scrutinizer
4 Replies

7. Shell Programming and Scripting

eval in shell scripting

I am stuck on something that should really be simple, and was looking for some help.. I am new to shell scripting.Need help on this..... The script is to find the stale nfs. cat file - - - - /abcd/1234 I am writing the script to check the nfs errors of above file ... (3 Replies)
Discussion started by: nareshkumar522
3 Replies

8. Shell Programming and Scripting

eval in shell scripting

what is the real usage of eval in shell scripting whats the difference between $ cmd && eval cmd (10 Replies)
Discussion started by: mobydick
10 Replies

9. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question