User Tools

Site Tools


Action disabled: source
cs:bash_language:example_2
Return to Home page

Example 2

Concepts:
Statements while and if, mathematical operations and continue command.

Text:
Implement a bash script that prints the number between 1 and 20, with the exception of the numbers 4 and 13.

An example of the output of the script is:

Print the numbers between 1 and 20 (with the exclusion of the numbers 4 and 13).
1 2 3 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20

Solution:

bash_ex2.sh
#!/bin/bash
 
LIMIT=20  # Superior limit
 
echo "Print the numbers between 1 and 20 (with the exclusion of the numbers 4 and 13)."
 
a=0
 
while [ $a -lt "$LIMIT" ]
do
    a=$(($a+1))
    # a=$[$a+1] # Another possible way to compute mathematical operations
 
    if [ "$a" -eq 4 ] || [ "$a" -eq 13 ]  # Exclusion of 4 and 13 numbers
    then
        continue  # Skip the rest of this cycle
    fi
 
    echo -n "$a "
done 
echo # Print new line
 
exit 0

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/bash_language/example_2?do=edit
/web/htdocs/www.skenz.it/home/data/pages/cs/bash_language/example_2.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki
Privacy Policy