I want to print 2 numbers that are in the following format:
001,002,003,...,015,016,017,...,098,099,100
for i in touch {001..100}; do
for j in touch {001..100}; do
if (( $j > $i )); then
echo $i $j
fi
done
done
The loop does what I want it to do.
But the problem is that it prints them without the 0s. For example it prints 1 2, 1 3, 1,4, ..., 99 100.
And I want it to print 001 002, 001 003, 001 004, ... , 099 100.
Does anyone have any idea of how can I fix that?
The retention of leading zeros was added in bash 4; you appear to be using an older version of bash. However, if the 0s were retained, you would have to adjust your comparison to avoid treating the values as octal numbers. (090, for instance, is an invalid octal number.)
for i in {001..100}; do
for j in {001..100}; do
if (( 10#$j > 10#$i )); then
echo $i $j
fi
done
done
Using printf with a C-style for loop would be preferable, though.
for ((i=1; i < 101; i++)); do
for ((j=i+1; j < 101; j++)); do
printf '%03d %03d\n' "$i" "$j"
done
done
Note there is no comparison; the inner loop starts j at a value greater than i.
One possibility would be to use printf:
for i in {1..100}; do
for j in {1..100}; do
if (( $j > $i )); then
printf "%03d %03d\r\n" $i $j
fi
done
done
Use seq:
for i in touch $(seq -w 1 100); do
for j in touch $(seq -w 1 100); do
if (( $j > $i )); then
echo $i $j
fi
done
done
printf could also work.
Related
My bubble sort code in bash script does not swap nor sort. Help me out on what I am doing wrong.
echo "Enter numbers between 4 and 12"
read n
#declare -a numArray #declare array of numbers
if [ $n -le 12 -a $n -ge 4 ]
then
#take input from user
echo "Enter numbers of array"
for (( i = 0; i < $n; i++ ))
do
read numArray[$i]
done
#sorting logic
for (( i = 0; i < $n; i++ ))
do
for (( j = 0; j < $n-1; j++ ))
do
# swapping logic
#read numArray[$j]
if [[ ${numArray[j]} -gt ${numArray[$((j+1))]} ]]
then
#echo "in swapping code"
temp=${numArray[j]}
echo $temp
numArray[$j]=${numArray[$((j+1))]}
numArray[$((j+1))]=$temp
fi
done
done
# print sorted data
echo -e "\nSorted Numbers "
echo "${numArray[*]}"
else
echo "Please enter only values between 4 and 12"
fi
Result:
Enter numbers between 4 and 12
5
Enter numbers of array
51428
51428
51428
51428
51428
Sorted Numbers
51428
I want to print prime numbers using for loop in shell script. Please provide some suggestions.
actually I could able to print odd numbers from range of 1 to 100, but now I am unable to print prime numbers. Provide some script to print prime numbers
# cat prime.sh
#!/bin/bash
for ((i=1; i<=100; i++))
do
output=$(( $i % 2))
if [ $output -ne 0 ]
then
echo "We got odd numbers: $i"
fi
done
The below code returns the list of prime numbers within a specified range.
the below command returns the prime numbers between 10 to 30
./filename.sh 10 30
#!/bin/bash
# Check if a number is prime
# function to return whether prime or not
check_prime() {
current_number=$1
flag=0
i=2
while test $i -le `expr $current_number / 2`
do
if test `expr $current_number % $i` -eq 0
then
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 0
then echo $current_number
fi
}
# Assign From and to number
from_number=$1
to_number=$2
for (( number=$from_number; number<=$to_number; number++ ))
do
check_prime $number
done
This code will Print all prime numbers upto given number & also print count of it
hope it helps !!
#!/bin/bash
echo "enter a number upto which you want the prime numbers"
read num
count=0
for (( n=2; n<=$num; n++ ))
do
t=1
if [ $n -lt 2 ]
then echo "Please give other numbers than 0 and 1"
else
#echo $n
for (( i=2; i<$n; i++ ))
do
#echo $n
if (($n%i==0))
then
#echo "$n is not prime number"
t=0
break;
fi
done
#echo $t
if [ $t == 1 ]
then
echo "$n is a prime number"
count=`expr $count + 1`
fi
fi
done
echo "Total prime numbers upto $num are $count "
I am quite new in shell script and trying to make some practice. What I want to do is to get an input from the user and create a triangle by using for loop. For example; if the user types 4 as input then the targeted triangle will be;
1
2
2
3
3
3
4
4
4
4
Here is my code:
num=4 ##Assume num is given by user##
for ((i=1; i<=$num; i++))
do
for ((j=1; j<=i; j++))
do
echo $i
done
echo " "
done
Output:
1
22
333
4444
Is it something related with 'new line' thing? How can I fix it?
By the way, I am using an online shell terminal.
Thank you very much.
echo $i prints the value behind the i variable and a newline. You want to print a newline after all the numbers. Use -n switch.
num=4 ##Assume num is given by user##
for ((i = 1; i <= num; i++))
do
for ((j = 1; j <= i; j++))
do
echo -n "$i "
done
echo
echo
done
Or printf:
num=4 ##Assume num is given by user##
for ((i = 1; i <= num; i++))
do
for ((j = 1; j <= i; j++))
do
printf "%s " "$i"
done
printf "\n\n"
done
Remember to qoute your variables (echo "$i" not echo $i)
No need to use $num inside (( .. )). All names are expanded automagically.
#edit
On revisiting the question I came up with the following oneliner with the same functionality:
num=4
seq 1 "$num" | xargs -n1 seq -s ' ' 1
seq 1 "$num" generates numbers 1 2 .... $num separated by newlines
Then for each number xargs runs seq -s ' ' 1 <number> generating 1 .. number for each number separated by spaces
You can insert double newlines with | sed 's/$/\n/' if needed.
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
The challenge is to sum the digits of a given number till the result has only one digit. Let say the number is "999" (9+9+9=27, 2+7=9). This is what I did till now.
#!/bin/bash
set +m
shopt -s lastpipe
NUMBER=999
DIGITS=`echo "${#NUMBER}"`
FINALSUM=0
if [ "$DIGITS" -gt 0 ] && [ "$DIGITS" -gt 1 ]; then
grep -o . <<< "${NUMBER}" | while read DIGIT; do
declare -x FINALSUM="$(($FINALSUM+$DIGIT))"
done
echo $FINALSUM
else
echo $SOMA
fi
A bit slow for large numbers:
function sumit {
i="$1"
while [ "$i" -gt 10 ]; do
(( i=i%10 + i/10 ))
done
echo "$1 => $i"
}
# Test
for i in 10 15 999 222 2229; do
sumit $i
done
Can an awk-ward guy join in?
$ awk -v i=999 '
BEGIN {
while( split(i,a,"") > 1) {
i=0;
for( j in a ) i+=a[j]
}
print i
}'
9