seq - invalid floating point argument error - bash

I'm currently writing a small bash file to search specific information on a file.
I need a for structure and I'm using "seq" but I keep getting "invalid floating point argument error : 4" and I do not know how to solve it.
When I try to do some arithmetic operation on my variable nmbretry, I get a arithmetic operator not available.
If you have any ideas how to solve it!
Here my code:
#!/bin/bash
nmbretry=`grep -c 'retry for the 1 times' /home/leconte/dossierpartage/business.log`
echo "Number of retry is $nmbretry"
let $nmbretry + 1
for i in `seq 0 $nmbretry`; do echo $i
done;
Thanks a lot!

increment nmbretry with let ++nmbretry - bash also has a counted for loop:
for((i=0; i < nmbretry; ++i)); do
echo $i
done

Thanks for your help.
The error was that I used notepad on Windows than use it on Unix. It did not like the "transfer".
I do everything on Unix and now it's work!
Thanks again.

Related

How to iterate in a range determined by TWO variables?

I have a code:
echo "the range's starting number:"
read -r a #it was 10
echo "the range's ending number:"
read -r b #it was 20
for (( c=$a; c<=$b; c++ ))
do
echo $c
done
Question: what is the working syntax? I found a similar question where c=1; c<=$b; c++ worked. I want to iterate between $a (example $a=10) and $b (example $b=20) and not between 1 and $b=20. Thanks for the help. (the output is blank here, the expected output was:
10
11
12
..
20
I tried my code, also closed terminal and started a new one because of possible caching issues, but there was still a blank output.
Update: in the comments accdias's answer was working. With for ((c=a; c<=b; c++)) i got the expected output. Thanks all for the help and comments!
I took #accdias's comment and figured I would just drop it here as an answer for easy reference:
START=12
END=24
for ((i=START; i<=END; i++)); do
echo $i
done

Passing bc into a variable isn't working right for me in Bash

I think I'm going crazy, I have a different script that's working fine with the usual way of passing the output of bc into a variable but this one I just can't get working.
The relevant bits of code are this:
PERCENT_MARKUP=".5"
$percentonumber=$(bc -l <<<"$PERCENT_MARKUP/100")
echo "Percent to number = $percentonumber"
$numbertomultiply=$(bc -l <<<"$percentonumber + 1")
echo "Number to multiply = $numbertomultiply"
$MARKUP=$(bc -l <<<"$buyVal * $numbertomultiply")
#$MARKUP=$(bc -l <<<"$buyVal * (1+($PERCENT_MARKUP/100))")
echo "Markup = $MARKUP"
Originally, I only had the second to last line that's currently commented out but I broke it down to try and troubleshoot.
The error I'm getting when I'm trying to run it is:
./sell_zombie_brains: line 65: =.00500000000000000000: command not found
where the .0050000000000000 is replaced by the output of bc. I've even tried the following at multiple points in the file including right after #!/bin/bash with and without -l
$test=`bc -l <<<"1"`
echo "$test"
echo
$test=$(bc -l <<<"1")
echo "$test"
echo
$test=$(echo "1"|bc -l)
echo "$test"
echo
And each one outputs ./sell_zombie_brains: line 68: =1: command not found
I'm really at my wits end. I don't know what I'm missing here. Any insight as to why it's behaving this way is appreciated.
You can't assign variables with the sigil $ :
instead of
$percentonumber=$()
it's
percentonumber=$()
If you are a bash beginner, some good pointers to start learning :
https://stackoverflow.com/tags/bash/info
FAQ
Guide
Ref
bash hackers
quotes
Check your script
And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide -ABS in particular) is outdated, and in some cases just plain wrong. The BashGuide and the bash-hackers' wiki are far more reliable.

echo -n fails with piped function

