How do I get the output of a command inside a taskfile variable? - taskfile

How do I get the output of a command inside a taskfile variable?

Instead of assigning a string to a variable, use a map and sh key to mention the command to be executed.
vars:
FOO: bar
CURRENT_DATE:
sh: date

Related

Show environment variable LD_LIBRARY_PATH

I have simple console application that prints environment variables:
int main(int argc, char **argv, char **envp)
{
printf("Scanning for LD_LIB_PATH\n");
for (char **env = envp; *env != 0; env++)
{
char *thisEnv = *env;
std::string s= *env;
if (s.find("LD_LIBRARY_PATH")!=std::string::npos)
{
printf("!!!!!!! %s\n", thisEnv);
}
printf("%s\n", thisEnv);
}
}
Before run executable I run script that sets LD_LIBRARY_PATH
export LD_LIBRARY_PATH=~/aaa/bbb/Debug:~/ccc/ddd/Debug
echo "searching:"
export | grep LD_LIBRARY
echo "done"
Script run fine with output:
exporting
searching:
declare -x LD_LIBRARY_PATH="/home/vicp/aaa/bbb/Debug:/home/vico/ccc/ddd/Debug"
done
I run executable and it finds many variables but no environment variable LD_LIB_PATH. Why?
UPD
As recommended I script . ./script.sh
Then double check with command:
export |grep LD_LIBRARY_PATH
Got output:
declare -x LD_LIBRARY_PATH="/home/vicp/aaa/bbb/Debug:/home/vico/ccc/ddd/Debug"
But still don't see LD_LIBRARY_PATH in my programm.
Depending on how you run the script, the env variable will only be added to the environment of the subshell running the script.
If you run the script like this:
$ ./script.sh
This will spawn a new shell wherein the script is run. The parent shell, i.e. the one you started the script from, will not be affected by what the script does. Changes to the environment will therefore not be visible (changing the working directory or similar will also not work).
If the script is intended to modify the environment of the current shell, the script needs to be sourced using the . or source commands:
$ . ./script.sh
$ source ./script.sh
From help source in bash:
Execute commands from a file in the current shell.
Then there seems to be a problem with the code:
As a commenter stated previousy, there are a lot of exclamation points in the success case printf. I presume these were meant to highlight the found variable name. Since they are outside of the opening quote of the format string, they are interpreted as logical not operators.
The result is that the format string literal (a pointer) is negated, resulting in false due to the number of operators. This usually maps to 0, which would mean the format string becomes a null pointer. What happens when handing a null pointer to printf as the format string depends, as far as I can tell, on the implementation. If it doesn't crash, it most certainly will not print anything however.
So the code may actually work, but there is no visible output due to that bug. To fix it, move the exclamation points into the format string, i.e. after the opening quote.
Look at the line printf(!!!!!!!"%s\n", thisEnv);
Change to printf("!!!! %s\n", thisEnv);
It has nothing to do with your C/C++ application.
try the following:
$ ./script.sh
$ echo $LD_LIBRARY_PATH
And you'll see that LD_LIBRARY_PATH is not set
When you launch your script, bash creates a new process with its environment inherited from the original bash process. In that newly created process, you set the process environment variable LD_LIBRARY_PATH=xxxx
When finalized your script.sh exits, and its environment dies with it.
Meaning the LD_LIBRARY_PATH is not set in your original shell.
As mentioned here above you need to run your script in the current shell
Either with . or with source.
I tested with your C/C++ and it works

How do I store the output into a variable or into a .txt file?

I am running the following code in Python 2.7:
values = os.system("bazel build tensorflow/examples/image_retraining:"
"label_image && bazel-bin/tensorflow/examples/image_retraining/label_image "
"--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt "
"--output_layer=final_result:0 --image=$HOME/Desktop/Image-3/image1.png")
print values
But for the values variable I am returned a 0. I believe this means that I am not getting any errors. How do I store the output into a variable or into a .txt file?
You can just redirect the output of the system call appending > output.txt to your command.
The output of the command will be in file output.txt in the directory where you invoke the command (likely the very same one you invoke your python script in).
Since I can't readily reproduce your command, I used a simple example - try to switch to Pyopen in the subprocess module:
from subprocess import Popen
proc = Popen(['ls', '-t'], stdout = open('/path/redir.txt', 'w'))
Here you run the command in square brackets and redirect the output from stdout i.e. the terminal to a file redir.txt.

getenv() does not return last backslash

I am trying to read a env variable using getenv() in C++,
lets say env variable is
CONTENT_PATH=C:\Users\rat\Documents\fix\plat\gen\content\
It is being set by a batch file before program runs, contents of batch file.
set mypath=%~dp0
set gameContent=%mypath%plat\gen\content\
:: setting CONTENT_PATH
setx -m CONTENT_PATH "%gameContent%"
during debugging in visual studio, I am getting value as
C:\Users\rat\Documents\fix\plat\gen\content"
the last '\' is not coming, even though if I manually set the env variable value using Control Panel->Advance system settings.

In GDB, how can I set 'exec-wrapper env' to multiple environmental variables?

In GDB you can set the environmental variables for a process using set exec-wrapper env 'MYENVVAR=...'. This works great, but I'm not sure how to set multiple ones - is there some sort of delimiter you have to use? I'd like to set both LD_PRELOAD and LD_LIBRARY_PATH for a process. How would I do this?
You can use
set exec-wrapper env VAR1=val1 VAR2=val2
to set multiple environment variables. The values should be appropriately quoted for your shell, so putting single quotes around them would be a good idea.
In slightly more detail:
The set exec-wrapper command sets a string variable to contain the rest of the command line.
When it comes time to run your executable, gdb does something like the following pseudo-code:
shell_cmd = "exec ";
if (exec_wrapper)
shell_cmd += exec_wrapper + " ";
shell_cmd += quote_shell_metacharacters(exec_file);
execl(getenv("SHELL"), "sh", "-c", shell_cmd, (char *)0);
So, exec-wrapper can be any command line that makes sense when preceded by "exec " in your shell.

How can I run multiple commands at once from windows command line using c++ / c?

how can I run multiple commands at once from windows command line?
i want to set a couple of env variables and use them in the c++ program.
Like:
set VAR=Hello and set VAR2=BYE
and same program should do:
echo %VAR% and echo %VAR2%
and the output should be:
Hello BYE
How to achieve this in c/c++ ? any way to do this using system() function ?
You may execute a number of shell commands with the & seperator:
echo %VAR% & echo %VAR2%
See this SO answer to get more details.
Edit:
Unfortunately this will put the output seperated in two lines.
Hello
BYE
However, there is a solution for that too:
SET /P Var=%VAR%<NUL & echo %VAR2%
will output
Hello BYE
Edit 2:
Do not use system(), better use the CreateProcess function which allows
you to set creation flags like CREATE_NO_WINDOW.
You can achieve this using & separator:
set VAR=Hello & set VAR2=BYE