6

I'm trying to call a Ruby script from a bash script, but no luck.

#!/bin/bash
ruby -v

works just fine, so I know that it's not Ruby, but

#!/bin/bash
ruby bash_test.rb

does not. Here's the fun part:

john@starfire:~/Desktop$ bash ubuntu_cmds.sh
(LoadError)h file or directory -- bash_test.rb
john@starfire:~/Desktop$ ls *.rb
bash_test.rb

Both files are sitting on my desktop.

ruby bash_test.rb

works just fine, too.

I'm new to bash scripting, so I'm pretty sure that I'm just making a stupid error.

I'm running Ubuntu 10.10 with Ruby 1.8.7. Thanks in advance for any help or advice.

EDIT: Deleted the .sh and the .rb and started over, and made sure to chmod +x the .sh, and it worked on the first try. I have no idea why. Thanks for the help, though.

3
  • Please see my edit; the best way to format blocks of code is to indent them by 4 spaces, or select them and press the {} button. Commented Mar 8, 2011 at 18:10
  • Ahh, thanks... that is much nicer. I appreciate it. Commented Mar 8, 2011 at 18:14
  • 1
    For the benefit of those looking for the {} button on their keyboard, @meagar's referring to the icon above the question editor. Commented Mar 8, 2011 at 22:16

3 Answers 3

5

try

#!/bin/bash
ruby ~/Desktop/bash_test.rb
Sign up to request clarification or add additional context in comments.

1 Comment

(LoadError)h file or directory -- /home/john/Desktop/bash_test.rb :P Should've mentioned that I tried that already.
3

Do you have a carriage return in the bash script after the file name ?


EDITED

Double check how the filename is being passed to ruby in the bash script.

Error output should be as below if the file wasn't found

ruby: No such file or directory -- bash_test.rb (LoadError)

From what you are displaying as an error it appears that there is a carriage return that is being assumed by ruby as part of the filename so you are getting the following error output.

 (LoadError)h file or directory -- bash_test.rb

2 Comments

@kivetros: It's not "do you" it's "you do". Run dos2unix against your script.
I ended up just deleting both the script and the .rb and starting over... mysteriously enough, both worked just fine on the first try. dos2unix is really cool, though - thanks for the heads-up on that.
3

You may have to do ruby ./bash_test.rb, as sometimes . isn't in your $PATH.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.