bash script to compile and run test cases - c++

Completely new to bash scripts, trying to make a script to compile and run all cpp files that start with "blah". What I have so far is
#1/bin/bash
for i in blah*.cpp
do
if g++ "$i" -o "${i%.cpp}.out"; then
/Users/[directory]/Desktop/"${i%.cpp}.out" #to run each testcase
else
echo "failed"
fi
done
I need to know how many blah files succeeded in running (I can either make the c++ programs return 1 or just cout "succeeded", any way to tell would be fine), and I'm having trouble figuring out how to get the return status of each program or how to use the output from each program run and see if it says "succeeded".

Have your test case return 1 for success and 0 for failure. The return value can then be used as follows:
counter=0
for i in blah*.cpp
do
if g++ "$i" -o "${i%.cpp}.out"; then
if /Users/[directory]/Desktop/"${i%.cpp}.out" ; then
counter=$((counter+1))
else
echo "failed to run"
fi
else
echo "failed to build"
fi
done
echo "$counter tests succeeded"

Related

Auto input in VS code terminal from input.txt whenever compiled

Generally we need to type the input after running any file where we have std::cin, like the c++ code in below
int M,N;
cin>>M>>N;
int i,a[M],b[N];
for(i=0;i<M;i++)
{
cin>>a[i];
}
for(i=0;i<N;i++)
{
cin>>b[i];
}
Solution ob;
cout<<ob.countPairs(a, b, M, N)<<endl;
I just don't like to enter the same big input every time. So I want to automate this process for the same input, say I save my input in a file called input.txt and after running the file, it should take the input from input.txt and output the results.
Ofc saving input to the clipboard is one way but I might want to copy other things while coding and copy-pasting is itself is again one small job.
I use VS code editor in ubuntu and run code in terminal using coderunner extension.
Scripting
Write your long input into a file, input.txt:
the quick brown fox jumped over the lazy dog
Use a bash script, script.sh:
# Compile your program, ie:
clang++ source.cpp -o application
# Check compilation succeeded
if [[ $? -ne 0 ]]; then
echo "compilation failed"
exit 1
fi
# Pipe your input into the application
cat input.txt | ./application
Finally, invoke your script:
$ bash script.sh
To Read:
Piping input to applications
Return values in bash

What is the purpose of echo command after executing a program?

Why do we exit issuing an echo command while executing a program?
As far as I know, the echo command for console windows is:
$ echo $
And for windows is:
$ echo %ERRORLEVEL%
When the child process ends, the set variable ERRORLEVEL contains an integer returned by it. ERRORLEVEL is measured 0 if the process was successful (i.e. return 0). OTOH, 1 or greater if the process encountered an error.
However, I don't know what do you exactly mean by:
echo command after executing a program
I never ran this command neither I've seen anywhere.

Library compiled to architecture x64 with error in Arm architecture

I'm developing a C++ library that has a piece of shell script code that return the name of a specific serial port. When I run this script in console either X64 desktop or Arm enviorment the script returns the right answer. My problem ocur when I execute the same script inside of the library, the returns shows bad formed string like ÈÛT¶ÈÛT¶¨a , but the expected is /dev/ttyACM0.
The script that run inside of library:
Script
bash -c 'for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev);do(syspath="${sysdevpath%/dev}";devname="$(udevadm info -q name -p $syspath)";[[ "$devname" == "bus/"* ]]&& continue;teste="$(udevadm info -q property --export -p $syspath | grep -i "company_name")";if [[ ! -z "${teste// }" && $devname == *"ttyACM"* ]]; then echo "/dev/$devname";fi);done;' 2> /dev/null
The following piece of code is used to save the content returned by the script into a file.
code c++
pfFile = fopen(CONFIG_FILE, "w+");
fwrite(result,strlen(result), 1, pfFile);
fclose(pfFile);
return 0;
Besides you didn't include what is result and where it comes from in your C++ code; you selected the hardest way to do this. Code running shell scripts inside a library most likely cause nothing but headaches.
Basically you can create an udev rule for your device to create an unique and stable file in /dev to access it. You can create one like this one in the ArchWiki
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", SUBSYSTEMS=="usb", ATTRS{idVendor}=="05a9", ATTRS{idProduct}=="4519", SYMLINK+="video-cam1"

