SHELL SCRIPT
Use variables in a bash for loop
One way is using eval
:
for i in $( eval echo {0..$length} )
do
echo "do something right $i"
done
Note what happens when you set length=;ls
or length=; rm *
(don't try the latter though).
safely, using seq
:
for i in $( seq 0 $length )
do
echo "do something right $i"
done
or you can use the c-style for loop, which is also safe:
for (( i = 0; i <= $length; i++ ))
do
echo "do something right $i"
done
courtesy: https://stackoverflow.com/questions/17181787/how-to-use-variables-in-a-bash-for-loop
Decimal calculations
For decimal calculations, we can use bc
command to get the output to a particular number of decimal places. bc
(Bash Calculator) is a command line calculator that supports calculation up to a certain number of decimal points.
echo "scale=2;22/7" | bc
Where scale
defines the number of decimal places required in the output.
courtesy:https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/
Comments
Post a Comment