'Problems with Case statements in MacOS ZSH
I'm trying to run an exercise code about "case" statement in the ZSH shell of MacOS Monterey. My code is not simple do describe but, in a nutshell, the code simply receives a positive integer number and should pass one of these statements + the final statement. Here is the code:
#!/bin/zsh
Bin=$(bc <<< "obase=2; $1")
Zeros=0000
Len=${#Bin}
Bin=${Zeros:$len}$Bin
case $Bin in
1[01][01][01]) echo Error type 8 ;;&
[01]1[01][01]) echo Error type 4 ;;&
[01][01]1[01]) echo Error type 2 ;;&
[01][01][01]1) echo Error type 1 ;;&
0000) echo No Error ;;&
*) echo Final Binary: $Bin
esac
But the shell gives this error message:
case.sh:17: parse error near `&'
I tried to change ;;& to ;| but it gives me another error message:
/case.sh:18: parse error near `)'
After one hour of thinking, I couldn't reach a solution.
The input of code:
$ case.sh 5
The code should return:
Error type 4
Error type 1
Final Binary:0101
Can somebody gives me a hand on this? Please?
Solution 1:[1]
Here is the solution of my code:
#!/bin/zsh
Bin=$(bc <<< "obase=2; $1")
Zeros=000
Len=${#Bin}
Bin=${Zeros:$len}$Bin
case $Bin in
[0-9]*1[01][01][01]) echo Error type 8 ;|
[0-9]*[01]1[01][01]) echo Error type 4 ;|
[0-9]*[01][01]1[01]) echo Error type 2 ;|
[0-9]*[01][01][01]1) echo Error type 1 ;|
0000) echo No Error ;|
*) echo Final binary: $Bin ;;
esac
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rafa_izu |
