Shell echo to file ending up on multiple lines? - shell

What I'm doing is: echo put $clientfilepath'client-'$clientversion-'.jar' >> ftp.ftp in a shell file.
Where $clientfilepath is: c:\\workspace\\project\\jack\\prj1\\target\\ and $clientversion is 1.0-snapshot
What I expect in ftp.ftp:
put
c:\\workspace\\project\\jack\\prj1\\target\\client-1.0-snapshot.jar
But what I'm getting is:
put c:\\workspace\\project\\jack\\prj1\\target\\
client-1.0-snapshot
.jar
I'm using double \ so nothing in the filepath should get treated as a special character.
So does anyone know what's happening?

echo put $clientfilepath'client-'$clientversion-'.jar'|tr '\n' '' >> ftp.ftp

You can simplify the quoting:
echo "put ${clientfilepath}client-${clientversion}-.jar" >> ftp.ftp
Try that to see if it helps with your problem. Also try printf instead of echo:
printf 'put %sclient-%s-.jar\n' "$clientfilepath" "$clientversion" >> ftp.ftp

Related

Is way to escape angle brackets in a text block directed to a file?

Using a bash script as a working example
#!/bin/bash
echo "\
<global_preferences>
...
</global_preferences>" >> global_prefs.xml
I tried the following that (IMHO) should have worked but didn't
(
^<config^>
^</config^>
) > test.xml
The following does work but is a PITA as the xml file is long
echo ^<config^> > test.xml
echo ^</config^> >> test.xml

Echo printing variables in a completely wrong order

I am trying to create a string with a query that will be save / send to another location, this string contains different variables.
The issue that I am having is that the echo of the variables are completely upside down and mix.
See code below:
tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt")
serverName="asdasd"
attQuery="$tokenID $serverName"
agentRegQuery="$./opt/mule/bin/amc_setup -H $attQuery"
echo TOKEN ID $tokenID
echo SERVER NAME $serverName
echo $attQuery
echo $agentRegQuery
Find below the output I am receiving:
TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
SERVER NAME asdasd
asdasdf-fa0e-4f08-87eb-418722872d80---46407
asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407
There's a carriage return character at the end of the tokenID variable, probably because /tempdir/tokenfile.txt is in DOS/Windows format (lines end with carriage return+linefeed), not unix (lines end with just linefeed). When you print tokenID by itself, it looks ok, but if you print something else after that on the same line, it winds up overwriting the first part of the line. So when you print $attQuery, it prints this:
29a6966f-fa0e-4f08-87eb-418722872d80---46407[carriage return]
asdasd
...but with the second line printed on top of the first, so it comes out as:
asdasdf-fa0e-4f08-87eb-418722872d80---46407
The solution is to either convert the file to unix format (dos2unix will do this if you have it), or remove the carriage return in your script. You can do it like this:
tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt" | tr -d '\r')
I think everything works as it should
echo TOKEN ID $tokenID -> TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
echo SERVER NAME $serverName -> SERVER NAME asdasd
echo $attQuery -> asdasdf-fa0e-4f08-87eb-418722872d80---46407
echo $agentRegQuery -> asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407
Why do you think something is wron here?
Best regards, Georg

how to write an empty line in file from a variable?

In Windows Command Line I normally write empty line in a file with
echo; >> file
However, what I have now is a variable
$param1%
If I want echo to write it in the file I have to do
echo %param1% >> file
HERE IS WHERE THE PROBLEM START :
If I'd like an empty like I'd make
set param1=;
However since the ; is not in contact with the echo word the command is
echo ; >> file
which write the ; in the file...
I need the variable to sometime contains text, and sometime nothing. How can I do it?
if "%param1%"=="" echo;>>file else echo %param1%>>file
If a param1 variable does not exist (the same as set "param1="), then %param1% results to:
In a .bat script: %param1% results to an empty string (a string of zero length);
In a CLI window: %param1% results to the %param1% string.
In a .bat script use (note no spaces surrounding %param1%)
>> file (echo;%param1%)
In a CLI window use
>>file (if not defined param1 (echo;) else echo;%param1%)
Note proper using of parentheses in if-else! For instance, check interesting result of next command:
if ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file
Output:
==>if ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file
==>type file
"THEN branch" else echo;"ELSE branch"

Shell: Printing "" in a file with echo

I want to add the following line:
%optflags "-O2"
In a file a.txt using shell.
When I write:
{ echo "%optflags "-O2""
} >> a.txt
It prints:
%optflags -O2
How can I get this right?
Escape the double quotes:
echo "%optflags \"-O2\""
or
Use single quotes:
echo '%optflags "-O2"'
Note: you can partly see what was going on with your original code if you check it in bash -x:
$ echo "%optflags "-O2""
+ echo '%optflags -O2'
It passed a single argument to echo.. Why? Well, it concatenated "%optflags ", -O2, and "" (the empty string) to a single argument.

Create HTML File with commandline

I'm trying to make a HTML file with command line, but there is one problem. I need the quotes otherwise the echo doesn't work. But now the quotes are also displayed on the HTML page. This is my code:
FindStr "BUILD SUCCESSFUL" test1.txt
echo "<html><body><table><tr><td>Plugin</td><td>Resultaten</td></tr>" > goedje.html
if %ERRORLEVEL% ==1 (echo "<tr BGCOLOR="#FF0000"><td>Build</td><td>Fout!</td></tr>" >> goedje.html ) else (echo "<tr BGCOLOR="#00FF00"><td>Build</td><td>Gelukt!</td></tr>" >> goedje.html)
echo "</table></body></html>" >> goedje.html
If i'm doing it without the quotes then the program doesn't run!
In windows, you can use ^ to escape < and >
C:\> echo ^<html^>...^</html^> > result.html
I quess this can be caused by the shell you are using. Your command works for me. You can try using ' ' quotes instead of " " ones.
Basically what you need to do is to skip the special characters in the shell..
For eg : if you want to print " you should use \"
Like that so many characters need to be skipped using some special sequences
check this link for such details
http://www.grymoire.com/Unix/Quote.html
Hope this will help you..
You can also refer
http://steve-parker.org/sh/escape.shtml
Try this code
FindStr "BUILD SUCCESSFUL" test1.txt
echo "<html><body><table><tr><td>Plugin</td><td>Resultaten</td></tr>" > goedje.html
if %ERRORLEVEL% ==1 (echo "<tr BGCOLOR=\"#FF0000\"><td>Build</td><td>Fout\!</td></tr>" >> goedje.html ) else (echo "<tr BGCOLOR=\"#00FF00\"><td>Build</td><td>Gelukt\!</td></tr>" >> goedje.html)
echo "</table></body></html>" >> goedje.html

Resources