HOW To ASSIGN THE OUT PUT OF THIS EXECUTIOn TO VARIABLE - shell

I am newbie in shell script , may be stupid query to experts, I am using following code to remove leading and trailing spaces from value, how do I assign output of echo variable to StringVar variable again or to other Variable. I am using ksh shell.
StringVar= ' abc '
echo StringVar | awk '{$1=$1};1'

x=$(command)
where x is the variable to which you want to assign the output of the command.
In your case, do not give space before or after the assignment operator =
StringVar=' abc '
x=$(echo "$StringVar" | awk '{$1=$1};1')

Related

How to prepend to a string that comes out of a pipe

I have two strings saved in a bash variable delimited by :. I want to get extract the second string, prepend that with THIS_VAR= and append it to a file named saved.txt
For example if myVar="abc:pqr", THIS_VAR=pqr should be appended to saved.txt.
This is what I have so far,
myVar="abc:pqr"
echo $myVar | cut -d ':' -f 2 >> saved.txt
How do I prepend THIS_VAR=?
printf 'THIS_VAR=%q\n' "${myVar#*:}"
See Shell Parameter Expansion and run help printf.
The more general solution in addition to #konsolebox's answer is piping into a compound statement, where you can perform arbitrary operations:
echo This is in the middle | {
echo This is first
cat
echo This is last
}

How to extract a variable length substring using ksh

I need to extract a variable length sub string using Korn shell on Linux.
Sample String: "SID_LIST_ORADBPOC1LSN ="
Need to extract substring: "ORADBPOC1LSN"
Note: The sample substring is of variable length.
Thanks in advance.
FR
With pure bash's parameter expansion capability.
var="SID_LIST_ORADBPOC1LSN =" ##Creating shell variable here.
temp1="${var##*_}" ##Removing everything till _ ## for longest match here.
echo "${temp1/ =/}" ##Substituting space = with null here.
ORADBPOC1LSN
I am printing value of temp1's parameter expansion, you could save this into variable too as per your need.
OR if you want to do it in a single awk or so then try:
echo "$var" | awk -F'_| ' '{print $3}'

Set string with special character in unix variable

I need to store in a unix variable a string that can contain special characters.
In my case I am decrypting a text that returns to me Her$p7 that I need to store. Obviously that result can be any string (example i*Fi+K'7).
Would they know how I can save that result that I throw in a variable and use it like this
var=Her$p7
echo var
by example?
I use this for retrieve the string
echo "`${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'`"|perl -e print
The syntax is correct, if not robust, and the issue is in understanding string interpolation. In the standard string context, the dollar sign signifies to the shell that it's about to interpret a variable. Generally, this means to replace the variable with the value of the variable. Consider:
$ t1=Her$p7
$ t2="Her$p7"
$ t3='Her$p7'
$ t4="$(echo 'Her$p7')"
$ echo "t1: $t1; t2: $t2; t3: $t3, t4: $t4"
t1: Her; t2: Her; t3: Her$p7; t4: Her$p7
Note that while setting t1 (the first line) and t2, $p7 was interpreted as a variable (which you had not set), and thus was consequently replaced with it's value (empty/nothing). So, t1 and t2 were set to the value Her<nothing> -> Her.
In the third case, we used single quotes to tell the shell "no interpolation please; I mean strictly what I say". So, t3 is set to exactly the string you typed.
In the last case, we use the subshell operator ($( ... )) to set the variable t4 to the output of subshell command. In this case, we use double quotes to make sure we capture the entire output, but because we aren't typing the variable $p7, the shell won't interpolate the output of the command.
So, you should be good to go with something like:
$ yourVar=$(echo "`${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'`" | perl -e print)
Moving into the last decade, we could clean that up slightly by not using backticks for subshell operations:
$ yourVar=$(echo "$(${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}')" | perl -e print)

Set bash variable equal to result of string where newlines are replaced by spaces

I have a variable equal to a string, which is a series of key/value pairs separated by newlines.
I want to then replace these newline characters with spaces, and set a new variable equal to the result
From various answers on the internet I've arrived at the following:
#test.txt has the content:
#test=example
#what=s0omething
vars="$(cat ./test.txt)"
formattedVars= $("$vars" | tr '\n' ' ')
echo "$taliskerEnvVars"
Problem is when I try to set formattedVars it tries to execute the second line:
script.sh: line 7: test=example
what=s0omething: command not found
I just want formattedVars to equal test=example what=s0omething
What trick am I missing?
Change your line to:
formattedVars=$(tr '\n' ' ' <<< "$secretsContent")
Notice the space of = in your code, which is not permitted in assignment statements.
I see that you are not setting secretsContent in your code, you are setting vars instead.
If possible, use an array to hold contents of the file:
readarray -t vars < ./test.txt # bash 4
or
# bash 3.x
declare -a vars
while IFS= read -r line; do
vars+=( "$line" )
done < ./test.txt
Then you can do what you need with the array. You can make your space-separated list with
formattedVars="${vars[*]}"
, but consider whether you need to. If the goal is to use them as a pre-command modifier, use, for instance,
"${vars[#]}" my_command arg1 arg2

how to find the position of a string in a file in unix shell script

Can you please help me solve this puzzle? I am trying to print the location of a string (i.e., line #) in a file, first to the std output, and then capture that value in a variable to be used later. The string is “my string”, the file name is “myFile” which is defined as follows:
this is first line
this is second line
this is my string on the third line
this is fourth line
the end
Now, when I use this command directly at the command prompt:
% awk ‘s=index($0, “my string”) { print “line=” NR, “position= ” s}’ myFile
I get exactly the result I want:
% line= 3, position= 9
My question is: if I define a variable VAR=”my string”, why can’t I get the same result when I do this:
% awk ‘s=index($0, $VAR) { print “line=” NR, “position= ” s}’ myFile
It just won’t work!! I even tried putting the $VAR in quotation marks, to no avail? I tried using VAR (without the $ sign), no luck. I tried everything I could possibly think of ... Am I missing something?
awk variables are not the same as shell variables. You need to define them with the -v flag
For example:
$ awk -v var="..." '$0~var{print NR}' file
will print the line number(s) of pattern matches. Or for your case with the index
$ awk -v var="$Var" 'p=index($0,var){print NR,p}' file
using all uppercase may not be good convention since you may accidentally overwrite other variables.
to capture the output into a shell variable
$ info=$(awk ...)
for multi line output assignment to shell array, you can do
$ values=( $(awk ...) ); echo ${values[0]}
however, if the output contains more than one field, it will be assigned it's own array index. You can change it with setting the IFS variable, such as
$ IFS=$(echo -en "\n\b"); values=( $(awk ...) )
which will capture the complete lines as the array values.

Resources