Bash while loop syntax error in do - bash

I´m trying do do a simple counter:
max=100
count=1
while [[ $count -le $max]]
do
echo "$count"
((count++))
done
This gives me a syntax error in conditional expression near do.
What´s my issue? (probably something obvious)
The idea is then to raise the max from 100 to 200 and so forth in a superior loop so I will get a new file to manipulate with a python program 100 lines each time, but that´s irrelevant here.

Your mistake is that it need one more space in [[ $count -le 100]]
max=100
count=1
while [[ $count -le $max ]]
do
echo "$count"
((count++))
done
Another solution :
while ((count < max+1)); do echo $((count++)); done
or
for ((i=count; i<max; i++)) { echo $i; }
or
for ((i=count; i<max; i++)); do echo $i; done
or
for i in {1..100}; do echo $i; done

Change the line:
while [[ $count -le 100]]
to:
while [[ $count -le 100 ]];
Notice the space after 100.

Related

Error getting in the second if statement in bash script

n=20
x=3
count=0
flag=0
i=1
declare -a arr[n+1]
for (( j=0;j<=n;j++ ))
do
arr+=(0)
done
#echo "${arr[#]}"
while [[ $count -ne $n ]]
do
if [[ $i -le $n ]]
then
if [[ ${arr[$i]} -eq '0' ]]
then
echo "Value is ${arr[$i]}"
#${arr[$(i-1)]}= (( ${arr[$i-1]++} ))
${arr[$i]}+=${arr[$i]}
echo " "
#echo -n "${arr[$i]}"
echo -n " $i"
count=$(( count+1 ))
i=$(( i+1+x ))
else
i=$(( i+1 ))
fi
else
i=$(( i-n ))
flag=$(( flag+1 ))
fi
done
echo " "
echo "No of round : $flag"
This is the whole code, I've tried to print numbers that follows this: n=20 is the number of elements and x=3 is the number that we have to avoid. For example,
20
3
1,5,9,13,17,2,6,10,14,18,3,7,11,15,19,4,8,12,16,20,
3
But, the problem is that my second if condition is not fulfilling, if ignores the condition. Above example is for the C++, but in bash script, 2nd if statement isn't working. This can be because syntax is wrong. So can you please help me to find the mistakes.
Output of the above code:
output
${arr[$i]}+=${arr[$i]}
This is incorrect. $ should not be used when you assign the value.
If you want to double the value, replace this string with the following:
arr[$i]=$(( ${arr[$i]} + ${arr[$i]} ))
Or what you want to do there?

Loop increasement with 0.5

I have loop which writes to file, but I want to write each 0.5 value to the file. I tried with let count+=0.5 but that didn't work somehow. Is this possible?
Script:
#!/bin/bash
COUNTER=50
count=0
until [ $COUNTER -lt 20 ]; do
echo $count >> value.txt
echo COUNTER $COUNTER
let COUNTER-=1
let count+=0.5
sleep 1
done
bash doesn't do floating-point arithmetic natively; you need to use an external tool. -= is also not a supported operator.
until [ "$COUNTER" -lt 20 ]; do
printf "%0.1f\n" "$count"
echo "COUNTER $COUNTER"
count=$(bc <<< "$count + 0.5")
COUNTER=$((COUNTER - 1))
sleep 1
done > value.txt