Forcing the environment to bash when executing shell command in C++

I'm creating an application in C++ that can execute some commands shell to get informations about the system. The problem occurs when I use in my script code something like [[ "$devname" == "bus/"* ]]&& continue;, executing this command returns error, probably because when executing the sheel script the environment are in dash instead of bash. I tried to execute the command with #!/usr/bin/env bash but don't work. The full command is hardcoded inline and I'm avoiding the use of a shell script file.
If you have a command like this, say:
[[ "$devname" == "bus/"* ]] && hostname || echo "no"
You can run it this way:
bash -c '[[ "$devname" == "bus/"* ]] && hostname || echo "no"'
So if you just build one string that contains the above, you can run it using popen() or whatever. In C++ you'll have to escape the inner quotes if you use the above literally, so:
const char* command = "bash -c '[[ \"$devname\" == \"bus/\"* ]] && hostname || echo \"no\"'";
You have two options:
1) Explicitly set SHELL via putenv():
putenv("SHELL=/bin/bash");
execl(...);
2) Explicitly execute /bin/bash, instead of relying on the hashbang:
execl("/bin/bash", script.c_str(), NULL);
// script is the script you're trying to execute.

Informatica Command Task

I asked this question on the Informatica forums, but no one knew the answer, so now I'm coming here for some help.
To my understanding, anything in a "post-session success command" field should be passed to the appropriate command interface and executed. However, when I use an IF statement, it fails. Any ideas?
"IF 1==1 echo.bob >> f:\filename.txt"
This works when I type it manually into the terminal (DOS in this case). But when I throw it into a reusable command task, I get this:
ERROR
POST-SESS
CMN_1949
Error: [Pre/Post Session Command] Process id 2996. The shell command failed with exit code 1.
PS: Using 9.5.1 Hotfix 1
I think the problem is not in how Informatica executes commands. The problem lies in how DOS return error codes, and specifically that some commands, like IF and ECHO does'nt. (The return code Informatica picks up from DOS can be seen with echo %ERRORLEVEL% in DOS, and I'll use the name DOS here for convenience even though under Windows now this is'nt strictly correct)
Run these commands in succession:
REM "cd" sets ERRORLEVEL => ERRORLEVEL is set to 0
cd c:\
echo %ERRORLEVEL%
REM "echo" does not set ERRORLEVEL => ERRORLEVEL is left unchanged
echo.bob >> c:\filename.txt
echo %ERRORLEVEL%
REM "echo" does not set ERRORLEVEL => ERRORLEVEL is left unchanged
echo.bob >> c:\thisdirdontexist\filename.txt
echo %ERRORLEVEL%
The first CD set a return code, in this case to 0.
The following ECHO (with or without the IF test) does not change the return code, thus it remains 0 even though the last ECHO fails.
If the first CD command would have returned an error;
#echo off
REM "cd" sets ERRORLEVEL => ERRORLEVEL is set to 1
cd xxxxxx
echo %ERRORLEVEL%
then all the subsequent ECHO would return 1 and Informatica would fail them both.
This said it is still strange since each post-session success command in Informatica is executed under its own cmd-shell, so the initial ERRORLEVEL for every command should allways be 0. I can't explain that and unfortunately I can't actually test this in Informatica as we run under UNIX, but I'm pretty sure this is at least part of the problem.
To get around the problem you should make sure that you set the "Fail task if any command fails" option on the property-tab. This makes Informatica use the cmd/c option and since this set a proper return code Informatica should be able to pick up the error (or success) correctly. If this still doesn't work properly try change the command yourself to:
cmd /c "IF 1==1 echo.uncle >> c:\filename.txt"