This question already has an answer here:
Shell Script: Hexadecimal Loop
(1 answer)
Closed 6 years ago.
I want a bash script to print list of incremental MAC addresses. This is the while loop I'm using:
i=1
j=1
k=1
while [ $i -le 5 ]; do
j=1
while [ $j -le 46 ]; do
k=1
while [ $k -le 44 ]; do
echo "mac=00:00:01:$i:$j:$k"
k=`expr $k + 1`
done
j=`expr $j + 1`
done
i=`expr $i + 1`
done
I want the MACs to print in hexadecimal. So I want this -
00:00:01:01:09:09
to increment to this -
00:00:01:01:09:0a
and NOT to this -
00:00:01:01:09:10
Look at printf builtin bash command, and change your echo with
printf "mac=00:00:01:%2.2x:%2.2x:%2.2x\n" $i $j $k
i=1
j=1
k=1
# using for loop instead of while loop
for i in $(seq 1 5); do
j=1
for j in $(seq 1 46); do
k=1
for k in $(seq 1 44); do
echo "mac=00:00:01:$i:$j:$k"
#using following command for HEX increment.
k=`echo "obase=ibase=16;${k}+1"`
done
j=`echo "obase=ibase=16;${j}+1"`
done
i=`echo "obase=ibase=16;${i}+1"`
done
Related
I was trying to print out some numbers from 1 to 20 with an increment of 2
#!/bin/bash
for i in {0..20..2}
do
echo $i
done
and this is what it has printed out
{0..20..2}
what I am doing wrong?
Using the built-in for loops syntax:
#!/bin/bash
for (( i=0; c<=20; c+=2 ))
do
echo $i
done
I am trying to print the multiplication table from 1 to 10 in shell. However, my output is only printing it for 1.
My current implementation is
i=1
j=1
while [ $i -le 10 ]
do
while [ $j -le 10 ]
do
echo " $i x $j = `expr $i \* $j`"
j=`expr $j + 1`
done
i=`expr $i + 1`
done
Just move the initialization of j inside the while loop. After completion of the first loop, your variable j has the value as 10. So for the second iteration of the outer loop, it's not entering the inner loop since it's value is 10.
i=1
while [ $i -le 10 ]
do
j=1
while [ $j -le 10 ]
do
echo " $i x $j = `expr $i \* $j`"
j=`expr $j + 1`
done
i=`expr $i + 1`
done
This question already has answers here:
Multiplication with expr in shell script
(3 answers)
Closed 5 years ago.
I am getting a syntax error in the following code.
I am trying to print the squares of a number n.
#!/bin/sh
echo "Enter a number n to print squares : "
read num
i=1
while [ $i -le $num ]
do
echo $(expr $i * $i)
i=$(expr $i + 1)
done
echo "Done with Script"
Can someone tell me whats wrong with the code?
Thank you guys in advance.
#!/bin/sh
echo "Enter a number n to print squares : "
read num
i=1
while [ $i -le $num ]
do
echo $(expr $i \* $i)
i=$(expr $i + 1)
done
echo "Done with Script"
Shield * symbol.
I have to a perform logic like this.
I have a array.
expression for this in shell
[(first no + arrlen) - ( index +1 ) - ge 10 ]
I have code this like this but it's not working
#!/bin/bash
array=(4 5 6 7 8 9)
for i in ${array[#]}
do
echo $i
done
echo "${#array[#]}"
l=${#array[#]}
count=0
for (( i=0; i < ${#array[#]}; i++ ))
do
if [ ($(`expr $i + $l`) - $(`expr ${!array[#]} + 1`)) -ge 10 ]
then
count=`expr $count + 1`
else
echo
fi
done
Your code could look like this:
#!/bin/bash
array=(4 5 6 7 8 9)
for i in "${array[#]}"; do
echo "$i"
done
length=${#array[#]}
first=${array[0]}
count=0
for (( i=0; i < length; i++ )); do
if (( (first + length) - (i + 1) >= 10 )); then
((count++))
else
echo "something"
fi
done
Don't use expr, use (( )) for arithmetic expressions
Quote expansions: "$i", "${array[#]}", ...
${!array[#]} expands to ALL indexes of your array, not the current index
Sorry about bits and snippit of information
So I am writing an average shell script program
so if use inputs
echo 1 3, .... | sh get_number
I would have to pull the numbers seperated by spaces from echo to be
var1 = 1, var2= 3, etc.
I tried
#!/bin/sh
sum=0
for i in $*
do
sum=`expr $sum + $i`
done
avg=`expr $sum / $n`
echo Average=$avg
but doesnt work....
do I include a read here?
also how would I do
sh get_number <file1>, <file2>... to grab numbers in them and sum them
in shell script?
Thanks
Sounds like you are looking for the read shell builtin:
% echo "1 2 3 4" | read a b stuff
% echo $b
2
% echo $stuff
3 4
To fix up your code:
for i in $*; do
sum=$(( sum + i ))
n=$(( n + 1 ))
done
echo "Average=$(( sum / n ))"
#!/bin/sh
while [ $# -gt 0 ]; do
(( i++ ))
(( sum += $1 ))
shift
done
echo "Average=$(( sum/i ))"
Note: This fails in dash which is the closest shell I could find to a real sh.
An example of reading values from files passed as command line arguments or from lines read from stdin:
add_to_sum() {
set $*
while [ $# -gt 0 ]; do
I=`expr $I + 1`
SUM=`expr $SUM + $1`
shift
done
}
I=0
SUM=0
if [ $# -gt 0 ]; then
# process any arguments on the command line
while [ $# -gt 0 ]; do
FILE=$1
shift
while read LINE; do
add_to_sum "$LINE"
done < "$FILE"
done
else
# if no arguments on the command line, read from stdin
while read LINE; do
add_to_sum "$LINE"
done
fi
# be sure not to divide by zero
[ $I -gt 0 ] && echo Average=`expr $SUM / $I`