dmesg log to file, and check number of lines [duplicate] - bash

This question already has answers here:
How can I compare numbers in Bash?
(10 answers)
Closed 4 years ago.
I have an if statement I need to run, as long as the value I have stored in my $counter variable is greater than 5.
Here is the respective section of my current (non-functioning) script:
if $counter > 5
then
echo "something"
fi
The mistake I'm making is probably very obvious, but for some reason I couldn't find the solution online..
Thanks!

Well that is quite simple:
if [ "$counter" -gt 5 ]
then
echo "something"
fi

Arithmetic needs to be done between (( and )):
if (( $counter > 5 ))
Incidentally, you can also leave off the $ in arithmetic, though it doesn't hurt to keep it.

Related

How to keep a variable after a while loop bash [duplicate]

This question already has answers here:
Propagate value of variable to outside of the loop [duplicate]
(3 answers)
A variable modified inside a while loop is not remembered
(8 answers)
Closed 5 months ago.
How do i keep a variable value after a while loop?
My intention here is to put a if clause within the while to count each time an operation has been done, but isn't working since the count resets after each while loop.
count=0
for file in $(ls /path)
do
cat $ file | while read line
do
count=$((count+1))
echo $count
done
done
echo $count #This echoes 0, even though the inner echo shows each sum.
There are many methods. In this case, it's probably easiest to drop the UUOC:
while read line; do
count=$((count+1))
echo "$count"
done < "$file"
which would be better written:
while read line; do echo "$((++count))"; done < "$file"
which would be even better written:
count=$(wc -l < "$file")
Although the last version sets count accurately (which the first two do not unless count is initialized) and does not emit the counts.

bash while loop gives syntax error saying missing '[' [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 1 year ago.
I'm new to bash scripting and am having trouble trying to get this to work
local attempt=1
local success=false
while [[ "$attempt" -le "$retryAttempt" && "$success" -eq "false"]]; do
if ! [[ some condition here]];
then
echo true
return
fi
done
I'm getting an error on the while condition saying
line 10: [: missing ]
I cannot figure out what is wrong here, suggestions anyone?
Short: insert spaces around braces
Long: I wouldn't consider myself a bash pro (no idea what local is), but in a minimal syntax example (which is actually an infinity loop).
while [ $b -le $a] ; do
b=$a
done
gives me the same error. Changing the first line to
while [ $b -le $a ] ; do
works.
Depending on the complexity of the script, you might want to consider python or perl. In my opinion bash syntax can be a real pain in the a**. Especially when passing arguments with spaces through more than one level.

How to sum floats variables in bash? [duplicate]

This question already has answers here:
"Invalid Arithmetic Operator" when doing floating-point math in bash
(5 answers)
How do I use floating-point arithmetic in bash?
(23 answers)
How can I compare two floating point numbers in Bash?
(22 answers)
Closed 3 years ago.
I am trying to run a script in which a variable is updated every time by a float number "given as user input" in a while loop. I know that bash doesn't support floats, I tried using | bc but it doesn't seem to work to me...
step_l0=0.25
step_l=0
while [ $step_l -le 2 ] do
step_r=$radius_input
while [ $step_r -le $radius_define ] do
stuff done in the while loops
step_r=$(( $step_r + $step_r0 ))
done
step_l=$(( $step_l + $step_l0 ))
done
Per chepner's suggestion -
$: a=0.25
$: b=1.753
$: awk "BEGIN{ print $a + $b }"
2.003
$: python -c "print $a + $b"
2.003
$: perl -e "print $a + $b"$'\n'
2.003
Where possible (and it's virtually always possible), if you need to invoke a more precise or powerful language to accomplish a task in your script, consider converting the entire script. You'll be glad you did.

Trying to understand a Bash script given to me, specifically the while loop [duplicate]

This question already has answers here:
What is "-le" in shell script?
(2 answers)
Closed 4 years ago.
I am using a script made by one of my former colleagues, he told me I'll need some working with it. I am wondering what this while loop does:
# This is the loop that does the simulation
lastsim=0
nextsim=`/usr/bin/expr $lastsim + 1`
while [ $nextsim -le $upperlimit ]
do
cp -i Dynamics_${lastsim}_500ps/*.prmtop ./$paramInput.prmtop
specifically I'm confused by thie -le syntax
This is only part of the script I can upload the rest if necessary.
-le means less or equal to. See the following example which would print 0-9:
i=0
while [ $i -le 9 ]; do
echo $i
let i++
done

Getting an error while subtracting variables [duplicate]

This question already has answers here:
bash arithmetic expressions with variables
(2 answers)
Closed 5 years ago.
I am writing a bash script and i get an error when i try to subtracts 2 variables. I tried many ways of declaring the subtraction as a single variable, but i am constantly getting errors. What's the right way to declare the variable? Thanks in advance.
if [ ${$packets1 - $packets2} -gt 30 ]
Here you have a working solution for Bash :
packets1=300
packets2=176
if [ $(( packets1 - packets2 )) -gt 30 ]
then
echo "This is true!"
fi
Regards!

Resources