Define the value of a variable as the result of a command execution

-sharp!/usr/bin/env bash
time=$(date)
nginxConfigPath=$(nginx -t)
echo "datetime" $time
echo "config path" $nginxConfigPath

execute the above shell script file, and the result is

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
datetime Fri Nov 16 14:27:14 CST 2018

theoretically, the variable nginxConfigPath should have a value.
when the actual result is, the result of the nginx-t command is already displayed when it is defined.

Nov.22,2021

abc=$ (cmd) this way can only get standard output turn. The nginx output is standard error output, so it cannot be obtained.

the following experiments can verify

$ date >a.txt
$ cat a.txt
20181116  210120 CST


$ sudo nginx -t >a.txt  -sharp
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$ cat a.txt -sharp 


$ sudo nginx -t 2>a.txt   -sharp
$ cat a.txt 
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

if you want to get the standard error output, you can use the following way

nginxConfigPath=$(nginx -t 2>&1)
The

file descriptor 1 is the standard output (stdout). The
file descriptor 2 is the standard error (stderr).

here is one way to remember this structure (although it is not entirely accurate): first, 2 > 1 may look like a redirected stderr to stdout. However, it will actually be interpreted as "redirecting stderr to a file named 1". & indicates that it is followed by a file descriptor rather than a file name. So it becomes: 2 > & 1.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b42704-2c61d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b42704-2c61d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?