Here I am taking the text input from the YAD UI field, and then I am writing that text into a file solc.c, but that text contains some \n characters, which are directly being written into file solc.c.
#!/bin/bash language=$(yad --form --field="Select Language:CB" "C!C++" --button="OK:0" --button="Cancel:1" --width=300 --height=100) if [ $? -eq 0 ]; then selected_language=$(echo "$language" | awk -F"|" '{print $1}') code=$(yad --form --field="Your Code:TXT" --title="Problem-A Solution" --button="OK:0" --button="Cancel:1" --width=800 --height=600) if [ $? -eq 0 ]; then if [ "$selected_language" = "C" ]; then "${code%?}" > "solc.c" gcc solc.c elif [ "$selected_language" = "C++" ]; then "${code%?}" > "sol1.cpp" g++ sol1.cpp else echo "Invalid language selected." exit 1 fi Current file solc.c:
#include <stdio.h>\n\nint main() {\n long long a, b;\n scanf("%lld %lld", &a, &b);\n printf("%lld\\n", a + b);\n return 0;\n}\n return 0;\n }\n Expected:
#include <stdio.h> int main() { long long a, b; scanf("%lld%lld", &a, &b); printf("%lld\n", a + b); return 0; } It worked sometime, but for the same code, it is not working now, and same it follows with sol1.cpp.
Also mention, if there is any other hacks to get rid of the \n being written into the file.
code(eg, output fromtypeset -p code)"${code%?}" > "solc.c"says to execute whatever${code%?}returns and place the output insolc.c; you're sayingcodecontains the string#include ...but if bash tries to execute that you get an error message; so either${code}doesn't contain what you say it does or the script you've provided isn't your real script (eg, does your real script echo the variable -echo "${code%?}" > "solc.c"?)${code}end up generating two sets ofreturn 0;\n }\n(current contents ofsolc.c)?echoto process the\nas a linefeed tryecho -e "${code%?}"; you can do something similar withprintf's%b=>printf "%b" "${code%?}"; this would've been easier/quicker to point out had we been presented with a valid, working script