开发者

Ruby to bash script translation

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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