On Fri, Dec 15, 2017 at 12:24:45PM +0100, Paul de Weerd wrote: | I've updated the diff to add this example as per jmc's suggestion. It | now has: | | - add the `-c command` feature | - updates usage | - removes /* ARGSUSED */ lint comments | - documents the -c feature | - adds an example to the manpage
jmc@ pointed out a missing colon at the end of the example. Apologies for the extra noise. Updated diff (still covers the above five changes) included. Cheers, Paul Index: script.1 =================================================================== RCS file: /cvs/src/usr.bin/script/script.1,v retrieving revision 1.14 diff -u -p -r1.14 script.1 --- script.1 15 Jan 2012 20:06:40 -0000 1.14 +++ script.1 16 Dec 2017 08:42:24 -0000 @@ -39,6 +39,7 @@ .Sh SYNOPSIS .Nm script .Op Fl a +.Op Fl c Ar command .Op Ar file .Sh DESCRIPTION .Nm @@ -65,9 +66,14 @@ Append the output to or .Pa typescript , retaining the prior contents. +.It Fl c Ar command +Run +.Ar command +instead of an interactive shell. +To run a command with arguments, enclose both in quotes. .El .Pp -The script ends when the forked shell exits (a control-D +The script ends when the forked program exits (a control-D .Pq Ql ^D to exit the Bourne shell @@ -102,6 +108,11 @@ Name of the shell to be forked by If not set, the Bourne shell is assumed. (Most shells set this variable automatically.) .El +.Sh EXAMPLES +Start a virtual machine and log all console output to a file: +.Bd -literal -offset indent +$ script -c "vmctl start myvm -c" myvm.typescript +.Ed .Sh HISTORY A predecessor called .Nm dribble Index: script.c =================================================================== RCS file: /cvs/src/usr.bin/script/script.c,v retrieving revision 1.33 diff -u -p -r1.33 script.c --- script.c 12 Apr 2017 14:49:05 -0000 1.33 +++ script.c 14 Dec 2017 07:34:10 -0000 @@ -89,7 +89,7 @@ int istty; __dead void done(int); void dooutput(void); -void doshell(void); +void doshell(char *); void fail(void); void finish(int); void scriptflush(int); @@ -102,17 +102,23 @@ main(int argc, char *argv[]) struct sigaction sa; struct winsize win; char ibuf[BUFSIZ]; + char *cmd; ssize_t cc, off; int aflg, ch; + cmd = NULL; aflg = 0; - while ((ch = getopt(argc, argv, "a")) != -1) + while ((ch = getopt(argc, argv, "ac:")) != -1) switch(ch) { case 'a': aflg = 1; break; + case 'c': + cmd = optarg; + break; default: - fprintf(stderr, "usage: %s [-a] [file]\n", __progname); + fprintf(stderr, "usage: %s [-a] [-c command] [file]\n", + __progname); exit(1); } argc -= optind; @@ -163,7 +169,7 @@ main(int argc, char *argv[]) if (child) dooutput(); else - doshell(); + doshell(cmd); } bzero(&sa, sizeof sa); @@ -196,7 +202,6 @@ main(int argc, char *argv[]) done(sigdeadstatus); } -/* ARGSUSED */ void finish(int signo) { @@ -215,7 +220,6 @@ finish(int signo) errno = save_errno; } -/* ARGSUSED */ void handlesigwinch(int signo) { @@ -294,7 +298,6 @@ dooutput(void) done(0); } -/* ARGSUSED */ void scriptflush(int signo) { @@ -302,9 +305,10 @@ scriptflush(int signo) } void -doshell(void) +doshell(char *cmd) { char *shell; + char *argp[] = {"sh", "-c", NULL, NULL}; shell = getenv("SHELL"); if (shell == NULL) @@ -313,8 +317,15 @@ doshell(void) (void)close(master); (void)fclose(fscript); login_tty(slave); - execl(shell, shell, "-i", (char *)NULL); - warn("%s", shell); + + if (cmd != NULL) { + argp[2] = cmd; + execv(_PATH_BSHELL, argp); + warn("unable to execute %s", _PATH_BSHELL); + } else { + execl(shell, shell, "-i", (char *)NULL); + warn("%s", shell); + } fail(); } -- >++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+ +++++++++++>-]<.>++[<------------>-]<+.--------------.[-] http://www.weirdnet.nl/