is this command valid for deleting evicted pods from last month? [duplicate] - bash

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
I just assigned a variable, but echo $variable shows something else
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Difference whilst using ";" or "|" symbols in command lines [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Can you please let me know if this command is valid ?
kubectl get pods -A | awk {'printf "%s %s %s %s\n", $1,$2,$4,$6'} | grep -E "Evicted" |
while read line; do ns=$(echo $line | cut -d , -f 1); pod=$(echo $line | cut -d , -f 2); age=$(echo $line | cut -d, -f 6) ;
final_age=$(awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< "$age");
if [[ $final_age -gt 30 ]]; then kubectl delete pod --dry-run='server' -n "$ns" "$pod"; fi done
The intention is to fetch the old evicted pods which their age is greater than 30 days and delete them .. can you please check as it returns null on my machine..
expected output: the names of pods which will be deleted
resulted output: null

Related

How to use multiple variable in for loop in sh? [duplicate]

This question already has answers here:
How do I pipe a file line by line into multiple read variables?
(3 answers)
Closed 18 days ago.
I want to use multiple variable in for loop at once in sh.
I have a query like this:
top -n 1 -b -c| awk -vOFS="\t" '{print $1,$2,$9}'
I know i use for loop in bash like this:
for i in {2..10}
do
echo "output: $i"
done
what i want to try is:
for x y z in $(top -n 1 -b -c| awk -vOFS="\t" {print $1,$2,$9}')
do
echo "output: $x $y $z"
done
Pipe to a while read loop:
top -n 1 -b -c| awk -vOFS="\t" '{print $1,$2,$9}' | while IFS=$'\t' read -r x y z
do
echo "output: $x $y $z"
done

How to pass string variable in cut shell command [duplicate]

This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
Assume that I have :
.sh file having these commands :
#!/bin/bash
GIT_BRANCH="origin/release/2.4.0"
echo $GIT_BRANCH
then, I want to compute new varible from the GIT_BRANCH (operation of substring) :
So,
RELEASE_VERSION=$( $GIT_BRANCH | cut -d "/" -f3)
echo $RELEASE_VERSION
But this does return message error : bad substitution
I tried many possibilities in the RELEASE_VERSION, but no result.
like
RELEASE_VERSION=$(echo $GIT_BRANCH | cut -d "/" -f3)
RELEASE_VERSION=$("$GIT_BRANCH" | cut -d "/" -f3) and this return empty results
You are definitelly missing an echo statement. Following code works for me just fine.
GIT_BRANCH="origin/release/2.4.0"
RELEASE_VERSION=$(echo $GIT_BRANCH | cut -d "/" -f3)
echo $RELEASE_VERSION

Bad Substitution when I try to print a specific position of array [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 10 months ago.
I'm getting started with bash programming, and I want to print a specific position of array, but when I try I get this error: Bad substitution
#!/bin/sh
user=`cut -d ";" -f1 $ultimocsv | sort -d | uniq -c`
arr=$(echo $user | tr " " "\n")
a=5
echo "${arr[$a]}" #Error:bad substitution
why?
You are using "sh" which does not support arrays. Even if you would use the "bash" you get the same error, because the "arr" will not be an array. I am not sure, if the "-c" at "uniq" is what you wanted.
I assume, this is what you are looking for:
#!/bin/bash
mapfile -t arr < <( cut -d ";" -f1 $ultimocsv | sort -d | uniq )
a=5
echo "${arr[$a]}"
This will not give the error, even if your file has less than 5 unique lines, because bash will return an empty string for a defined but empty array.
It works even with "uniq -c", because it puts complete lines in the array.

Assigning a command output to a shell script variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
How do I assign a command output to a shell script variable.
echo ${b%?} | rev | cut -d'/' -f 1 | rev
${b%?} gives me a path..for example: /home/home1
The above command gives me home1 as the output. I need to assign this output to a shell script variable.
I tried the below code
c=${b%?} |rev | cut -d '/' -f 1 | rev
echo $c
But it didn't work.
To assign output of some command to a variable you need to use command substitution :
variable=$(command)
For your case:
c=$(echo {b%?} |rev | cut -d '/' -f 1 | rev)
Just wondering why dont you try
basename ${b}
Or just
echo ${b##*/}
home1
If you want to trim last number from your path than:
b="/home/home1"
echo $b
/home/home1
b=${b//[[:digit:]]/}
c=$(echo ${b##*/})
echo ${c}
home
Just like this:
variable=`command`

BASH script looking for some guidance [duplicate]

This question already has answers here:
Bash variable scope
(7 answers)
Closed 9 years ago.
well im in this for hours and I can't understand why I cant save the values to the variable:
let inicio=${tlinhas[0]}/2+1
tail -n +$inicio $1 | head -n $tlinhas | grep $2 | while read linha
do
let palavras=$palavras+$(echo $linha | wc -w)
echo $palavras
done
printf "%d" $palavras
the problem is that every time I print the variable palavras its always zero but if I print it inside the while it has the value 14
[leganuno#LegaNuno-PC FichasIndividuais]$ ./exercicio1.sh f1 Licenciatura
7
7
0
Try
palavras = echo $($palavras + $(echo $linha | wc -w) | bc)

Resources