unexpected token `fi' in script

Whenever I run the bash script I encounter this problem:
LRU.sh: line 121: syntax error near unexpected token `fi'
The script is:
#!/bin/bash
declare -i numOfPageRRef=0
declare -i numOfFrames=$1
declare -i a=0
declare -i k=0
declare -i c=0
declare -i q
declare -i c1=0
OIFS=$IFS
IFS=','
read line < Input2.csv
for val in $line
do
pageRef[$numOfPageRRef]=$val
((numOfPageRRef++))
done
#echo ${pageRef[#]}
q[$k]=${pageRef[$k]} #chck here
echo ${q[$k]}
((c++))
((k++))
for((i=1;i<numOfPageRRef;i++))
do
c1=0
for((j=0;j<numOfFrames;j++))
do
if (( ${pageRef[$i]} -ne ${q[$j]} ))
then
((c1++))
fi
done
if (( c1 -eq numOfFrames ))
then
((c++))
if (( k -lt numOfFrames )) ;then
q[$k]=${pageRef[$i]}
((k++))
for((j=0;j<numOfFrames;j++))
do
echo ${q[$j]}
done
else
for((r=0;r<numOfFrames;r++))
do
c2[r]=0
for((j=i-1;j<numOfPageRRef;j--))
do
if (( ${q[$r]} -ne ${p[$j]} ))
then
((c2[r]++))
else break
fi
done
done
for((r=0;r<numOfFrames;r++))
do
t4=${c2[r]}
b[$r]=$t4
for((r=0;r<numOfFrames;r++))
do
for((j=r;j<numOfFrames;j++))
do
if (( ${b[$r} -lt ${b[$j]} ))
then
t=${b[r]}
t2=${b[j]}
b[$r]=$t2
b[$j]=$t
fi
done
done
for((r=0;r<numOfFrames;r++))
do
if (( ${c2[$r]} -eq ${b[0]} ))
then
t3=${p[$i]}
q[$r]=$t3
fi
echo ${q[$r]}
done
#echo
fi
fi
done
echo "The no of page fault is $c"
Your error is because you missed a done closing statement, when i remade all the syntax in your code i was able to see it, its in the bottom. so here is your code without the error.
P.S
In the future please pay attention on the syntax, it makes big difference while reading the code.
OIFS=$IFS
IFS=','
read line < Input2.csv
for val in $line
do
pageRef[$numOfPageRRef]=$val
((numOfPageRRef++))
done
#echo ${pageRef[#]}
q[$k]=${pageRef[$k]} #check here
echo ${q[$k]}
((c++))
((k++))
for((i=1;i<numOfPageRRef;i++))
do
c1=0
for((j=0;j<numOfFrames;j++))
do
if (( ${pageRef[$i]} -ne ${q[$j]} ))
then
((c1++))
fi
done
if (( c1 -eq numOfFrames ))
then
((c++))
if (( k -lt numOfFrames ))
then
q[$k]=${pageRef[$i]}
((k++))
for((j=0;j<numOfFrames;j++))
do
echo ${q[$j]}
done
else
for((r=0;r<numOfFrames;r++))
do
c2[r]=0
for((j=i-1;j<numOfPageRRef;j--))
do
if (( ${q[$r]} -ne ${p[$j]} ))
then
((c2[r]++))
else break
fi
done
done
for((r=0;r<numOfFrames;r++))
do
t4=${c2[r]}
b[$r]=$t4
for((r=0;r<numOfFrames;r++))
do
for((j=r;j<numOfFrames;j++))
do
if (( ${b[$r]} -ne ${b[$j]} ))
then
t=${b[r]}
t2=${b[j]}
b[$r]=$t2
b[$j]=$t
fi
done
done
for((r=0;r<numOfFrames;r++))
do
if (( ${c2[$r]} -eq ${b[0]} ))
then
t3=${p[$i]}
q[$r]=$t3
fi
echo ${q[$r]}
done
done #Here You forgot the done!!!!!
fi
fi
done
echo "The no of page fault is $c"

Using if and controlling numbers in ksh

I would like to download file with the format cars000.txt, cars003.txt,cars006.txt, till cars105.txt...interval of 3 as you can see
I use the following code in ksh, but after downloading cars012.txt, it fails, it begins to download cars13.txt,...and I don't wish it. What does it fails in the code?
FHR=000
while [ $FHR -le 105 ]
do
file=cars${FHR}.txt
wget http://${dir_target}/${file}
(( FHR = $FHR + 03 ))
echo $FHR
if [[ $FHR -le 10 ]]; then FHR="00"$FHR
else FHR="0"$FHR
fi
done
You should decide: is FHR a string, a decimal or an octal.
You are mixing them currently.
Try the next improvement:
FHR=0
while [ ${FHR} -le 105 ]; do
file=cars${FHR}.txt
(( FHR = FHR + 3 ))
echo Without leading zero: FHR=${FHR}
if [[ $FHR -le 10 ]]; then
echo "FHR=00${FHR}"
else
echo "FHR=0${FHR}"
fi
sleep 1
done
(The next improvement might be using printf or awk and no zero for 102/105)

Change variables in bash

How do I change this var ?
max=0;
min=20000000;
cat |while read
do
read a
if [[ $a -gt $max ]]
then
max=a`
fi
`if [[ $a -lt $min ]]
then
min=a
fi
done
echo $max
echo $min
My min and max are still the same, 0 and 2000000. Can anybody help me with this ? I have no idea.
The (main) problem with your script is that setting min and max happens in a subshell, not your main shell. So the changes aren't visible after the pipeline is done.
Another one is that you're calling read twice - this might be intended if you want to skip every other line, but that's a bit unusual.
The last one is that min=a sets min to a, literally. You want to set it to $a.
Using process substitution to get rid of the first problem, removing the (possibly) un-necessary second read, and fixing the assignments, your code should look like:
max=0
min=20000000
while read a
do
if [[ $a -gt $max ]]
then
max=$a
fi
if [[ $a -lt $min ]]
then
min=$a
fi
done < <(cat) # careful with the syntax
echo $max
echo $min

Resources