-4

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.

8
  • 1
    could you update the question to show the contents of code (eg, output from typeset -p code) Commented Feb 28, 2024 at 19:02
  • 3
    "${code%?}" > "solc.c" says to execute whatever ${code%?} returns and place the output in solc.c; you're saying code contains 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"?) Commented Feb 28, 2024 at 19:21
  • 1
    how does ${code} end up generating two sets of return 0;\n }\n (current contents of solc.c)? Commented Feb 28, 2024 at 19:25
  • 2
    We want you to update the question with a minimal reproducible example that demonstrates the problem you're asking for help to debug and nothing else, see How to Ask. Posting snippets of code that aren't your real code and can't possibly do what you say they do isn't useful for us to be able to help you debug your problem. Commented Feb 28, 2024 at 19:42
  • 1
    to get echo to process the \n as a linefeed try echo -e "${code%?}"; you can do something similar with printf's %b => printf "%b" "${code%?}"; this would've been easier/quicker to point out had we been presented with a valid, working script Commented Feb 28, 2024 at 19:59

1 Answer 1

2

Just convert the string "\n" into the actual character \n:

code=$(yad --form \ --field="Your Code:TXT" \ --title="Problem-A Solution" \ --button="OK:0" \ --button="Cancel:1" \ --width=800 \ --height=600 | sed 's/\\n/\n/g') 

You can now do (you really don't want to use echo for unsanitized input like that, see here; and your code was trying to execute the $code variable instead of saving to a file)

printf '%s\n' "$code" > solc.c 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.