I have to make bash script that will print my name as many times as i put number.
#! /bin/bash
echo "Please enter a number"
read n
until $n
do
echo "$n My name"
done
Maintain an explicit counter. For example, here's one that starts with n
and counts down to 0.
echo "Please enter a number"
read n
i=$n
until [ "$i" -lt 1 ]
do
echo "$i My name"
i=$((i-1))
done
This will work in any POSIX-compliant shell; since you are using bash
, you can use a C-style for
loop to maintain the counter.
echo "Please enter a number"
read n
for ((i=0; i < n; i++)); do
echo "$i My name"
done
With two building blocks:
for i in some whitespace-separated words
do
echo "Hello, $i"
done
This will execute the block inside the loop for every word. It will output the following:
Hello, some
Hello, whitespace-separated
Hello, words
The term {1..3}
will get replaced by 1 2 3
at runtime.
Now you know how to execute something 20 times:
for i in {1..20}
do
something
done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.