Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 7072

Removing "\n" character while writing into a file in a shell script

$
0
0

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/bashlanguage=$(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.


Viewing all articles
Browse latest Browse all 7072

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>