HaveyoueverwantedtocombinethespeedandsafetyofBashwiththeraw,unbridledpowerofC?Thanksto@taviso'swonderfulctypes.shthisispossible:
#!/bin/bash#wherectypes.shisinstalled;youwilllikelyhavetochangethisLD_LIBRARY_PATH=$HOME/local/lib.~/code/ctypes.sh/ctypes.sh#compilestdintoaDSOfunctionbuild{cfile=$(mktemp/tmp/XXXXXX.c)sofile=$(mktemp/tmp/XXXXXX.so)whilereadline;doecho$line>>$cfiledonecc-fPIC-shared$cfile-o$sofilerm-f$cfileecho$sofile}#ourcodesofile=$(build<<EOF#include<stdio.h>voidhello_world(void){puts("helloworld");}intpopcnt(intnum){intout;__asm__("popcnt%1,%0":"=r"(out):"r"(num):"0");returnout;}EOF)#cleanupwhenwe'redonetrap"rm-f$sofile"EXIT#loadthecodedlopen$sofile#printhelloworlddlcallhello_world#getthepopcntof5dlcall-rint-noutpopcnt5echo$out|egrep-o'[0-9]+'Moredetailsonmyblog.
评论