config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

exec.awk (641B)


      1 # Copyright 2022 Jacob R. Edwards
      2 # Awk exec function
      3 #
      4 # The shquote() function quotes a string for sh(1).
      5 #
      6 # The getcmd() function concatnates the given argument array on
      7 # space after quoting each one for sh(1) using shquote().
      8 #
      9 # The exec() function executes the command returned by getcmd()
     10 # using the system() builtin.
     11 
     12 function shquote(arg) {
     13 	if (!match(arg, "[|&;<>()$`\\\"' 	\n*?[#~=%]"))
     14 		return arg;
     15 	gsub("'", "'\\''", arg);
     16 	return "'" arg "'";
     17 }
     18 
     19 function getcmd(argv, _) {
     20 	_ = shquote(argv[1]);
     21 	for (i = 2; argv[i]; ++i)
     22 		_ = _ " " shquote(argv[i]);
     23 	return _;
     24 }
     25 
     26 function exec(argv) {
     27 	return system(getcmd(argv));
     28 }