Getting an error while subtracting variables [duplicate] - bash

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!

Related

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.

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

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.

Shell gives error "[: too many arguments" when using while loop [duplicate]

This question already has answers here:
How can I compare a string to multiple correct values in Bash?
(5 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 1 year ago.
I am very new to shell scripting, and i have had this error. I am using a while loop that goes a bit like this:
while [ "$variable" =! "hello" -o "$variable" =! "hi" ]
do
echo "variable isn't hi or hello"
done
but shell then gives the error [: too many arguments

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

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

Resources