# Script with -e command line option?

**URL:** <https://www.ruby-forum.com/t/script-with-e-command-line-option/83526>\
**Category:** Ruby\
**Created:** [January 26, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526 "2007-01-26T22:41:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 26, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/1 "2007-01-26T22:41:55Z")

</div>

If I type in the following example from “Programming Ruby”, I get an  
error:

$ ruby -n -e “print if /wombat/” \*.txt  
-e:1: No such file or directory - if (Errno::ENOENT)

This is from a BASH command line on a linux system with ruby 1.8.4

At first I thought it was a problem with BASH interfering with quoting  
or something, but I’ve tried many variations with no luck. Any idea?

thanks,

–su

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 26, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/2 "2007-01-26T22:55:17Z")

</div>

Suresh U. schrieb:

> If I type in the following example from “Programming Ruby”, I get an  
> error:
> 
> $ ruby -n -e “print if /wombat/” \*.txt  
> -e:1: No such file or directory - if (Errno::ENOENT)
> 
> This is from a BASH command line on a linux system with ruby 1.8.4

My answer is from Windows 2000, Ruby 1.8.5, but the reason may be, that  
there is  
no file in your directory which mathes “\*.txt”

> > > > > Example from Windows Console \>\>\>\>\>

C:\Dokumente und Einstellungen\wolfgang\Desktop\>type otto.txt  
Hello, world!

I’m here to wombat all thinks,  
whatever wombat means…  
C:\Dokumente und Einstellungen\wolfgang\Desktop\>ruby -n -e “print if  
/wombat/” \*.txt  
I’m here to wombat all thinks,  
whatever wombat means…

> > > > > EoE \>\>\>\>\>

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 26, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/3 "2007-01-26T23:08:29Z")

</div>

Wolfgang NÃ¡dasi-donner wrote:

> My answer is from Windows 2000, Ruby 1.8.5, but the reason may be, that  
> there is  
> no file in your directory which mathes “\*.txt”

no, that’s not my problem. oh - but this reminds me that if i just use  
something like “print” for the command line command, then it works fine:

$ ls \*.txt  
test.txt

$ more test.txt  
I wish I had a fish.  
I wish I had a wombat.  
Fish are tasty.  
Wombats are tasty too.

$ ruby -n -e “print if /wombat/” \*.txt  
-e:1: No such file or directory - if (Errno::ENOENT)

$ ruby -n -e “print if /wombat/” test.txt  
-e:1: No such file or directory - if (Errno::ENOENT)

$ ruby -n -e “print” test.txt  
I wish I had a fish.  
I wish I had a wombat.  
Fish are tasty.  
Wombats are tasty too.

$ ruby -n -e “print” if test.txt  
-e:1: No such file or directory - if (Errno::ENOENT)

so it seems clear that ruby isn’t getting the entire -e argument,  
probably due to intereference by BASH. anyone know how to fix this?

–su

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 26, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/4 "2007-01-26T23:07:19Z")

</div>

On Jan 26, 2007, at 4:55 PM, Wolfgang Nádasi-Donner wrote:

> > > > > > Example from Windows Console \>\>\>\>\>
> 
> > > > > > EoE \>\>\>\>\>
> 
> Wolfgang Nádasi-Donner

I suspect Wolfgang is correct. The output that I get is:

$ ruby -n -e “print if /wombat/” \*.txt  
-e:1: No such file or directory - \*.txt (Errno::ENOENT)

Are you copying the exact message? It’s odd that it thinks “if” is  
the name. When the file exists it appears to do what you probably  
expected.

$ ls  
README app config doc log script tmp  
Rakefile components db lib public test vendor  
$ ruby -n -e “print if /wombat/” README\*  
$ ruby -n -e “print if /Congratulations/” README\*  
2. Go to [http://localhost:3000/](http://localhost:3000/) and get “Congratulations, you’ve put  
Ruby on Rails!”  
3. Follow the guidelines on the “Congratulations, you’ve put Ruby on  
Rails!” screen

Sorry, I didn’t make a new .txt file, but just used what was handy.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 26, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/5 "2007-01-26T23:35:22Z")

</div>

On 26.01.2007 23:08, Suresh U. wrote:

> -e:1: No such file or directory - if (Errno::ENOENT)  
> so it seems clear that ruby isn’t getting the entire -e argument,  
> probably due to intereference by BASH. anyone know how to fix this?

