Shell Implementation Fizzbuzzwhizz Example (Pull network interview question) _linux Shell

Source: Internet
Author: User

Question: Https://www.jinshuju.net/f/EGQL3D

Language: Shell (Bash)
Operation mode:
SH num-game.sh digit 1 digit 2 Number 3
If you do not want to use the method of passing numbers, you can modify num-game.sh line 9th to
num=$*
To
num= ' number 1 digit 2 digit 3 '
Then the direct operation of SH num-game.sh can

Copy Code code as follows:

#!/bin/bash

# # Author:taxkiz
# # Email:taxkiz@gmail.com

Export path= '/bin:/sbin:/usr/bin:/usr/sbin '

# # num= ' 3 5 7 '
num=$*

# # Check whether the number entered is 3
function CT () {
[$ (Echo $* | sed ' s//\n/g ' | wc-l)!= ' 3 '] && echo "please Enter 3 number!" && exit 1
}

CT $NUM

# # single Digit list
Nt= ' 1/2/3/4/5/6/7/8/9 '

# # Check if the number entered is a single-digit number
function CK () {
For num in $*
Todo
[$num = = ' 0 '] && echo "Error number ' $num '" && exit 2
echo $NT | grep "\b$num\b" >/dev/null 2>&1
[$?!= ' 0 '] && Echo ' $num is not a single digit ' && exit 3
Done
}

CK $NUM

# # To determine if there are duplicate numbers
function SN () {
If [$ = $] | | [$ = = $] | | [$ = $]; Then
echo "error! There have same number: ' $* '
Exit 4
Fi
}

SN $NUM

# # Yell A cry
function Go () {

A1=$1
A2=$2
A3=$3

t1=$ (($ * $))
t2=$ (($ * $))
t3=$ (($ * $))

ta=$ (($ * $))

For num in {1..100}
Todo
echo $num | grep $A 1 >/dev/null 2>&1
[$ = = ' 0 '] && Echo ' Fizz ' && continue

echo "scale=2; $num/$TA "| BC | grep >/dev/null 2>&1
[$ = = ' 0 '] && echo ' Fizzbuzzwhizz ' && continue

echo "scale=2; $num/$T 1 "| BC | grep >/dev/null 2>&1
[$ = = ' 0 '] && echo ' Fizzbuzz ' && continue

echo "scale=2; $num/$T 2 "| BC | grep >/dev/null 2>&1
[$ = = ' 0 '] && echo ' Buzzwhizz ' && continue

echo "scale=2; $num/$T 3 "| BC | grep >/dev/null 2>&1
[$ = = ' 0 '] && echo ' Fizzwhizz ' && continue

echo "scale=2; $num/$A 1 "| BC | grep >/dev/null 2>&1
[$ = = ' 0 '] && Echo ' Fizz ' && continue

echo "scale=2; $num/$A 2 "| BC | grep >/dev/null 2>&1
[$ = = ' 0 '] && echo ' Buzz ' && continue

echo "scale=2; $num/$A 3 "| BC | grep >/dev/null 2>&1
[$ = = ' 0 '] && echo ' Whizz ' && continue

Echo $num

Done
}

Go $NUM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.