$() removes trailing newlines from the output

This commit is contained in:
vrd
2025-06-06 18:00:38 +03:00
parent 6da2d6bc0f
commit 010b8bc699
5 changed files with 34 additions and 21 deletions
+2 -2
View File
@@ -3,9 +3,9 @@ set -e
# Test cppipe functions # Test cppipe functions
OKs=$(cppipe test/functions_test.cppipe 2>/dev/null | grep OK | wc -l) OKs=$(cppipe test/functions_test.cppipe 2>/dev/null | grep OK | wc -l)
if ! [ $OKs = 12 ] if ! [ $OKs = 13 ]
then then
echo "Functions test failed: EXPECTED 12 OKs, got $OKs" echo "Functions test failed: EXPECTED 13 OKs, got $OKs"
exit 1 exit 1
fi fi
echo Functions test OK! echo Functions test OK!
+2 -2
View File
@@ -82,7 +82,7 @@ private:
void exec(const Cmd&); /* todo: take Pending? */ void exec(const Cmd&); /* todo: take Pending? */
/* Execute the command, like the operator() */ /* Execute the command, like the operator() */
DeadProc run(Cmd&); DeadProc run(const Cmd&);
/* Run commands in sequence /* Run commands in sequence
shell: shell:
@@ -156,7 +156,7 @@ PendingCmd operator&(const PendingCmd&, const Cmd&);
// operator const char*() { return c_str();} // operator const char*() { return c_str();}
// }; // };
/* Execute a command and capture the output /* Execute a command and capture the output, remove trailing newlines like the shell version
shell: shell:
var=$(ls) var=$(ls)
becomes: becomes:
+11 -2
View File
@@ -141,8 +141,17 @@ inline std::string $(const PendingCmd& cmd)
while(read_count > 0); while(read_count > 0);
// p finished? // p finished?
// Remove trailing newlines
// i is on the past the end element, go back to end
int j = i - 1;
while(j > 0 && output[j] == '\n')
--j;
i = j + 1;
// erase the extra elements // erase the extra elements
output.erase(i, output.size() - i); output.erase(i, -1); // till the end
close(p.out); close(p.out);
@@ -154,7 +163,7 @@ inline void exec(const Cmd& cmd)
exec_or_die(cmd.argv.data()); exec_or_die(cmd.argv.data());
} }
DeadProc run(const Cmd& cmd) inline DeadProc run(const Cmd& cmd)
{ {
return cmd(); return cmd();
} }
+19 -14
View File
@@ -24,16 +24,17 @@ int main(int argc, char* argv[])
string out1 = $(ll + "src" | grep + "inl" | grep + "child"); string out1 = $(ll + "src" | grep + "inl" | grep + "child");
if(out1.find("childProcess.inl") != string::npos) if(out1.find("childProcess.inl") != string::npos)
cout << "OK 0/11" << endl; cout << "OK 0/12" << endl;
string out2 = $(echo + "abc" + "def"); string out2 = $(echo + "abc" + "def");
if(auto len = out2.size(); len != 8) // "abc def" = 7 chars, trailing newlines are stripped by $()
if(auto len = out2.size(); len != 7)
{ {
cerr << "FAILURE: unexpected output length " << len << endl; cerr << "FAILURE: unexpected output length " << len << endl;
exit(1); exit(1);
} }
Cmd success("echo", "OK 1/11"); Cmd success("echo", "OK 1/12");
Cmd fail("mkdir", "."); Cmd fail("mkdir", ".");
Cmd unexpected("echo", "FAILURE"); Cmd unexpected("echo", "FAILURE");
success && success &&
@@ -45,30 +46,34 @@ int main(int argc, char* argv[])
// unexpected && // unexpected &&
// unexpected; // unexpected;
Cmd write_file("echo", "Existing ", " ", "file. OK 2/11"); Cmd write_file("echo", "Existing ", " ", "file. OK 2/12");
write_file > "file.txt"; write_file > "file.txt";
grep + "Existing" < "file.txt"; grep + "Existing" < "file.txt";
echo + "Appended to file OK 3/11" >> "file.txt"; echo + "Appended to file OK 3/12" >> "file.txt";
grep + "Appended" < "file.txt" && grep + "Appended" < "file.txt" &&
rm + "file.txt" && rm + "file.txt" &&
fail || fail ||
Cmd("echo", "OK 4/11"); Cmd("echo", "OK 4/12");
echo + "OK 5/11" && echo + "OK 5/12" &&
echo + "OK 6/11", echo + "OK 6/12",
Cmd("echo", "OK 7/11"); Cmd("echo", "OK 7/12");
echo + "OK 8/11" & echo + "OK 8/12" &
echo + "OK 9/11" && echo + "OK 9/12" &&
echo + "OK 10/11"; echo + "OK 10/12";
// wait for all detached // wait for all detached
while(wait(nullptr) != -1); while(wait(nullptr) != -1);
exec( echo + "OK 11/11" ); Cmd run_OK = echo;
run_OK.append_args({ "OK 11/12" });
run( run_OK );
exec( echo + "OK 12/12" );
// todo // todo
// exec( echo + $(echo + "OK 11/11") ); // exec( echo + $(echo + "OK 11/12") );
} }
-1
View File
@@ -13,7 +13,6 @@ precompiled header cppipe.h
-Wno-unused-result in install.sh -Wno-unused-result in install.sh
file operations, lack of uniformity (C++ vs POSIX) file operations, lack of uniformity (C++ vs POSIX)
Check all return codes and report errors Check all return codes and report errors
shaded obj
uninstall uninstall
improve debug (the src file shown is the .ii) improve debug (the src file shown is the .ii)
no optimizations option no optimizations option