1

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.

5
  • Can you give us an idea of what the scripts do? Commented 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. Commented May 23, 2011 at 3:38
  • 2
    that 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? Commented May 23, 2011 at 3:39
  • 1
    Hmm. Ruby is easier to read than bash. This is sort of like firing the college grad and hiring a dropout. Commented May 23, 2011 at 3:45
  • 2
    In my opinion, readability is subjective to what one is used to. Commented May 23, 2011 at 3:47

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd qualify "less-capable" by saying that bash beats Ruby for some tasks (specifically if all you're doing is calling a bunch of system commands). If the initial Ruby script would be better written as a bash script, well, then... But your advice about not directly porting Ruby to bash is spot on.
1

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 *.jpg For this task, you should know as much unix commands as much possible. Try ls /bin /usr/bin and 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

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.