So, I made a simply ruby script,
#!/usr/bin/env ruby
puts "Hello!"
When I try to run it in terminal it doesn't put "Hello!" on the screen. I have tried entering chmod +x test.rb (test.rb is the name of my file). When I run it, it doesn't give me an error, it just doesn't display "Hello!". Any help will be much appreciated. I have looked everywhere for a possible answer, and I have found nothing so far.
I'd guess that you're trying to run it as just test like this:
$ test
But test is a bash builtin command that doesn't produce any output, it just sets a return value. If you run your script properly:
$ ./test.rb
then you'll see something. Note the explicit ./ path, the current directory is rarely (and hopefully never) in your PATH so you need to say ./ to run something in the current directory (unless of course you're in /bin, /usr/bin, etc.).
In the comments you say that there are some Ctrl+M characters in your script:
$ cat -e test.rb
#!/usr/bin/env ruby^M^Mputs "Hello!"
I don't see any $s in that cat -e output so you don't have any actual end-of-line markers, just some carriage-return characters (that's the ^M). A single CR is an old MacOS end-of-line, Windows uses a CR-LF pair, and Unix (including OSX) uses just a single LF to mark the end of a line of text. Since you don't have any EOLs, the shell just sees a single line that looks like:
#!/usr/bin/env ruby ...
without an actual script for ruby to run, the shell just sees the shebang comment and nothing else. The result is that nothing noticeable happens when you run your script. Fix your EOLs and your script will start working sensibly. You might also want to look at your editor's settings so that it starts writing proper EOLs.
How are you calling this method. You would want to call this out by something like ruby test.rb if you are in the directory with the test.rb file. Another general tip for trying something out that doesn't work would be to go into irb on command line and try your program, like puts "Hello! to see if it is that particular code that is the problem.
Related
I have a script in unix that looks like this:
#!/bin/bash
gcc -osign sign.c
./sign < /usr/share/dict/words | sort | squash > out
Whenever I try to run this script it gives me an error saying that squash is not a valid command. squash is a shell script stored in the same directory as this script and looks like this:
#!/bin/bash
awk -f squash.awk
I have execute permissions set correctly but for some reason it doesn't run. Is there something else I have to do to make it able to run like shown? I am rather new to scripting so any help would be greatly appreciated!
As mentioned in #Biffen's comment, unless . is in your $PATH variable, you need to specify ./squash for the same reason you need to specify ./sign.
When parsing a bare word on the command line, bash checks all the directories listed in $PATH to see if said word is an executable file living inside any of them. Unless . is in $PATH, bash won't find squash.
To avoid this problem, you can tell bash not to go looking for squash by giving bash the complete path to it, namely ./squash.
I have two identical shell script: A.sh , A , I want to know what is the difference ?
You can even call it A.txt. Then give it execute permission and run it. If you give a .sh extension then others can easily identify that its a shell script.
As far as Linux based operating system, the extension does not make any special meaning. You can have any name for your file. You can execute the script with respective runtime.
For bash, sh sample.txt
For Python, python sample.txt
For NodeJS, node sample.txt
For better maintainability of the code, you have to name your filename with proper extension like .sh, .py, .js and etc.
Actually,
Once your /path/to/anyfunnyfilename script has execution privileges, if you want to run it without calling the interpreter, you can have the FIRST line of your script like this:
#!/usr/bin/bash
code line 1
code line 2
...
code line n
it also apply for
#!/bin/perl
your perl code here
Another example:
#!/bin/python
your python code here
#!/bin/expect
your expect code here
And so on..
Hope this helps..
I am trying to run my shell script from command line lets say;
my script looks like this:
#!bin/bash
echo hello
When try to run this source ./abcd.sh I get this error.
"' is not a typo you can run the following command to lookup the package that contains the binary:
command-not-found
: command not found
hello
"
Never seen this before something wrong with having a empty line before "echo hello" ? I was wondering if anyone else encountered something like this.
Along with the first line of your script being a comment, it sounds like your file has DOS line endings, and the carriage return is being treated as the command that isn't found. The error message sounds like something provided by a custom command_not_found_handle function (which I believe Ubuntu defines).
#!bin/bash
needs to be
#!/bin/bash
or wherever bash is installed (you can locate this by doing whereis bash).
Your program should work fine when invoked using bash, i.e., bash ./abcd.sh, but when executed directly ./abcd.sh then the hashbang line does matter because that is how the interpreter is located for the script contained in the executable file.
Try echo 'hello', within quotes. It looks like there is a newline between the echo command and hello and it is trying to run 'hello' as a command.
The hashbang line should be #!/bin/bash, but messing that up won't matter as it will interpret any line that starts with a hash as a comment.
Run script with debug option to see which line actually is failing:
bash -x abcd.sh
Note: in this case the Shebang line will be treated as a comment, so if the rest of your script is correct, it will execute correctly:
$ bash -x abcd.sh
+ echo hello
hello
Make sure your file does not have a BOM
I had the same problem when editing a script under Windows with Notepad++.
make sure to convert to "UTF-8 witout BOM".
I'm trying to write a bash script which will behave as a basic interpreter, but it doesn't seem to work: The custom interpreter doesn't appear to be invoked. What am I doing wrong?
Here's a simple setup illustrating the problem:
/bin/interpreter: [owned by root; executable]
#!/bin/bash
echo "I am an interpreter running " $1
/Users/zeph/script is owned by me, and is executable:
#!/bin/interpreter
Here are some commands for the custom interpreter.
From what I understand about the mechanics of hashbangs, the script should be executable as follows:
$ ./script
I am an interpreter running ./script
But this doesn't work. Instead the following happens:
$ ./script
./script: line 3: Here: command not found
...It appears that /bin/bash is trying to interpret the contents of ./script. What am I doing wrong?
Note: Although it appears that /bin/interpreter never invoked, I do get an error if it doesn't exist:
$ ./script
-bash: ./script: /bin/interpreter: bad interpreter: No such file or directory
(Second note: If it makes any difference, I'm doing this on MacOS X).
To make this work you could add the interpreter's interpreter (i.e. bash) to the shebang:
#!/bin/bash /bin/interpreter
Here are some commands for the custom interpreter.
bash will then run your interpreter with the script path in $1 as expected.
You can't use a script directly as a #! interpreter, but you can run the script indirectly via the env command using:
#!/usr/bin/env /bin/interpreter
/usr/bin/env is itself a binary, so is a valid interpreter for #!; and /bin/interpreter can be anything you like (a script of any variety, or binary) without having to put knowledge of its own interpreter into the calling script.
Read the execve man page for your system. It dictates how scripts are launched, and it should specify that the interpreter in a hash-bang line is a binary executable.
I asked a similar question in comp.unix.shell that raised some pertinent information.
There was a second branch of the same thread that carried the idea further.
The most general unix solution is to have the shebang point to a binary executable. But that executable program could be as simple as a single call to execl(). Both threads lead to example C source for a program called gscmd, which is little more than a wrapper to execv("gs",...).
I finished my short file for a homework assignment which uses IO.popen("command").readlines to grab the STDOUT of that command. However, I need to write a shell script to wrap my ruby file in. No problem, but somehow putting it in the shell script makes readlines hang.
ruby script.rb foo example > example.out
this works
script.sh foo example >example.out
this hangs on readlines. ruby script.rb is all that script.sh contains.
Looks like you forgot to pass your arguments to the ruby command. You may also be failing to specify an interpreter
script.sh
#!/bin/sh
ruby script.rb "$#"
Alternatively you could just add #!/usr/bin/ruby to the top of script.rb and make it executable (chmod +x script.rb). It's not a shell script. But it's generally the preferred way of executing a script in an interpretive language.
Once that's done you can run it with
./script.rb