example:
for i in 1 2 3 4, j in 7 8 9 0
do
echo $i"="$j
done
output:
1=7
2=8
3=9
4=0
i know this code will throw you a lot of error right away, but is there any way?
There is no such thing in Bash, but you can get your desired output:
foo=(1 2 3 4)
bar=(7 8 9 0)
for (( i=0; i<${#foo[#]}; i++ ))
do
echo "${foo[$i]}=${bar[$i]}"
done
Related
Write a short program that prints each number from 1 to 100 on a new line.
• For each multiple of 3, print "Fizz" instead of the number.
• For each multiple of 5, print "Buzz" instead of the number.
• For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the
number.
This what i have so far:
#!/bin/bash
*for i in {0..20..5}
do
echo "Number: $i"
done*
but i get this:
Number: {0..20..5}
Need help
If it's on a mac, use $(seq 0 5 20) to get the same result.
Using GNU sed
$ for ((i=1;i<=100;i++)); do echo "$i"; done | sed -E '0~3s/[0-9]+/Fizz/;0~5s/[0-9]+|$/Buzz/'
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
This question already has an answer here:
Command Line Arguments vs Input - What's the Difference?
(1 answer)
Closed 12 months ago.
I have for example a user with a set of numbers. How can I make bash add them together?
Example in one go the user enters
(The amount of numbers they enter is up to them and it is unknown)
bash file 3 1 5 2 2 4
How can I make bash return 17 directly from that example?
I tried
#!/usr/bin/env sh
sum=0
while read number && [ -n "$number" ]; do
sum=$((sum + ${number/#-}))
echo "$sum"
done
But this is not clean and it is returning
$ bash file
3
3
1
4
5
9
2
11
2
13
4
17
I instead want the user to only place their numbers in 1 go and not be there to put more and more numbers
Instead of having them excute the command like I have it like
bash file
1
3
4
etc
instead I want to do it in 1 go
bash file 1 3 5 6
How?
You can loop through all script arguments and calculate sum:
#!/usr/bin/env sh
sum=0
for i in "$#"; do
sum=$(( $sum + $i ))
done
echo $sum
Running with your example:
$ bash sum 3 1 5 2 2 4
17
I wrote a simple code via bash script,
Code 1
for((i=1;i<=10;i++))
do
echo $i
i=$((i+1))
echo $i
i=$((i+2))
done
output of Code 1
1
2
5
6
9
10
Code 2
for i in {1..10}
do
echo $i
i=$((i+1))
echo $i
i=$((i+2))
done
output of Code 2
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
I'm just wondering, why outputs are not same ?
Thnx in advance
With in, the variable iterates over the list. You can change its value in the loop, but when the next iteration starts, the next value will be assigned to it, regardless of what value you assigned to it. (And I can't imagine any other behaviour: should the shell try to guess how far in the list you want to jump? What if the value is repeated, or not present in the list at all?)
With the C-style for, the variable is initialized, and at each iteration, its value is changed and the condition checked. There's no list of values, only the condition to end the loop.
These are not shorthand for each other: They're completely different code, and expected to behave in different ways.
{1..10} is just shorthand for 1 2 3 4 5 6 7 8 9 10; when you run for i in 1 2 3 4 5 6 7 8 9 10, you're explicitly assigning those exact values to i in turn, overwriting whatever else was previously there.
By contrast, for ((i=1; i<=10; i++)) is providing three separate statements: An initializer (i=1), telling it what to do to start the loop; a check (i<=10) to tell it how to determine whether the loop is finished; and an update (i++), telling it what to do between loop iterations. These are completely arbitrary commands, and you can put any arithmetic expression you want in these positions.
The for((i=1;i<=10;i++)) loop could be rewritten as such:
i=1 # for loop's 'i=1'
while [[ "${i}" -le 10 ]] # for loop's 'i<=10'
do
echo $i
i=$((i+1))
echo $i
i=$((i+2))
((i++)) # for loop's 'i++'
done
This generates ... you guessed it ...
1
2
5
6
9
10
Charles Duffy has already expanded the for i in {1..10} loop to show how that behaves.
This question already has answers here:
Shell script read missing last line
(7 answers)
Closed 6 years ago.
I am having some difficulty understand why i'm not able to enter my while read j loop. The goal of my program is to enter files 1-5 and calculate the sum of the numbers in each file. The output i'm getting is showing me that the read j line is executed, however the while loop isn't entered. This doesn't make intuitive sense to me, since the while condition would be true if the line is read. Heres the output, and the snippet of program below:
in for loop
file1
in for loop
1 9 6 3 3 6
file2
in for loop
1 3 7 6 4 4
file3
in for loop
1 4 8 8 2 4
file4
in for loop
1 5 9 9 1 7
file5
Code:
for (( n=0;n<$columns;n++ ))
do
echo "in for loop"
echo $j
filename="file$counter"
echo "$filename"
#while reading line from current file, take the numbers and find the sum
while read j
do
echo "in while loop"
for number in $j
do
sum=$(($sum + $number))
done
echo "Sum: $sum"
done < $filename
counter=$(($counter + 1))
done
Solution: Add a newline character to the end of the line in each file, read looks for the newline character to recognize a line.
I want to use bash script to generate some files. The file names will be in the format 2_x.yRandom.txt, where x is 2, 4, 6, 8, 10 and y is from 1 to 5.
eg. "2_2.2Random.txt" or "2_4.3Random.txt"
This is my script:
#Generate input for sort1
for i in 2 4 6 8 10
do
for j in 1 2 3 4 5
do
java utils.StringGenerator r 2 $i > "2_$i.$jRandom.txt"
java utils.StringGenerator s 2 $i > "2_$i.$jSorted.txt
java utils.StringGenerator v 2 $i > "2_$i.$jReversed.txt
done
done
The output file is always 2_2..txt or 2_4..txt, it seems that $j is not in the output.
What am I doing wrong?
Thanks!
PS: I'm using a Mac.
You forgot to tell bash where the variable name ends.
java utils.StringGenerator r 2 $i > "2_$i.${j}Random.txt"