Bash Whitespace being calculated - bash

I am having issues with this script taking using the whitespace from my file that contains a list of words with each word on a separate line, I have double check and after each word there is a simple (return) no space yet the script is still calculating with whitespace
ok at that hash then type in:
"hello
"
So hello followed by enter to start a new line. Look at that hash value
That is what I am getting a hash value that is a sting + the enter. As a result my script isnt working the way it is supposed to. Can anyone help.

echo outputs with enter
You can use
echo -n "$test2" | sha256sum

Related

in Ksh, why does the last (empty) line of my multi-line string disappears when saving it in a variable?

While implementing a script, I am facing the following issue :
when putting the multi-line result of a command into a variable, it seems the last (empty) line of my multi-line string disappear.
This line is "empty", but however, I can not lose the carriage return it contains (because I am concatenating blocks of code saved in DB and containing "\n" character into a human-readable string... If I lose some of the "\n", I will lose a part of my code indentation)
Here is the code to illustrate my issue :
test="A
B
";
test2=`echo "$test"`;
echo "||$test2||";
This returns
||A
B||
while I was expecting :
||A
B
||
--> the last (empty) line has disappeared... and a carriage return is thus missing in my human-readable code.
This issue only occurs when the last line of my multi-line string is empty...
Do you know
Why this last line disappears ?
How I can ensure my last empty line is saved in my multi-line string variable ?
Note that I can of course not use the easiest solution
test2="$test";
because the complete process is rather :
test="^A\n\nB\n^"
test2="`echo "$test" | sed -e 's/\^//g'`";
but I tried to simplify the issue the most I could.
Command substitutions always trim trailing newlines -- that's in accordance with design and specification. If you don't want that, you can append a fixed sigil character to your output and trim it, such that the newlines you want to preserve are before the sigil:
test="A
B
"
test_wip=$(printf '%sEND' "$test")
test2=${test_wip%END}
Instead of trying to work around the issues that arise from assigning the output from echo to a variable (eg, stripping of trailing \n's), consider using ksh's built in string processing in this case, eg:
$ test="^A\n\nB\n^"
$ test2="${test//^}"
$ echo "||${test2}||"
||A
B
||
//^ : remove all ^ characters

Appending string to the variable

I read some file line by looking for some specific string. When I find I assign it to the var.
But when I try to append to that string I have problem. Instead of add to the end of var i get var where characters at the beginning are replaced by new characters.
Example:
echo $fileToGet
newVar=$fileToGet".xml"
echo $newVar
Output:
c024z160205
.xmlz160205
And what I want is: c024z160205.xml
I think I tried everything what is on Stack, several ways of appending but nothing works.
The problem was \r at the end of var. Before assigning line to var fileToGet I do something like:
newVar=$(echo "$fileToGet" | tr -d '\r')
and after concatenation I have what I want.
I edit this answer because it contains false informations. What really was the problem #chepner described in comment: 'The terminal simply displays any characters following the carriage return at the beginning of the line, overwriting the earlier characters.'
Thanks!
Your code is working for me, try using:
newVar="${fileToGet}.xml"
Instead of newVar=$fileToGet".xml"; you gotta put the variable also inside the quotes.
Tell me if it works now.

Remove part of a string using KornShell

I have this string DFUB1AG.T1310LC.C140206.XIYG000.FCIPHE31 and I need to remove the first part of it. How can I do this?
As has been asked here is what I want to achieve.
Want to get this string that is separated by "."
DFUB1AG.T1310LC.C140206.XIYG000.FCIPHE31
And want to remove the first part of it to get the result as below
T1310LC.C140206.XIYG000.FCIPHE31
I have already achieved it by doing this way:
Okay guys I got it done by doing this.
# var=DFUB1AG.T1310LC.C140206.XIYG000.FCIPHE31
# var=${var#*.}
# echo $var
# T1310LC.C140206.XIYG000.FCIPHE31
If STRING is your variable, and you want to strip everything before first dot you can say STRING=${STRING#*.} ........Removes shortest match at beginning of string of any character followed by a dot.
echo "$VarWithYourString" | sed "s/^[^.]\{1,\}./"
or
sed "s/^[^.]\{1,\}./" YourFileInput

Use of the Internal Field Separator when capturing array data from a command in a bash script

When I run the command
git cherry origin/Server_Dev
in my git repository, I get a list of commits of the form
+ 95b117c39869a810595f1e169c64e728d2d7443d
+ e126f1b996ecf1d2a8cf744c74daa92cce338123
+ 869169a6cb0bbe8f1922838798580a1e74ec3884
+ 667819b617c88bd886dc2001f612b5c7a4d396c3
+ fd41328a84b0a127affa6fe4328c93e933de378c
+ cfe1807e5d4acc6b5e75f4463dadb3b1c957376f
This is a good thing.
I now want to execute this command from within a bash script and capture the output into an array using the following code:
commit_hashes=(`git cherry origin/Dev`)
echo ${commit_hashes[#]}
which yields the following output:
+ 95b117c39869a810595f1e169c64e728d2d7443d + e126f1b996ecf1d2a8cf744c74daa92cce338123 + 869169a6cb0bbe8f1922838798580a1e74ec3884 + 667819b617c88bd886dc2001f612b5c7a4d396c3 + fd41328a84b0a127affa6fe432
8c93e933de378c + cfe1807e5d4acc6b5e75f4463dadb3b1c957376f
This is not a good thing
My list of commits is being returned as a string which I must first break up before I can use it. After some searching I found out that if I add IFS="" to my script before the capturing of the data, my problems would be solved.
So I edited my code to read
IFS=""
commit_hashes=(`git cherry origin/Dev`)
echo ${commit_hashes[#]}
which output
+ 95b117c39869a810595f1e169c64e728d2d7443d
+ e126f1b996ecf1d2a8cf744c74daa92cce338123
+ 869169a6cb0bbe8f1922838798580a1e74ec3884
+ 667819b617c88bd886dc2001f612b5c7a4d396c3
+ fd41328a84b0a127affa6fe4328c93e933de378c
+ cfe1807e5d4acc6b5e75f4463dadb3b1c957376f
This completely ended my sense of reality.
I like to know why things are doing what they're doing, so after some more searching I found out that this is called the Internal Field Separator and it is used on Unix systems by command interpretors to figure where to break patterns up into tokens.
This I understand.
What I don't understand is
Why setting this variable to an empty string allowed it to handle my array data in a sane manner.
Why I had to set it so in the first place, instead of the interpretor realising it was dealing with array data and handling it appropriately.
What effect setting the Internal Field Separator to an empty string will have in the grand scheme of things, since by default it contains the characters for a space, a tab and a newline.
Some help in getting my head around these three points would be appreciated.
See man bash/Arrays. Everything is fine with your array. When you do
echo ${commit_hashes[#]}
echo shows all array elements on one line, as it is told to. When you say
for i in `seq 10`; do
echo ${commit_hashes[$i]}
done
you will see that just one entry per line is shown.
When you set IFS= to an empty string however, the result of git cherry isn't broken up into multiple values. The whole string including the newlines is assigned to the first array element. If you
echo ${commit_hashes[0]}
in your second case, you will see, that echo shows all output lines.

Can't seem to get a consistent substring in a batch file

This is a strange problem. I have a batch file where I have two arguments. I wish to check the first three characters of each. The first will substring fine, but the second will not. Here is an example:
SET FIRST_ARG=%1
SET SECOND_ARG=%2
ECHO first argument is %FIRST_ARG%
ECHO first substring is %FIRST_ARG :~1,3%
ECHO second argument is %SECOND_ARG%
ECHO second substring is %SECOND_ARG :~1,3%
The first two ECHO statements work fine and display my strings as they should. The ECHO statement "ECHO second argument is" shows the second argument as it should, but the last line that says "ECHO second substring is" returns nothing.
Have I missed something?
Thanks for any help.
Rob
Your issue is the space preceding the colons :. Using %FIRST_ARG:~1,3% and %SECOND_ARG:~1,3% should fix your issue.

Resources