This looks like “ruby” was a shell script that does not properly quote  
arguments because ruby thinks the first word after “print” is a file  
name. Here you can see the effect:

robert@fussel ~  
$ ./aa -n -e “print if /wombat/” \*.txt  
-e:1: No such file or directory - if (Errno::ENOENT)

robert@fussel ~  
$ cat aa  
#!/bin/sh -f

ruby $\*

robert@fussel ~  
$

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 27, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/6 "2007-01-27T17:22:07Z")

</div>

On Friday 26 January 2007 17:08, Suresh U. \<Suresh U.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

> test.txt  
> $ ruby -n -e “print if /wombat/” test.txt  
> -e:1: No such file or directory - if (Errno::ENOENT)

jab3:~% cat \> wombat.txt  
I wish I had a fish.  
I wish I had a wombat.  
Fish are tasty.  
Wombats are tasty too.  
jab3:~% ruby -n -e “print if /wombat/” \*.txt  
I wish I had a wombat.  
jab3:~%

Something is screwy with your setup. Note that it is complaining that  
the file ‘if’ doesn’t exist. You could try something like this and see  
if it works:

ruby -n -e “$stdout.print if /wombat/” \*.txt

Why it thinks that ‘if’ should be a file I’m not sure about.

-jab3

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 26, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/7 "2007-01-26T23:26:21Z")

</div>

On Jan 26, 2007, at 5:08 PM, Suresh U. wrote:

> $ ruby -n -e “print” if test.txt  
> -e:1: No such file or directory - if (Errno::ENOENT)
> 
> so it seems clear that ruby isn’t getting the entire -e argument,  
> probably due to intereference by BASH. anyone know how to fix this?
> 
> –su

Well, the first thing to try is changing the kind of quotes.

$ ruby -n -e ‘print if /wombat/’ test.txt

If that doesn’t work, I’d start to suspect that you have “ruby”  
defined as an alias or something to make the args be evaluated twice.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 27, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/8 "2007-01-27T20:13:26Z")

</div>

Robert K. wrote:

> This looks like “ruby” was a shell script that does not properly quote  
> arguments

Rob B. wrote:

> I’d start to suspect that you have “ruby”  
> defined as an alias or something to make the args be evaluated twice.

Turns out that this was indeed the problem. When I started using the  
ruby fltk extensions, I found that I had to replace /usr/bin/ruby with  
the following script:

#!/bin/bash

export LD\_ASSUME\_KERNEL=2.3.98  
exec /usr/bin/ruby1.8 $\*

so that’s why the arguments were being evaluated twice.

thanks everyone for your help!

–su

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 28, 2007, 11:20am UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/9 "2007-01-28T11:20:43Z")

</div>

On 27.01.2007 20:13, Suresh U. wrote:

> the following script:
> 
> #!/bin/bash
> 
> export LD\_ASSUME\_KERNEL=2.3.98  
> exec /usr/bin/ruby1.8 $\*
> 
> so that’s why the arguments were being evaluated twice.
> 
> thanks everyone for your help!

You can easily fix that script with this line which will do proper  
quoting:

exec /usr/bin/ruby1.8 “$@”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![sunadrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@sunadrad](https://www.ruby-forum.com/u/sunadrad)\
**Post date:** [January 27, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/script-with-e-command-line-option/83526/10 "2007-01-27T20:27:19Z")

</div>

Suresh U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> the following script:
> 
> #!/bin/bash
> 
> export LD\_ASSUME\_KERNEL=2.3.98  
> exec /usr/bin/ruby1.8 $\*
> 
> so that’s why the arguments were being evaluated twice.
> 
> thanks everyone for your help!

I would recommend that you do this in your script:

#!/bin/bash

export LD\_ASSUME\_KERNEL=2.3.98  
exec /usr/bin/ruby1.8 “$@”

The “$@” construct will help to maintain the quoting of the arguments  
which are passed in.

For example:

## script1

#!/bin/bash  
for arg in $\*  
do  
echo arg=$arg  
done

## script2

#!/bin/bash  
for arg in “$@”  
do  
echo arg=$arg  
done

% chmod +x script1 script2  
% ./script1 “this is the first arg” “this is the second arg”  
arg=this  
arg=is  
arg=the  
arg=first  
arg=arg  
arg=this  
arg=is  
arg=the  
arg=second  
arg=arg  
% ./script2 “this is the first arg” “this is the second arg”  
arg=this is the first arg  
arg=this is the second arg
