c - Can't understand why this causes an error -
i have large directory of music listed in file called op. have been able build command randomly pick song op file using creative math nanosecond output date command. works fine command line:
sed -n $((10#$(date +%n)%$(wc -l /shared/southpark/music/op|cut -d ' ' -f 1)))p /shared/southpark/music/op i want include command in c program , read line in popen.
#include <stdio.h> #include <string.h> int main (int argc, char *argv[]) { char command[201]; char buf[501]; file *fp; strcpy(command, "sed -n $((10#$(date +%n)%$(wc -l /shared/southpark/music/op|cut -d ' ' -f 1)))p /shared/southpark/music/op"); if((fp = popen(command, "r")) == null) { fprintf(stderr, "music_player: popen failed\n"); return(1); } if(fgets(buf, sizeof(buf), fp) == null) { fprintf(stderr, "music_player: fgets failed\n"); return(1); } printf("%s\n", buf); pclose(fp); return(0); } but when run it, following error:
sh: 1: arithmetic expression: expecting eof: "10#271445839%2278" music_player: fgets failed how can this? i'm not understanding error message.
popen executes command using
/bin/sh -c "command" and sh doesn't understand 10# base-conversion prefix. you've been running command in bash previously.
to fix, have 2 options:
- discard unnecessary
10#prefix (it default)shcompatibility use
bash:popen("bash -c 'command'", ...)
Comments
Post a Comment