How to extract value from a string in shell script? [duplicate] - shell

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 8 months ago.
I have this environment variable
echo $DT_CUSTOM_PROP
returns
APPCODE=IK22 ENVIRONMENT=DEV APPLICATION=xxxxx-xxxxx-xxxxx-xxxxx ANOTHERKEY=ANOTHERVALUE
How can I get APPLICATION from it?
So that if I do echo $APPLICATION I get xxxxx-xxxxx-xxxxx-xxxxx

Given:
DT_CUSTOM_PROP='APPCODE=IK22 ENVIRONMENT=DEV APPLICATION=xxxxx-xxxxx-xxxxx-xxxxx ANOTHERKEY=ANOTHERVALUE'
You can do it with parameter expansion in a 2-step process:
tmp=${DT_CUSTOM_PROP#*APPLICATION=}
application=${tmp%% *} # ==> xxxxx-xxxxx-xxxxx-xxxxx
Or with bash regex matching
if [[ $DT_CUSTOM_PROP =~ "APPLICATION="([^[:blank:]]+) ]]; then
application=${BASH_REMATCH[1]}
else
application="not found"
fi

Related

bash loop problem - output not as expected (returned command) [duplicate]

This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 8 months ago.
I have a script.sh
#!/bin/bash
for i in {1..$1}
do
echo $i
done
someone could explain me why if I try
./script.sh 10 my output is {1..10}?
expected
1
2
...
10
You're echoing the parameter that you're passing in instead of printing out the loop iteration. Try:
echo $i

How to echo ![](../images/a.png) in bash? [duplicate]

This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
How to escape history expansion exclamation mark ! inside a double quoted string?
(4 answers)
Closed 2 years ago.
I have tried so many variations to echo the following string:
![](../images/a.png)
my attempts
b="a.pdf"
$ echo -n "![](../images/""${b%.*}.png")
-bash: ![]: event not found
$ echo -n "\![](../images/""${b%.*}.png"\)
\![](../images/a.png)
How to get:
![](../images/a.png)

bash sh script always executes regardless of parameter [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 4 years ago.
I have the following script
#!/bin/bash
if [ $1=="1" ]
then
echo $1
fi
Whenever I run ./myscript.sh 0 it still prints "0". I am not sure why? It prints whatever I type in because the if executes. What would I need to change?
Add proper spaces, i.e. before and after == inside if condition
#!/bin/bash
if [ $1 == "1" ]
then
echo $1
fi

Bash conditional returning an error of bash: =: command not found [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
if [[ "$PROXY_URL"==https* ]]; then
echo "Woohoo"
else
echo "Woohoo"
fi
Running $PROXY_URL = "https://yolo" ; ./proxyEnv.sh gives me output of:
bash: =: command not found
Woohoo
What does the "bash: =: command not found" refer to?
Your string comparison should have spaces around the comparator:
if [[ "$PROXY_URL" == https* ]]; then
echo "Woohoo https"
else
echo "Woohoo no https"
fi
Also, that's not how you pass environment variables to bash scripts. You have two options:
PROXY_URL="https://yolo" ./proxyEnv.sh
or
export PROXY_URL="https://yolo"; ./proxyEnv.sh
The first option assigns (without the $) the value to the symbol and then uses that environment for the script (without the ; separating them). It only exists for the script.
The second option exports that symbol to the current environment, which the script inherits.

Unix shell scripting to reverse a string without rev [duplicate]

This question already has answers here:
reverse the order of characters in a string
(14 answers)
Closed 9 years ago.
I want to write a shell script that accepts a string as a command line argument and prints out what is entered in the reverse order with out using the rev command but rather reversing the letters one by one. how would I do that?
so like
if Flower is entered it will print out rewolF
Thanks
A good subject for bash parameter expansion :
#!/bin/bash
for ((i=${#1}; i>=0; i--)); do printf "${1:$i:1}"; done; echo
Example :
./script.sh foobar
Output :
raboof
s1='Flower'
a1=($(echo $s1|fold -w1 ))
for (( i=${#a1[#]}-1;i>=0;i--));do echo "$i=>${a1[i]}"; done
5=>r
4=>e
3=>w
2=>o
1=>l
0=>F

Resources