I'm a bit of a noob so bear with me:
I have a pretty basic script to check the cpu temp of the rpi, and I need the output on a single line as a requirement for reporting to a messenger service with a webhook. The output should look something like "54.0°C,129.2°F". I know the switch to append to existing line with echo, -n, but because I am piping the Fahrenheit conversion to the bash calc (BC) I get a syntax error if i try to start that line with "echo -n etc."
I also realize that I don't really need to print the °C and °F, but i demand luxury!
Here's my script (which works fabulous if i don't try to cram it all on the same line):
#!/bin/bash
(
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$((cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % cpuTemp1))
#date
#echo cpu temp in °C and °F
echo -n $cpuTemp1"."$cpuTempM
echo -n "°C,"
echo -n "$cpuTemp1 * 1.8 + 32"|bc
echo "°F"
) > /home/pi/bin/tlog
the error I receive there is:
(standard_in) 1: syntax error
So the question is this; how do I get the °F on the same line as the conversion formula without borking the |bc function? I am positive the |bc is the issue as the script runs fine if I remove it, but it doesn't do the math for me. =(
Any help appreciated, thanks.
As you have discovered, bc wants a properly terminated line. So why don't we just give it one?
We can rearrange your code to do all the computation first and then do a single echo at the end:
#!/bin/bash
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$((cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % cpuTemp1))
tempF=$(echo "$cpuTemp1 * 1.8 + 32"|bc)
echo -n "${cpuTemp1}.${cpuTempM}°C,${tempF}°F" > /home/pi/bin/tlog

1394661620440271000/1000000: No such file or directory

I am running this command in my shell script to get time in milliseconds:
START=`$(date +%s%N)/1000000`
However I keep getting the error:
1394661620440271000/1000000: No such file or directory
I tried to change the code by adding brackets, extra dollar signs, but I keep getting different kinds of errors. How can I fix the code? Could anyone help with that?
assuming that you're using bash:
START=$(( $(date '+%s%N') / 1000000 ))
you can't just say / on the command line to divide numbers. (( ... )) does arithmetic evaluation in bash.
I think you want the following:
START=$(($(date +%s%N)/1000000))
You could also use plain string manipulation:
$ start=$(date '+%s%N')
$ echo $start
1394663274979099354
$ echo ${start:0:-6}
1394663274979
The printf statement can round a value to the nearest millisecond:
printf "%0.3f\n" $(date +%s.%N)

BASH Arithmetic Issues

I'm working in BASH and I'm having an idiot moment right now. I've got a project I'm working on that I'm going to need to use some very basic arithmetic expressions and I just realized that a lot of my problems with it are because my variables are not updating. So I threw together a basic algorithm that increments a variable by another variable with a while loop until a certain number is reached.
counter=1
counter2=0
while [[ counter2 < 10 ]]; do
counter2=$(($counter2+$counter))
echo $counter
echo $counter2
done
I run the script. Does nothing. I set the < to > just for kicks and an infinite loop occurs with a repeated output of:
1
0
1
0
Forever and ever until I stop it. So it's obvious the variables are not changing. Why? I feel like such an idiot because it must be something stupid I'm overlooking. And why, when I have <, it also isn't an infinite loop? Why doesn't it print anything at all for that matter? If counter2 is always less than 10, why doesn't it just keep going on forever?
Thanks folks in advance.
EDIT: Well, I realize why it wasn't outputting anything for when the check is <... I should have been using $counter2 instead of just counter2 to get the actual value of counter2. But now it just outputs:
1
2
And that's it... I feel like such a derp.
If this is all bash (100% sure) then you could use declare -i in order to explicitly set type of your variables and then your code will be as simple as :
declare -i counter=1
declare -i counter2=0
while [[ $counter2 -lt 10 ]]; do
counter2=$counter2+$counter
echo $counter
echo $counter2
done
EDIT:
In bash, you can do arithmatic comparison with double paranethesis. So, your while can be written as:
while (($counter2 < 10)) ; do
Inside the $((...)), don't use the sigil ($).
counter2=$((counter2+counter))
In bash, you can use c-like for loops:
for (( counter2=0; counter2<10; counter2+=counter ))
do
echo $counter": "$counter2
done
Often you will find this construct more appealing to use:
for counter2 in {0..9}
do
echo $counter": "$counter2
done

Resources