Unable to initialize device PRN message upon executing .rb-Script in cygwin - ruby

The headline should pretty much say it all. I'm using windows 7, installed cygwin, set it up, created a .rb-File which looks like this:
print "Test"
(Impressive stuff, eh?)
Now, using Cygwin, I want to execute it, I tried
chmod +x ruby.rb
afterwards
./ruby.rb
The following message comes up:
Unable to initialize device PRN
Any ideas?
Thanks!

You forgot the "she-bang" line. Without it it will be interpreted as a bash script, not a ruby script.
The print command that bash finds is Window's print.exe, which wants to print to a physical priner, rather than print to the screen.
Add "#!/usr/bin/env ruby" as the first line of the file.

Related

How do I execute a terminal command that runs a bash script and makes use of a string argument?

I'm a bit confused (no experience with this stuff)
So, I'm supposed to run a terminal command that looks something like this:
./script_name file.txt "1, 2, 5"
So, the command would run a script that would take a file and print out the lines indicated in the string (this is just an example).
I'm just a bit confused how to do that in-line with running the script, like how the command indicates. I understand that I can run the script first, and then get user input for which lines, etc, which file, etc. But how is such a command like above setup and used in the script?
I can include file.txt in the same directory and I assume it can be accessed that way, so it wouldn't be a problem. It's the string that is confusing me. How would that be used and stored prior to running the script first and asking for user input?

Problems running bash script from incron

I have a simple incron task setup to run a command whenever a particular .json file is written-to, then closed.
/var/www/html/api/private/resources/myfile.json IN_CLOSE_WRITE,IN NO LOOP /var/www/html/api/private/resources/run_service.sh
I can see that whenever the file to written to, there is a syslog entry for the event, and the command that was triggered - along the lines of <date> - incrond: CMD (/var/www/html/api/private/resources/run_service.sh).
But nothing seems to happen...
initially I thought this would be caused by an issue with the script, but replacing the script command to something simple such as echo "hello world" > /tmp/mylog.log still yields no output or results. I seem to have hit a brick wall with this one!
Update
Changing the incron command to read "/bin/bash /var/www/html/api/private/resources/run_service.sh" now seems to triggering the script correctly, as I can now get output from the script.
A simple mistake on my part, despite all examples online showing that using the script as the command should run it, for me it only works if I explicitly call bash to execute it
"<my directory/file to watch> <trigger condition> /bin/bash /var/www/html/api/private/resources/run_service.sh

Changing directory in ubuntu with Ruby and a command line tool

#!/home/user/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
case ARGV[0]
when "apache"
exec('cd /etc/apach2')
exec('sudo nano httpd.conf')
#...
end
I am trying to make a quick command line tool that will change directories for me with one word.. so from the command line (in ubuntu 12). It tells me it cant cd.. But I try the command myself and it will work just fine.
Ruby's Dir class is your friend for this, check-out the chdir method:
Dir.chdir('/path/to/change/to')
will change Ruby's concept of the current working directory for the time the code is running. Any sub-shells would consider that their starting directory.
You can also pass chdir a block, and all code in that block will assume the new directory, which will then revert to the old one when the block exits:
Dir.chdir('/path/to/change/to') do
# do some stuff
end
OK, so I did this and it works (I'm on OS X but should be the same):
ARGV[0]
when "testme"
system('cd ripple')
system('ls -al')
#...
end
calling system('cd ... does not change move you to that directory in the current shell you are executing your .rb file in. So it would make more sense to do:
system('sudo nano /etc/....
all on one line
I tested it with back ticks and it didn't work at all for me.
I tested with exec() and got the expected result, it runs one line and that's it. So exec() could work if you only have one command to run or you chain them all together with &&
exec('ls /etc && sudo nano /etc/....
I would read this: http://rubyquicktips.com/post/5862861056/execute-shell-commands

Ruby - How to use -r switch with ruby command line tool

I was trying to figure out how to work the command line switch -r.
My understanding is that the code is typed out as follows:
ruby -r*nameOfRequired*
I am finding that this is not the case. When I type out the above and press enter, the terminal expects an "end of input syntax" and does not continue.
What am I missing? Does there need to be a space in between the switch and the name of the required file?
Please and thank you!
EDIT:
I am currently reading "The Well Grounded Rubyist" by David A. Black, and I came up with this question while reading the section on command line switches.
Having said that, I created a "test.rb" file, containing:
puts Date.today
Then, in the terminal, I typed out:
ruby -r date
I thought this would 'require' the date module, and then enable me to run the "test.rb" file, using ruby test.rb (given that I am in the correct directory).
Instead, the terminal cursor moves to a newline, expecting more input. Let me know if I need to clarify anything else. Thanks!
If you just type ruby -rmodule, then Ruby will load the module and wait for you to type the main program that requires that module.
If you just want to run the module and do nothing else, you can do do rubyfull-path-to-module without the -r, or ruby -rmodule -e exit, or ruby -rmodule </dev/null, or similar.
In general, the ruby command does not record any state from one run to the next, so you need to tell it every thing that it needs to know whenever you run it.
Whenever you run it, you need to tell it the main program to run or else it will expect you to type that program on the standard input. The -r does not specify the main program.
Try this:
ruby -rdate test.rb
According to ruby -h:
-rlibrary require the library, before executing your script
Without giving your script file path, it read the script from stdin.
Try following (You can omit script file path when you give -e command):
ruby -r**nameOfRequired** -e ""

Simple shell script doesn't work like command line?

I'm trying to write a script that contains this
screen -S demo -d -m which should start a new screen session named demo and detach it.
Putting screen -S demo -d -m in the command line works.
If I put it in a file named boot.sh, and run it ./boot.sh I get
Error: Unknown option m
Why does this work in the command line but not as a shell script?
This file was transferred from windows and had ctrl-M characters.
Running "screen" on my Linux machine, a bad option (Screen version 4.00.03jw4 (FAU) 2-May-06) gives the error,
Error: Unknown option -z"
while your description includes no dash before the offending option. I'd check that the characters in your script file are what you expect them to be. There are many characters that look like a dash but which are not.
cat -v boot.sh
may show something interesting as it'll show codes for non-ascii characters.
This may seem a little like the "make sure your printer is plugged in" kind of help, but anyway:
have you tried to check if the screen you're invoking from the script is the same as the one invoked from the command line ?
I'm thinking you may change the PATH variable inside your script somewhere and perhaps screen from the script would be something else (a different version, perhaps ?).

Resources