BASH script looking for some guidance [duplicate] - bash

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)

Related

why function variable not working in command substitution inside a function? [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.
im running this in bashrc file
function simplefunc () {
output=$(ls -1 "$HOME")
linecount=$(${output} | wc -l)
echo "${linecount}"
echo "${output}"
}
getting this error
Desktop: command not found
0
Desktop
Documents
Downloads
Music
Pictures
Public
snap
SoftMaker
Templates
venv
Videos
i tried these too
putting "local" before variable or
# linecount=$(output | wc -l)
# echo "$(${output} | wc -l)"
I think you should change the third line to:
linecount="$(echo "${output}" | wc -l)"
# OR alternatvely:
# linecount="$(wc -l < <(echo "$output"))"

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

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

How to divide 2 integer numbers and get the output stored in float variable using ksh shell scripting? [duplicate]

This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 3 years ago.
I have below values stored in the respective variables:
s=$(nice grep -i "sf WOW" *`date --date='1 hour ago' +%y%m%d%H`* | grep -i "TRAF:5" | wc -l)
f=$(nice grep -i "sf WOW" *`date --date='1 hour ago' +%y%m%d%H`* | grep -i "TRAF:7" | wc -l)
a=`expr $s + $f`
I need to store the division output in a float variable with an accuracy of 5 digits.
echo "scale=5; 355/113" | bc
3.14159

shell script(calculate the division of two variables taking from a file) [duplicate]

This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 6 years ago.
I would like to divide two numbers which were extracted from a file using the below commands
temp1= grep PERM_ALLOCATED_SIZE /log/health_eg/DBsize.txt | cut -d':' -f2 | tr -d ' '
temp2= grep PERM_IN_USE_SIZE /log/health_eg/DBsize.txt | cut -d':' -f2 | tr -d ' '
and I'm able to print this
251658240
16239740
temp1 and temp2 respectively but I could not able to perform division for the above..
Sample output:
temp=temp2/temp1(0.064)
A possible solution is using echo and bc -l:
echo "251658240/16239740" | bc -l
Output:
15.49644514013155383029
Using your example, you can do that:
temp=`echo $temp2/$temp1*0.64 | bc -l`

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`

Resources