Remove a property in a YML file based on a condition in bash - bash

I have below piece of bash code to apply YML config in 2 environments.
cat "script/service.yml" \
| sed -e "s/_REGISTRY_/$_REGISTRY/g" \
| sed -e "s/_PROJECT_/$_PROJECT/g" \
| $oc apply -f -
Now I need to change this to stop applying certain property in one environment. I have a variable _PROJECT to identify the setup. I tried below.
My logic : if the _PROJECT is xyz I am trying to replace the YML property with empty line (remove property)
cat "script/service.yml" \
| sed -e "s/_REGISTRY_/$_REGISTRY/g" \
| sed -e "s/_PROJECT_/$_PROJECT/g" \
| if [ "$_PROJECT" = "xyz" ]; then
sed -e "s/^storage//g"
fi \
| $oc apply -f -
But this piece of bash code not working. How to correct this piece in order to satisfy my requirement?

Able to figured out. Just need to add if else block as below.
if [ "$_PROJECT" = "xyz" ]; then
cat "script/service.yml" \
| sed -e "s/_REGISTRY_/$_REGISTRY/g" \
| sed -e "s/_PROJECT_/$_PROJECT/g" \
|sed -e "s/storage//g" \
| $oc apply -f -
else
cat "script/service.yml" \
| sed -e "s/_REGISTRY_/$_REGISTRY/g" \
| sed -e "s/_PROJECT_/$_PROJECT/g" \
| $oc apply -f -
fi

Related

How to get a command variable inside another command variable?

Example here:
gitrepo=$(jq -r '.gitrepo' 0.json)
releasetag=$(curl --silent ""https://api.github.com/repos/\"$gitrepo\""/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
echo "$releasetag"
Used \" to escape characters.
0.json:
{
"type": "github-releases",
"gitrepo": "ipfs/go-ipfs"
}
How to put $gitrepo to work inside $releasetag?
Thanks in advance!
Bash variables expand inside quoted " strings.
gitrepo="$(jq -r '.gitrepo' 0.json)"
releasetag="$(
curl --silent "https://api.github.com/repos/$gitrepo/releases/latest" \
| grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
)"
echo "$releasetag"
Btw, as you are using jq to extract .gitrepo from 0.json, you could also use it in the exact same way to extract .tag_name from curl's output (instead of using grep and sed) like so:
gitrepo="$(jq -r '.gitrepo' 0.json)"
releasetag="$(
curl --silent "https://api.github.com/repos/$gitrepo/releases/latest" \
| jq -r '.tag_name'
)"
echo "$releasetag"
And to simplify it even further (depending on your use case), just write:
curl --silent "https://api.github.com/repos/$(jq -r '.gitrepo' 0.json)/releases/latest" \
| jq -r '.tag_name'

How to `rm` files as awk action?

This one-liner:
sudo df /tmp \
| grep '/tmp' \
| expand - \
| cut -d " " -f 12 \
| sed 's/%//' \
| awk '{ if ($1<50)
$("sudo rm -rf /path/to/trash/files/*")
}'
seems to have no effect, while this:
sudo df /tmp \
| grep '/tmp' \
| expand - \
| cut -d " " -f 12 \
| sed 's/%//' \
| awk '{ if ($1<50)
print $1
}'
prints the percentage of disk used for tmp.
(The end goal is to flip the comparison around to ($1>50), but for testing I'm trying <.)
You don't really need so many commands in pipeline with awk.
You can just use:
df /tmp | awk 'NR>1 && $5+0 > 50 {system ("date")}'
Here change date command to something else that you need to run there.

Sed error "command a expects \ followed by text"

Here is my script:
openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed 's/ECHO: \"\[LC\] //' |
sed 's/"$//' |
sed '$a;' >./2d_$1
That output:
sed: 1: "$a;": command a expects \ followed by text
Your version of sed is not GNU sed which allows what you use. You need to write:
openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed 's/ECHO: \"\[LC\] //' |
sed 's/"$//' |
sed '$a\
;' >./2d_$1
Also, three copies of sed is a little excessive (to be polite); one suffices:
openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed -e 's/ECHO: \"\[LC\] //' \
-e 's/"$//' \
-e '$a\' \
-e ';' >./2d_$1
or:
openscad $1 -D generate=1 -o $1.csg 2>&1 >/dev/null |
sed -e 's/ECHO: \"\[LC\] //' -e 's/"$//' -e '$a\' -e ';' >./2d_$1

explain this shell script to me please

I am confused with this make.sh file. I read previous posts about shell scripts structure but I could not find out about this file. what is the function of this file? ....
Can anyone explain it step by step?
#!/bin/sh
rm out/*
example_number=0
for name in `ls in`
do
out=`cat in/$name | grep ".o " | tr -s \ | cut -d\ -f2`
inp=`cat in/$name | grep ".i " | tr -s \ | cut -d\ -f2`
echo -n "${name} (i=${inp}, o=${out}) "
if [ $inp -le 12 ]
then
cat in/$name \
| sed '/.i/d' \
| sed '/.o/d' \
| sed '/.p/d' \
| sed '/.e/d' \
| sed 's/|/ /g' \
| tr -s \ \
| sed 's/^[ \t]*//;s/[ \t]*$//' \
> out/${name}.in
tst=`cat out/${name}.in | cut -d\ -f2 | grep - -c`
if [ $tst -ne 0 ]
then
echo "remove file"
rm out/${name}.in
else
echo processing...
./unix2dos.exe -q out/${name}.in
example_number=`expr $example_number + 1`
fi
else
echo " skip"
fi
done
for name in `grep 2 -l out/*`
do
echo Remove $name
rm $name
example_number=`expr $example_number - 1`
done
echo Number of examples is $example_number
# bad files
# apla ( 222? )
# tms
# mainpa...
It is not a Makefile, but a shell script. You can see this from the file extension .sh and from the header
#!/bin/sh
Which is the instruction to use the shell to execute this file.

BASH:How do i make output like in watch command

My linux 'watch' command is quite old and doesn't support '--color' option. How can I have same output like it does? because in my script the loop gives output one after another(of course). But i need it to replace the previous. Is there any tricks with terminal output?
#!/bin/bash
while true
do
/usr/sbin/asterisk -rx "show queue My_Compain" \
| grep Agent \
| grep -v \(Unavailable\) \
| sort -t"(" -k 2 \
| GREP_COLOR='01;31' egrep -i --color=always '^.*[0-9] \(Not in use.*$|$' \
| GREP_COLOR='01;36' egrep -i --color=always '^.*\(Busy*$|$'
sleep 2
done
You can use clear to clear the screen before dumping your output to give the appearance of in-place updates.
To reduce blinking, you can use the age old technique of double buffering:
#!/bin/bash
while true
do
buffer=$(
clear
/usr/sbin/asterisk -rx "show queue My_Compain" \
| grep Agent \
| grep -v \(Unavailable\) \
| sort -t"(" -k 2 \
| GREP_COLOR='01;31' egrep -i --color=always '^.*[0-9] \(Not in use.*$|$' \
| GREP_COLOR='01;36' egrep -i --color=always '^.*\(Busy*$|$'
)
echo "$buffer"
sleep 2
done

Resources