ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sorry to be asking such a simple question here, but I've been googling for an hour already and can't seem to find the answer, perhaps I'm not searching for the right thing.
I'm working on an AWK script, and I need it to execute a shell command and assign the output of that command to a variable for use inside the AWK script itself.
If I were writing a bash script or executing it on the command line, it would go like this:
I've tried using the system("./distance "coordinates) thing, but from what I've read, that only outputs an exit status. The distance script did invoke with this attempt, but the output was not assigned to a variable, it was just echoed to the terminal.
From the man page example, I was led to believe I needed to bracket my variable name, but that's what caused the syntax error. That's what I meant. Thanks for your help, though. It's much appreciated.
If your requirements are more complex, here's the program I keep in /usr/share/awk so I can include it when I need to access system commands:
Code:
# cat /usr/share/awk/command_functions.gawk
##############################################################################################
#
# Functions defined in this file:
# define_sucmd()
# Sets the (global) variable "sucmd" to null or "sudo "
# run_cmd(cmd)
# Runs "cmd," returns error code (if any)
# (See below for optional arguments)
# su_run_cmd(cmd)
# Runs "cmd" as "root" and returns error code (if any)
# (See below for optional arguments)
# cmd_result(cmd, result_array)
# Runs "cmd," returns result_array[1] - pass result_array if more results are desired.
# (See below for other optional arguments.)
# su_cmd_result(cmd, result_array)
# Runs "cmd" as "root" and returns result_array[1] - pass result_array if more results are desired.
# (See below for other optional arguments.)
#
# NOTE: This code assumes that the global variable "exe" will contain the name of the program
# (or the parent thereof) for display in some error messages.
#
##############################################################################################
##############################################################################################
#
# Define the prefix, if any, needed to run commands as root.
#
##############################################################################################
function define_sucmd( id)
{
if (! sucmd) {
# Are we already running as "root"
id=cmd_result("id -u")
if (id == 0) {
return
}
}
# If here we need "sudo" access. Note: If "sudo" times out, the password prompt will be re-displayed
print "This program requires "root" access. Enter your password if prompted."
run_cmd("sudo echo \"O.K.\"","Aborting",255)
sucmd="sudo "
}
##############################################################################################
#
# Run a command.
# Print a message to stderr and exit on failure if exit_code != 0
# Note: Only the system() return code is returned. See cmd_result() for string/array returns
#
#############################################################################################
function run_cmd(\
cmd, # Command to be run\
# Optional arguments
error_message,# Message to print if the command fails. Default: no message\
exit_code, # Exit code to use if the command fails. Default: Do not exit\
# Local variables
ret) # System call return value holder
{
ret=system(cmd)
if (ret) {
if (error_message != "") print exe ": " error_message > "/dev/stderr"
if (exit_code +0 != 0) exit(exit_code)
}
return ret
}
##############################################################################################
#
# Run a command as root.
# Note: Only the system() return code is returned. See cmd_result() for string/array returns
#
#############################################################################################
function su_run_cmd(\
cmd, # Command to be run\
# Optional arguments
error_message,# Message to print if the command fails. Default: no message\
exit_code) # Exit code to use if the command fails..Default: Do not exit\
{
define_sucmd()
return run_cmd(sucmd cmd, error_message, exit_code)
}
##############################################################################################
#
# Run a command and return its output. If more than one result is returned by the system,
# the return value will be the first value returned by the command.
#
# If more than one value is desired, pass the optional array name as the second argument
# and set the third argument to "list."
#
#############################################################################################
function cmd_result(\
cmd, # Command to execute\
# Optional arguments\
results, # Array in which the results should be returned\
expect, # Expected result (either "fail" or "list") Default: Single returned value\
error_message,# Error message if the command fails (or, if failure is expected, succeeds)\
# Local variables\
result_string,\
temp,\
n)
{
n = 0
result_string=""
while (cmd | getline temp) {
++n
result_string = ((result_string) ? result_string SUBSEP : "") temp
}
close(cmd)
if ( expect == "fail" && n > 0) {
if (error_message) {
print exe ": cmd_result(): " error_message > "/dev/stderr"
}
else {
print exe ": cmd_result(): " cmd " did not fail." > "/dev/stderr"
}
}
if ((n > 1) && (expect !="list")) {
print exe ": Warning (cmd_result()): " cmd " returned " n " values."
}
split(result_string, results, SUBSEP)
return results[1]
}
##############################################################################################
#
# Run a command as "root" and return its output. If more than one result is returned by the
# system, the return value will be the first value returned by the command.
#
# If more than one value is desired, pass the optional array name as the second argument
# and set the third argument to "list."
#
#############################################################################################
function su_cmd_results(\
cmd, # Command to execute\
# Optional arguments\
results, # Array in which the results should be returned\
expect, # Expected result (either "fail" or "list") Default: Single returned value\
error_message)# Error message if the command fails (or, if failure is expected, succeeds)\
{
define_sucmd()
return cmd_result(sucmd cmd, results, expect, error_message)
}
Note that this code is not portable to standard AWK since it uses gawk extensions.
From the man page example, I was led to believe I needed to bracket my variable name, but that's what caused the syntax error. That's what I meant. Thanks for your help, though. It's much appreciated.
In most GNU man pages I've come across square brackets indicate
an optional parameter, and are never used literally (unless they
explicitly are indicated to have syntactial meaning, e.g. in
regular expressions).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.