0

This is an image of what I'm asking for I am using the following -echo- in a script and after I execute, the output format is as shown below:

`echo -e "UPDATE table1 SET table1_f1='$Fname' ,table1_f2='$Lname' where table1_f3='$id';\ncommit;" >> $OutputFile` 

output: UPDATE table1 SET table1_f1='Fname' ,table1_f2='Lname' where table1_f3='id '; the '; is appearing on a new line, why is that happening?

aynber
  • 22,380
  • 8
  • 50
  • 63
  • https://stackoverflow.com/a/20233998/2955337 ; is treated an end of command character. escape it with a backslash – sleepyhead Dec 27 '21 at 07:26
  • Could you clarify the question and source a bit. I think some is missing or conflicting with the SO formatting. The `;` you say is on a lower line, isn't shown at all here. It's not clear (to me) if the very first backtick character is part of the script line or not, as the character has special meaning both in scripts and here on SO. Also, by *"echo on a script"*, i imagine you mean *"echo inside a script"* ? – Raxi Dec 27 '21 at 07:27
  • @sleepyhead It's inside a quoted string, it doesn't need to be escaped. – Barmar Dec 27 '21 at 07:29
  • I suspect the problem is that `$id` contains a newline. – Barmar Dec 27 '21 at 07:30
  • @Raxi I provided a screenshot of the problem, and yes echo inside a script – Omar Yahya Dec 27 '21 at 07:39
  • @Barmar $id is referring to a variable – Omar Yahya Dec 27 '21 at 07:40
  • Please format the codes in your description properly. – konsolebox Dec 27 '21 at 07:51
  • @konsolebox I provided a screenshot of the codes, can please check it. – Omar Yahya Dec 27 '21 at 08:52
  • Yeah but looks like somebody gave you the right answer. – konsolebox Dec 27 '21 at 10:58

1 Answers1

1

The variable $id in your shell script actually contains that newline (\n or \r\n) at the end; so there isn't really anything wrong in the part of the script you've shown here.

This effect is pretty common if the variable is created based on external commands (update:) or by reading external files as you are here.

For simple values, one way to strip the newline off the end of the value, prior to using it in your echo is:

id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );

or for scripts that already rely on a particular bash IFS value:

OLDIFS="${IFS}"; 
IFS=$'\n\t '; 
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
IFS="${OLDIFS}";
Raxi
  • 2,452
  • 1
  • 6
  • 10
  • I tried to set `IFS=$'\n';` but the issue is still there. – Omar Yahya Dec 27 '21 at 08:49
  • This is how I am defining the `id` variable: `while IFS='|'; read Fname Lname id` It get its value from the input file – Omar Yahya Dec 27 '21 at 08:51
  • does the input all look like `A|B|C` ? or can there be more `|` – Raxi Dec 27 '21 at 08:52
  • and try: `while IFS=$'\n|'; read Fname Lname id` – Raxi Dec 27 '21 at 08:53
  • It is `A|B|C`, I am using one pipe between every value – Omar Yahya Dec 27 '21 at 08:54
  • I tried `while IFS=$'\n|'; read Fname Lname id` as you suggested and still got the same issue :( – Omar Yahya Dec 27 '21 at 08:56
  • Try adding `id="$( echo ${id} )";` inside that loop (so ~2 lines lower; the line after `do`) – Raxi Dec 27 '21 at 09:01
  • `do` `echo -e "UPDATE table1 SET table1_f1='$Fname' ,table1_f2='$Lname' where table1_f3='$id';\ncommit;" >> $OutputFile` where exactly? – Omar Yahya Dec 27 '21 at 09:04
  • above your `echo` line – Raxi Dec 27 '21 at 09:21
  • still the same issue, it seems like there is a problem with the input file; although I have seen other scripts with similar input files working fine. – Omar Yahya Dec 27 '21 at 10:11
  • I really appreciate your help @Raxi, if I find any solution I will update here. – Omar Yahya Dec 27 '21 at 10:12
  • Strange, it works for me when i cobble these bits together; 1 more thing you could try is: `OLDIFS="${IFS}"; IFS=$'\n\t '; id=$( echo ${id} | tr -s '\r' '' | tr -s '\n' '' ); IFS="${OLDIFS}";` (again, placing it inside the loop, just before your `echo -e "UPDATE...` – Raxi Dec 27 '21 at 10:36
  • @Omar Yahya I suggest opening another question about why your `id` variable stores a newline. Your original question has been answered. – konsolebox Dec 27 '21 at 11:00
  • It worked but the id is not between `''`, all the output on one line but the id was ignored. As I said I think the problem is with the input file as I get the data from an Excel sheet and copy it to a txt file. I use notepad++ to prepare my input file – Omar Yahya Dec 27 '21 at 11:03
  • I think it was just that there was a `\r\n` newline in the file (windows style). not just a `\n` (unix/linux style) as i expected earlier; but fundementally still the same problem. -- i'll update the answer with the working version – Raxi Dec 27 '21 at 11:25