We have a few ruby scripts that needs to be translated to bash scripts. Has anyone done this or similar thing in the past (say from python to bash). Do you have any guidelines/heuristics for me.
-
Can you give us an idea of what the scripts do?Rafe Kettler– Rafe Kettler2011-05-23 03:34:12 +00:00Commented May 23, 2011 at 3:34
-
The scripts do some utility computing such as doing database calls, caching connections using memcached, and some arithmetic as far as I have been able to understood. Need to simplify so that a wider audience can understand them.Ketan– Ketan2011-05-23 03:38:26 +00:00Commented May 23, 2011 at 3:38
-
2that stuff isn't really bash's sweet spot (especially arithmetic). I'd recommend you take Ignacio's advice and keep the existing Ruby scripts. Maybe refactor them a bit?Rafe Kettler– Rafe Kettler2011-05-23 03:39:34 +00:00Commented May 23, 2011 at 3:39
-
1Hmm. Ruby is easier to read than bash. This is sort of like firing the college grad and hiring a dropout.DigitalRoss– DigitalRoss2011-05-23 03:45:13 +00:00Commented May 23, 2011 at 3:45
-
2In my opinion, readability is subjective to what one is used to.Ketan– Ketan2011-05-23 03:47:41 +00:00Commented May 23, 2011 at 3:47
2 Answers
The first guideline is: Don't do it. If your scripts already work then there's no point in making them possibly not work by translating them.
The second guideline is: Translate tasks, not code. This is especially important since you're moving from a more-capable language to a less-capable one.
1 Comment
I don't want judge your idea, because it is impossible to tell what is better for the given job. Maybe bash, maybe ruby. So here are my $0,02:
break the task to smaller tasks. Try find some shell command (or unix executable) for the given smaller task. for example:
files = Dir.glob("*.jpg")=>ls *.jpgFor this task, you should know as much unix commands as much possible. Tryls /bin /usr/binand check your commands. Do you know them? If not - converting will be an hard task... ;)debug each smaller task alone (if it is possible)
use as much shell built-ins as possible. (they are usually a little bit faster than running external commands) Do you know what built-ins have your shell?
use pipes as much as possible for connecting smaller task into big one. This is the hard part. Breaking up code to smaller tasks are the best when they're can run alone, with stdin/out (pipe ready). Usually need (somewhat) changing the logic of the application.
if you have reasonably new version of bash - you can use it for some network programing too, for example:
bash networking with /dev/{tcp|udp} - has nothing with your /dev/ directory - they are bash's internals.
exec 3<>/dev/tcp/www.google.com/80
echo -e "GET / HTTP/1.1\n\n" >&3
cat <&3